views:

335

answers:

2

I would like to check if my application is being run on VMWare. Is there a reliable way to do this in C++?

+2  A: 

I just found this assembly on codeproject.

http://www.codeproject.com/KB/system/VmDetect.aspx

Daniel A. White
+1  A: 

I think this link might be able to help you out. Its in assembly, not C++, but you could always create an assembly block in your C++...

////////////////////////////////////////////////////////////////////////////////
//
//  Simple VMware check on i386
//
//    Note: There are plenty ways to detect VMware. This short version bases
//    on the fact that VMware intercepts IN instructions to port 0x5658 with
//    an magic value of 0x564D5868 in EAX. However, this is *NOT* officially
//    documented (used by VMware tools to communicate with the host via VM).
//
//    Because this might change in future versions - you should look out for
//    additional checks (e.g. hardware device IDs, BIOS informations, etc.).
//    Newer VMware BIOS has valid SMBIOS informations (you might use my BIOS
//    Helper unit to dump the ROM-BIOS (http://www.bendlins.de/nico/delphi).
//
function IsVMwarePresent(): LongBool; stdcall;  // platform;
begin
  Result := False;
 {$IFDEF CPU386}
  try
    asm
            mov     eax, 564D5868h
            mov     ebx, 00000000h
            mov     ecx, 0000000Ah
            mov     edx, 00005658h
            in      eax, dx
            cmp     ebx, 564D5868h
            jne     @@exit
            mov     Result, True
    @@exit:
    end;
  except
    Result := False;
  end;
{$ENDIF}
end;

As with any code from the internet, be careful of merely copying & pasting it and expecting it to work perfectly.

CrimsonX
This appears to cause a crash on Win2k8 server and possibly Windows 7 32bit OS unfortunatly
Mike Trader
You probably need to wrap it in a Win32 "structured" exception handler. If you get an invalid instruction exception or similar, you're not in VMware. http://msdn.microsoft.com/en-us/library/s58ftw19%28VS.80%29.aspx
Tim Sylvester