Cora
Cora

Reputation: 269

Is it possible using C# to check if hardware virtualization enabled?

Couldn't find an answer, so I thought I ask for myself.

Using C#, how can I check if the CPU has hardware virtualization enabled? Hyper-V, for example. It's not in the Win32_Processor class, and my attempt to connect to the MSVM_ComputerSystem was met with utter failure. Keeps telling me there's an invalid namespace.

Code below.

ManagementObjectSearcher vm = new ManagementObjectSearcher(@"\\.\root\virtualization", "select * from MSVM_Computersystem");
ManagementObjectCollection vmCollection = vm.Get();
foreach (ManagementObject mo in vmCollection)
{
     cpuVirtual = mo["EnabledState"].ToString();
}

Upvotes: 1

Views: 2867

Answers (2)

Claudiu
Claudiu

Reputation: 1

Just an FYI, "root/virtualization" namespace no longer exists, starting with Windows / Hyper-V Server 2012 R2 (possibly Windows 8.1 too). Also, it might be because you wrote "root\virtualization" (try forward slashes). You should be using "root/virtualization/v2" anyways.

Upvotes: 0

Mr Rivero
Mr Rivero

Reputation: 1248

if you enumerate all properties in Win32_Processor class, you see a property named "VirtualizationFirmwareEnabled", I think you are talking about this property.

As you can see in this link, to check if some machine processor can run on Hyper-V, they use VirtualizationFirmwareEnabled" in conjunction to other properties:

I wrote this simple snipped to iterate over all win32_processor class values:

ManagementClass managClass = new ManagementClass("win32_processor");
ManagementObjectCollection managCollec = managClass.GetInstances();

foreach (ManagementObject managObj in managCollec)
{
   foreach (var prop in managObj.Properties)
   {
       Console.WriteLine("Property Name: {0} Value: {1}",prop.Name,prop.Value);
   }               
}

Console.ReadKey();

Upvotes: 3

Related Questions