【windows】查询处理器架构

问题: 在 arm 架构的 windows 上使用 GetSystemInfo 获取到的 wProcessorArchitecture 参数不准确。

由于 arm 架构的 windows 操作系统非常少见,所以客户端上线时并没有回归过检测 arm 操作系统。导致上线后出现驱动安装失败问题。

[toc]

使用 GetSystemInfo 查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
std::string GetNcProcessorArchitecture()
{
SYSTEM_INFO si = { 0 };
GetSystemInfo(&si);
DWORD architecture = si.wProcessorArchitecture;
#if 0
#define PROCESSOR_ARCHITECTURE_INTEL 0
#define PROCESSOR_ARCHITECTURE_MIPS 1
#define PROCESSOR_ARCHITECTURE_ALPHA 2
#define PROCESSOR_ARCHITECTURE_PPC 3
#define PROCESSOR_ARCHITECTURE_SHX 4
#define PROCESSOR_ARCHITECTURE_ARM 5
#define PROCESSOR_ARCHITECTURE_IA64 6
#define PROCESSOR_ARCHITECTURE_ALPHA64 7
#define PROCESSOR_ARCHITECTURE_MSIL 8
#define PROCESSOR_ARCHITECTURE_AMD64 9
#define PROCESSOR_ARCHITECTURE_ARM64 12
#endif

if (architecture == PROCESSOR_ARCHITECTURE_AMD64)
{
return "amd64";
}
else if (architecture == PROCESSOR_ARCHITECTURE_INTEL)
{
return "i386";
}
else if (architecture == PROCESSOR_ARCHITECTURE_ARM64 || architecture == PROCESSOR_ARCHITECTURE_ARM)
{
return "arm";
}

return "";
}

使用 GetSystemInfo 查询的 wProcessorArchitecture 的值为 0

查询注册表

调研一番后,发现使用注册表的方式更稳妥一点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <Windows.h>

int main() {
HKEY hKey;
TCHAR processorArchitecture[255];
DWORD bufferSize = sizeof(processorArchitecture);

// 打开注册表键
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"), 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
// 读取注册表值
if (RegQueryValueEx(hKey, TEXT("PROCESSOR_ARCHITECTURE"), nullptr, nullptr, reinterpret_cast<LPBYTE>(processorArchitecture), &bufferSize) == ERROR_SUCCESS) {
std::cout << "Processor Architecture: " << processorArchitecture;
}

// 关闭注册表键
RegCloseKey(hKey);
}

return 0;
}

【windows】查询处理器架构
https://hodlyounger.github.io/A_OS/Windows/查询系统信息/查询处理器架构/
作者
mingming
发布于
2023年10月27日
许可协议