您的位置:首页 > 其它

如何检测当前操作系统是64位还是32位

2010-10-20 14:25 387 查看
目前有几种方法:

一,采用MSDN推荐的一个方法,通过调用kernel.dll中的IsWow64Process来判断系统是否是64位的OS。

MSDN中给了一段代码:

BOOL IsWow64()
{
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL bIsWow64 = FALSE;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle(_T("kernel32")), "IsWow64Process");
if (NULL != fnIsWow64Process)
{
fnIsWow64Process(GetCurrentProcess(),&bIsWow64);
}
return bIsWow64;
}

不过这个方法之适用于Win32_based application运行于WOW的情况(WOW的含义可查询MSDN)。如果Win64_based application 运行于64位操作系统上,bIsWow64的返回值将会是FALSE。因此MSDN中有如下一段话:

To determine whether a Win32-based application is running on WOW64, call the IsWow64Process function. To determine whether the system is running a 64-bit version of Windows, call the GetNativeSystemInfo function.

另外,如果你用Visual Studio进行编译,默认安装则只包含32位的编译器/链接器,即便你是在64位操作系统上也会生成32位程序。

同时,在64位的操作系统上运行的kernel32.dll中,将会实现IsWow64Process方法,而在32位系统中提供的kernel32.dll中则没有提供相关函数的实现。因此上面代码需要加以改进才能正常运行。

BOOL IsWow64(void)
{
UINT unResult = 0;
int nResult = 0;
TCHAR szWinSysDir[MAX_PATH+1] = _T("");
TCHAR szKernel32File[MAX_PATH+1+14] = _T("");
HINSTANCE hLibKernel32 = NULL;
BOOL bIsWow64Process = FALSE;

typedef BOOL (WINAPI *ISWOW64)(HANDLE,PBOOL);
ISWOW64 lpIsWow64Process = NULL;

unResult = GetSystemDirectory(szWinSysDir,sizeof(szWinSysDir)/sizeof(TCHAR));
if(unResult > 0){
nResult = _stprintf(szKernel32File,_T( "%s//kernel32.dll "),szWinSysDir);
if(nResult > 0){
hLibKernel32 = LoadLibrary(szKernel32File);
}
}

if(NULL == hLibKernel32){
hLibKernel32 = LoadLibrary(_T( "kernel32.dll "));
}

// Get the Address of Win32 API -- IsWow64Process()
if( NULL != hLibKernel32 ){
lpIsWow64Process = (ISWOW64) GetProcAddress(hLibKernel32,"IsWow64Process");
}

if(NULL != lpIsWow64Process){
// Check whether the 32-bit program is running under WOW64 environment.
if(!lpIsWow64Process(GetCurrentProcess(),&bIsWow64Process)){
FreeLibrary(hLibKernel32);
return FALSE;
}
}
if(NULL != hLibKernel32){
FreeLibrary(hLibKernel32);
}

return bIsWow64Process;
}

二,正如上面提到的MSDN中的说明,如果需要获取更多详细的信息,推荐调用GetNativeSystemInfo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: