【发布时间】:2020-03-08 10:43:42
【问题描述】:
如果当前的 Windows 安装处于 UEFI 或传统模式,我的目标是简单地获取。手动我可以运行 msinfo32 并查看“Bios-Mode”行。我目前的方法是检查“HKLM\SYSTEM\CurrentControlSet\Control\SecureBoot”键是否存在,但我不确定这是否是一个好方法。
我已经尝试查看相当多的 WMI 类和属性,但它们都没有我需要的信息。我读到你可以使用 GetFirmwareType() 但它似乎已经在 Windows 8 中引入,我希望它也可以在 Windows 7 上运行。我还尝试了微软给我的方法,用一个虚拟变量和一个虚拟命名空间调用 GetFirmwareEnvironmentVariableA。
public const int ERROR_INVALID_FUNCTION = 1;
[DllImport("kernel32.dll",
EntryPoint = "GetFirmwareEnvironmentVariableA",
SetLastError = true,
CharSet = CharSet.Unicode,
ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int GetFirmwareType(string lpName, string lpGUID, IntPtr pBuffer, uint size);
public static bool IsWindowsUEFI()
{
// Call the function with a dummy variable name and a dummy variable namespace (function will fail because these don't exist.)
GetFirmwareType("", "{00000000-0000-0000-0000-000000000000}", IntPtr.Zero, 0);
if (Marshal.GetLastWin32Error() == ERROR_INVALID_FUNCTION)
{
// Calling the function threw an ERROR_INVALID_FUNCTION win32 error, which gets thrown if either
// - The mainboard doesn't support UEFI and/or
// - Windows is installed in legacy BIOS mode
return false;
}
else
{
// If the system supports UEFI and Windows is installed in UEFI mode it doesn't throw the above error, but a more specific UEFI error
return true;
}
}
当我收到 ERROR_INVALID_FUNCTION 时它告诉我,我正在运行旧版,否则它将返回一个不同的、更具体的错误。我从该代码中得到的只是任何类型的系统上的 ERROR_INVALID_PARAMETER,我不知道我的错误在哪里。
【问题讨论】:
-
您请求的是
CharSet.Unicode,但要导入ANSI 版本(GetFirmwareEnvironmentVariableA)。这种不匹配很可能会导致 API 返回一个错误代码,表明参数无效。 -
哦,我已经添加了这个,因为我已经在一个示例中看到了这种组合。真没礼貌。看看能不能解决问题
-
由于您的回答是评论而不是答案,我认为我不能接受它,但感谢您提及这一点。将其更改为“.Ansi”显然完成了这项工作。