如何通过代码确定硬件虚拟化是否可用?(C#, C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/840873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine if hardware virtualization is available via code? (C#, C++)
提问by Raj Rao
How can I detect if a system supports hardware virtualization via code? (Preferably in C# or C++).
如何通过代码检测系统是否支持硬件虚拟化?(最好使用 C# 或 C++)。
I tried using WMI and ManagementObjectSearcher, and could not find a property that seemed to represent if virtualization support was present or not in the machine.
我尝试使用 WMI 和 ManagementObjectSearcher,但找不到似乎表示机器中是否存在虚拟化支持的属性。
Bonus question: Is it possible to tell if the CPU supports HW virtualization, but disabled in BIOS?
额外问题:是否可以判断 CPU 是否支持硬件虚拟化,但在 BIOS 中被禁用?
采纳答案by DMI
I think the original poster is asking how to detect that the computer hardware supports virtualisation, not that it is running inside a virtual machine. See this questionfor a possible answer.
我认为原始海报是在询问如何检测计算机硬件是否支持虚拟化,而不是它是否在虚拟机内运行。请参阅此问题以获得可能的答案。
Also, it might be worth seeing if you can check the CPU flags, as there will either be the Intel vmx
flag or the AMD svm
flag if the processor supports virtualisation extensions. I can't say I know how this should be done under Windows, but the information is available in /proc/cpuinfo
on Linux.
此外,如果您可以检查 CPU 标志,可能值得一看,因为如果处理器支持虚拟化扩展,就会有 Intelvmx
标志或 AMDsvm
标志。我不能说我知道在 Windows 下应该如何做到这一点,但是/proc/cpuinfo
在 Linux 上可以找到这些信息。
回答by Slipfish
回答by gvlx
回答by Kellerman Rivero
Check this link, as you can see there, maybe you should use WMI classes to get info. .NET framework has System.Management namespace to do that. Win32_processor class can give you all info you need about processor, because you can't access the cpu flags directly using win32_processor class, maybe you need to check the value of VirtualizationFirmwareEnabled
检查此链接,正如您在那里看到的那样,也许您应该使用 WMI 类来获取信息。.NET 框架有 System.Management 命名空间来做到这一点。Win32_processor 类可以为您提供有关处理器的所有信息,因为您无法使用 win32_processor 类直接访问 cpu 标志,也许您需要检查 VirtualizationFirmwareEnabled 的值
回答by Mark Minasi
I do not have a C-ish answer, but here's a PowerShell approach -- it's just a simple WMI query -- that can help.
我没有 C 语言的答案,但这里有一个 PowerShell 方法——它只是一个简单的 WMI 查询——可以提供帮助。
Query Win32_Processor; there are flags "SecondLevelAddressTranslationExtensions" and "VirtualizationFirmwareEnabled." They seem to answer the questions for me.
查询 Win32_Processor; 有标志“SecondLevelAddressTranslationExtensions”和“VirtualizationFirmwareEnabled”。他们似乎在为我回答问题。
In PoSH, it's
在 PoSH 中,它是
(GWMI Win32_Processor).VirtualizationFirmwareEnabled
(GWMI Win32_Processor).VirtualizationFirmwareEnabled
and
和
(GWMI Win32_Processor).SecondLevelAddressTranslationExtensions
(GWMI Win32_Processor).SecondLevelAddressTranslationExtensions
I hope this helps.
我希望这有帮助。
Mark Minasi
马克·米纳西