1

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();
}
Cora
  • 269
  • 3
  • 17

2 Answers2

3

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();
Mr Rivero
  • 1,248
  • 7
  • 17
  • I ran that code but nowhere in my console does there appear to be a **VirtualizationFirmwareEnabled** value. Inserting it into my `foreach` loop causes the exception _Not found_ – Cora Jul 19 '14 at 17:02
  • What processor are you using? Maybe the non-presence of property **VirtualizationFirmwareEnabled** or the presence but with value false means the virtualization is not supported. When I run the code snippet in VStudio 2013, with .NET Framework 4.5, I saw the property, my machine has intel Core i7 with Virtualization Support, in fact I'm running Windows Guess with OSX Host. – Mr Rivero Jul 19 '14 at 17:06
  • I'm running an i5-3570K with Hyper-V currently disabled. That command also shows negative in PowerShell. – Cora Jul 19 '14 at 17:15
  • Can you enabled Hyper-V on the Firmware BIOS or UEFI and check if the code shows property? – Mr Rivero Jul 19 '14 at 17:16
  • Still fails in PowerShell, still breaks in VS – Cora Jul 19 '14 at 17:22
  • Also, the provided tutorial is for Windows 8: I'm loaded into a Windows 7 environment. – Cora Jul 19 '14 at 17:27
  • @DarkWolffe: Windows 8 shouldn't matter. Enumerate the properties using a `for` loop with a `try/catch` and then look for the presence or absence of these properties. That may give you enough hints – Raheel Khan Jul 19 '14 at 19:46
  • @RaheelKhan I believe is for Windows version, sounds rarely, but can be that. Reviewing [this](http://fossies.org/linux/misc/apache-cloudstack-4.3.0-src.tar.gz/apache-cloudstack-4.3.0-src/plugins/hypervisors/hyperv/DotNet/ServerResource/WmiWrappers/ROOT.CIMV2.Win32_Processor.cs) wrapper around WMI, they use IsVirtualizationFirmwareEnabledNull property to determine when **VirtualizationFirmwareEnabled** is not present on Win32_Processor class, another approach is use __cpuid present on Windows C++, and create a wrapper around this, maybe like a Windows DLL, and use it from C# Program. – Mr Rivero Jul 20 '14 at 00:39
  • 1
    After research, I found this [code](http://code.ohloh.net/file?fid=3jeDxQS6AOZAm9CMrrulNfUBkJI&cid=8jg1cJFyros&s=&fp=465146&mp&projSelected=true#L0), here you will see the code we shaw you, only works on Windows 8 or later. That is the reason because you don't see the property in Win32_Processor class, can be useful if you review the code. – Mr Rivero Jul 20 '14 at 14:14
  • So his answer isn't _wrong_ in any way, it just is only supportive in the newer environments. – Cora Jul 21 '14 at 09:40
  • Maybe you should use SecondLevelAddressTranslationExtensions property if is visible on Windows 7, else I believe the unique approach you can use is a wrapper around __cpuid. – Mr Rivero Jul 21 '14 at 12:40
0

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.