VC++的代码,虽然有点老但是很实用,发出来留做备用!
#include "windows.h" DWORD __forceinline IsInsideVPC_exceptionFilter(LPEXCEPTION_POINTERS ep); bool IsInsideVPC(); bool IsInsideVMWare(); int CheckVPC(); #ifndef _DEBUG #pragma comment(linker, "/ENTRY:EntryPoint") #pragma comment(linker, "/SECTION:VPC,") #pragma comment(linker, "/MERGE:.data=VPC") int EntryPoint() { CheckVPC(); ExitProcess(0); } #else int WINAPI WinMain(IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, IN LPSTR lpCmdLine, IN int nShowCmd ) { return CheckVPC(); } #endif // _DEBUG int CheckVPC() { if(IsInsideVPC()) MessageBox(NULL, "你在虚拟电脑Microsoft Virtual PC中!", "提示", MB_OK|MB_ICONINFORMATION); else if(IsInsideVMWare()) MessageBox(NULL, "你在虚拟电脑VMWare中!", "提示", MB_OK|MB_ICONINFORMATION); MessageBox(NULL, "你在真实的电脑中!", "提示", MB_OK|MB_ICONINFORMATION); return 0; } DWORD __forceinline IsInsideVPC_exceptionFilter(LPEXCEPTION_POINTERS ep) { PCONTEXT ctx = ep->ContextRecord; ctx->Ebx = -1; ctx->Eip += 4; return EXCEPTION_CONTINUE_EXECUTION; } bool IsInsideVPC() { bool rc = false; __try { _asm push ebx _asm mov ebx, 0 _asm mov eax, 1 _asm __emit 0Fh _asm __emit 3Fh _asm __emit 07h _asm __emit 0Bh _asm test ebx, ebx _asm setz [rc] _asm pop ebx } // The except block shouldn't get triggered if VPC is running!! __except(IsInsideVPC_exceptionFilter(GetExceptionInformation())) { } return rc; } bool IsInsideVMWare() { bool rc = true; __try { __asm { push edx push ecx push ebx mov eax, 'VMXh' mov ebx, 0 mov ecx, 10 mov edx, 'VX' in eax, dx cmp ebx, 'VMXh' setz [rc] pop ebx pop ecx pop edx } } __except(EXCEPTION_EXECUTE_HANDLER) { rc = false; } return rc; }