VC++如何判断当前操作系统是32位还是64位?
2015-12-24 15:31
597 查看
方法如下:
[cpp] view
plaincopy
#include <windows.h>
#include <tchar.h>
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL IsWow64()
{
BOOL bIsWow64 = FALSE;
//IsWow64Process is not available on all supported versions of Windows.
//Use GetModuleHandle to get a handle to the DLL that contains the function
//and GetProcAddress to get a pointer to the function if available.
fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if(NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
{
//handle error
}
}
return bIsWow64;
}
int main( void )
{
if(IsWow64())
_tprintf(TEXT("The process is running under WOW64.\n"));
else
_tprintf(TEXT("The process is not running under WOW64.\n"));
return 0;
}
注意,是判断当前操作系统,不是判断CPU
不能通过 #ifdef _WIN64 来判断,因为如果开发环境中定义了该宏,则该判断永远为真
参考:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684139%28v=vs.85%29.aspx
[cpp] view
plaincopy
#include <windows.h>
#include <tchar.h>
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL IsWow64()
{
BOOL bIsWow64 = FALSE;
//IsWow64Process is not available on all supported versions of Windows.
//Use GetModuleHandle to get a handle to the DLL that contains the function
//and GetProcAddress to get a pointer to the function if available.
fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if(NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
{
//handle error
}
}
return bIsWow64;
}
int main( void )
{
if(IsWow64())
_tprintf(TEXT("The process is running under WOW64.\n"));
else
_tprintf(TEXT("The process is not running under WOW64.\n"));
return 0;
}
注意,是判断当前操作系统,不是判断CPU
不能通过 #ifdef _WIN64 来判断,因为如果开发环境中定义了该宏,则该判断永远为真
参考:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684139%28v=vs.85%29.aspx
相关文章推荐
- VC++实现恢复SSDT
- VC++程序中用PlaySound函数加入自定义声音
- C++小题(二)
- OC语言 内存管理
- c语言中的rand()函数和srand()函数产生随机的整数 (转载)
- C++类型转换详解--const_cast
- C++ Primer Plus 第六版(中文版)课后编程题----第四章
- c/c++ ,gettext,软件的多语言支持(i18n)
- Effective C++(条款5-12)
- C++ 同名隐藏、覆盖,虚函数 的详解和对比
- 筛法与素数
- C#调用 C++的DLL
- 内联函数的effective C++解释
- C语言学习笔记--------伊能C语言学习笔记------C语言结构体字节对齐机制
- 用C语言判断象限
- C语言中通过分隔符来截取字符串
- 【c语言】统计一个数二进制中的1的个数
- 六种流行的语言大餐---C、C++、python、Java、php、C#你更喜欢哪一个呢?
- 高效C++无锁队列实现(转载)
- C语言学习笔记