使用C++检测Windows体系结构(32或64位)-wProcessorArchitecture显示错误的结果
Detect Windows architecture (32 or 64 bit) with C++ - wProcessorArchitecture shows wrong results
我使用这段小代码来了解我的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("%dn", is64bitOS());
return 0;
}
我购买并安装了64位版本的Windows7-然而,上面的代码显示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, ' ', 255);
#if defined _WIN64
strcpy(archType, "64-bit");
#else
strcpy(archType, "32-bit");
#endif // defined
return archType;
}
int main()
{
char *arch = getArchType1();
printf("%sn", arch);
free(arch);
printf("%dn", getArchType2());
char c;
scanf("%c", &c);
return 0;
}
来自MSDN:
GetNativeSystemInfo函数
将有关当前系统的信息检索到在WOW64下运行的应用程序。如果该函数是从64位应用程序调用的,则它等效于
GetSystemInfo
函数。
GetSystemInfo描述中有一个提示:
要检索在WOW64上运行的应用程序的准确信息,请调用
GetNativeSystemInfo
函数。
它基本上解释了您请求的是有关所处环境的信息,而不是真实的系统体系结构。
它也给了你另一个提示,在64位Windows上,你可以同时执行两个代码:Win32
和x64
,这取决于你要针对哪个平台。
另请参阅:
- 是否有一个Windows API调用会告诉我如果我';我在64位操作系统上运行
- 获取Windows体系结构(32/64位版本(
- 检测cpu体系结构的正确方法
相关文章:
- 没有找到相关文章