Sometimes you need to programmatically determine whether a program or a command you wish to run is running within a virtualized environment.

On x64 platforms you can execute the CPUID instruction with an input (EAX) value of 1. 

Check bit 31 of register ECX (the ―hypervisor present bit). If this bit is set, a hypervisor is present. In a non-virtualized environment, the bit will be clear.

Here is sample code provided by a colleague at Microsoft: 

#include <intrin.h>

// Returns 0 if the machine is not a virtualized environment; 1 otherwise

// Reference: Hypervisor Funcspec

// Section 3.2 - Hypervisor Discovery

extern "C" __declspec(dllexport) int IsVirtualizedEnvironment()


int CPUInfo[4];                          // EAX, EBX, etc...

       __cpuid(CPUInfo, 1);                      // Check bit 31 of EBX

return (CPUInfo[2] & (1<<31)) ? 1 : 0;