判断程序是否在VMWare内运行

现在有许多用户都喜欢用虚拟机来测试他们的软件,以避免对真实机器环境造成损害。但是在虚拟机中,有些功能是受限,甚至不可能完成的,因此,需要在程序中判断虚拟机的环境,如果程序在虚拟机内运行,则就要把虚拟机下不能使用的功能屏蔽掉。

判断程序是否在VMWare虚拟机内,可以用以下代码来完成:

function IsRunInVMWare(out ErrMsg: string): Boolean;
begin
Result := False;
try
    asm
      push     edx
      push     ecx
      push     ebx
      mov      eax, 'VMXh'
      mov      ecx, $0A
      mov      edx, 'VX'
      in       eax, dx
      cmp      ebx, 'VMXh'
      setz     [Result]
      pop      ebx
      pop      ecx
      pop      edx
    end;
except
    on E: Exception do
      ErrMsg := E.Message;
end;
end;

本段代码在Delphi2009下编译通过,VMWare 5/6测试通过。

原文地址:https://www.cnblogs.com/findumars/p/5541011.html