如何检测当前 Windows 是否安装在 UEFI 或传统模式下
How do I detect if the current Windows is installed in UEFI or legacy mode
我的目标是简单地获取当前 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 时它告诉我,我是 运行ning 遗留问题,否则它会 return 一个不同的、更具体的错误。在任何类型的系统上,我从该代码中得到的都是 ERROR_INVALID_PARAMETER,我不知道我的错误在哪里。
那只是一个打字错误。
3 [DllImport("kernel32.dll",
4 > EntryPoint = "GetFirmwareEnvironmentVariableA",
5 SetLastError = true, ^
将 >
和 ^
指向的 A
替换为 W
。
我的目标是简单地获取当前 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 时它告诉我,我是 运行ning 遗留问题,否则它会 return 一个不同的、更具体的错误。在任何类型的系统上,我从该代码中得到的都是 ERROR_INVALID_PARAMETER,我不知道我的错误在哪里。
那只是一个打字错误。
3 [DllImport("kernel32.dll",
4 > EntryPoint = "GetFirmwareEnvironmentVariableA",
5 SetLastError = true, ^
将 >
和 ^
指向的 A
替换为 W
。