判断程序是否运行在wow64模式下

本文深入探讨了WOW64模式的概念,它允许32位Windows应用程序无缝运行在64位Windows系统上。通过提供关键函数的解释和示例代码,帮助开发者了解如何检测和利用此功能。文章还讨论了该方法的局限性,即无法可靠地检测操作系统的64位版本,因为32位Windows系统也包含此功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天review同事的代码,碰巧看到这一段,自己对这些也不熟悉。碰巧遇到就查阅资料记录下来。

wow64模式,MSDN的解释如下:

WOW64 is the x86 emulator that allows 32-bit Windows-based applications to run seamlessly on 64-bit Windows.

简单说来,就是64位的系统提供一个模拟环境,使得32位程序也能无差别(与运行在32位系统上)。

BOOL IsWow64Process(HANDLE hProcess)
{
	typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
	LPFN_ISWOW64PROCESS fn_IsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(_T("kernel32.dll",	"IsWow64Process"));
	BOOL bIsWow64 = FALSE;
	if (NULL != fn_IsWow64Process) {
		if (!fn_IsWow64Process(hProcess, &bIsWow64)) {
			//maybe some error
		}
	}
	return bIsWow64;
}

基本思路如上述,没有验证,使用时请小心错误。


值得注意的是MSDN提到:

If the application is a 64-bit application running under 64-bit Windows, theWow64Process parameter is set to FALSE.

To compile an application that uses this function, define _WIN32_WINNT as 0x0501 or later.


MSDN给出的参考代码:

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

typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

LPFN_ISWOW64PROCESS fnIsWow64Process;

BOOL IsWow64()
{
    BOOL bIsWow64 = FALSE;

    fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
        GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
  
    if (NULL != fnIsWow64Process)
    {
        if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
        {
            // handle error
        }
    }
    return bIsWow64;
}

void main()
{
    if(IsWow64())
        printf("Running on WOW64\n");
    else printf("Running on 32-bit Windows\n");
}


很有趣的是MSDN提到了:

Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function.
这个方法不能判断操作系统是否是64位的,因为32位版本的windows系统也有这个函数,哈哈!坑多,要小心!

2015-02-28 17:31


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值