gpt4 book ai didi

c++ - 使用 C++ 检测 Windows 体系结构(32 位或 64 位)- wProcessorArchitecture 显示错误的结果

转载 作者:可可西里 更新时间:2023-11-01 10:56:00 26 4
gpt4 key购买 nike

我使用这一小段代码来了解我的 Windows 是 32 位还是 64 位:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

static int is64bitOS()
{
SYSTEM_INFO si;
GetSystemInfo(&si);

if((si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_IA64)||(si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_AMD64)==64)
{
return 1;
}
else
{
return 0;
}
}

int main()
{
printf("%d\n", is64bitOS());
return 0;
}

我购买并安装了 64 位版本的 Windows 7 - 但是,上面的代码显示 0 表示我的操作系统是 32 位...如何处理?

好吧,我的其他方法基本上都不起作用......在我的 64 位 Windows 7 上我只能看到 32 位......

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>

int getArchType1()
{
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);

switch(sysInfo.wProcessorArchitecture)
{
case 6:
return 64;
case 9:
return 64;
case 0:
return 32;
default:
return -1;
}
}

static char *getArchType2()
{
char *archType = malloc(sizeof(char) * (255));
memset(archType, '\0', 255);

#if defined _WIN64
strcpy(archType, "64-bit");
#else
strcpy(archType, "32-bit");
#endif // defined

return archType;
}

int main()
{
char *arch = getArchType1();
printf("%s\n", arch);
free(arch);
printf("%d\n", getArchType2());
char c;
scanf("%c", &c);
return 0;
}

最佳答案

来自 MSDN:

GetNativeSystemInfo function

Retrieves information about the current system to an application running under WOW64. If the function is called from a 64-bit application, it is equivalent to the GetSystemInfo function.

GetSystemInfo 中有提示描述:

To retrieve accurate information for an application running on WOW64, call the GetNativeSystemInfo function.

它基本上解释了您请求的是关于您所处环境的信息,而不是真实的系统架构。

它还给了你另一个提示,在 64 位 Windows 上你可以同时执行代码:Win32x64,这取决于你什么平台目标。

另见:

关于c++ - 使用 C++ 检测 Windows 体系结构(32 位或 64 位)- wProcessorArchitecture 显示错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23375666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com