现在的位置: 首页 > 综合 > 正文

通过加载Kernel32来动态判断 当前操作系统32bit还是64bit

2013年12月03日 ⁄ 综合 ⁄ 共 1216字 ⁄ 字号 评论关闭

工作原理:通过加载Kernel32来获取IsWow64Process 函数然后通过函数的地址操作,执行函数的操作。

在程序中只要我们获取了一个函数的地址,就可以找到正确的方法执行这个函数。

下面是msdn上的一段话,用的时候请注意:

For
compatibility with operating systems that do not support this function, call 
GetProcAddress to
detect whether
IsWow64Process is
implemented in Kernel32.dll. If 
GetProcAddress succeeds,
it is safe to call this function. Otherwise, WOW64 is not present. 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.

#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;
}

抱歉!评论已关闭.