I recently had a case come up related to OpenGL where I needed to know programmatically if my application was running within VMWare Fusion.
It took me a while to understand and piece together a solution, but here is one based on the half-psuedo-code from VMWare’s site.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#ifdef Q_OS_WIN // Determine if we are running on VMWare or not. // This is based on code from here: // https://kb.vmware.com/s/article/1009458 bool _isVMware() { int cpuInfo[4]; char hyperVendorID[13]; __cpuid( cpuInfo, 0x1 ); if ( cpuInfo[2] & (1<<31) ) { __cpuid( cpuInfo, 0x40000000 ); memcpy( hyperVendorID + 0, &cpuInfo[1], 4 ); memcpy( hyperVendorID + 4, &cpuInfo[2], 4 ); memcpy( hyperVendorID + 8, &cpuInfo[3], 4 ); hyperVendorID[12] = '\0'; return !strcmp( hyperVendorID, "VMwareVMware" ); } return false; } #endif |
(Note: Q_OS_WIN is a Qt framework define. Use whatever you use to conditionally compile Windows-only code.)
I didn’t need the extra checks for testing the virtual BIOS DMI information and hypervisor port outlined in that VMWare page, so I didn’t try to implement them.
Hope this helps someone!