如何确定进程是32位还是64位?

时间:2013-01-06 16:27:30

标签: c++ windows winapi 32bit-64bit

鉴于Windows进程句柄,如何使用C ++代码确定进程是32位还是64位?

3 个答案:

答案 0 :(得分:38)

如果您有进程句柄,请使用IsWow64Process()

如果IsWow64Process()报告为true,则该进程在64位操作系统上运行32位。

如果IsWow64Process()报告错误(或kernel32.dll中不存在),则该过程在32位操作系统上运行32位,或者在64位运行时运行64位位OS。要了解操作系统本身是32位还是64位,请使用GetNativeSystemInfo()(如果GetNativeSystemInfo()中没有kernel32.dll,则使用GetSystemInfo()

答案 1 :(得分:0)

如果你有模块的处理,那么你可以这样做:

IMAGE_NT_HEADERS * headers = ImageNtHeader(handle);

if ( headers->FileHeader.Machine == IMAGE_FILE_MACHINE_I386 )
{
    //module is x86
}
else if  ( headers->FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64 )
{
    //module is x64
}

我从my own answer获得了帮助。

答案 2 :(得分:0)

BOOL IsWow64(HANDLE process)
{
    BOOL bIsWow64 = FALSE;

    typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
    LPFN_ISWOW64PROCESS fnIsWow64Process;
    fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");

    if (NULL != fnIsWow64Process)
    {
        if (!fnIsWow64Process(process, &bIsWow64))
        {
            //handle error
        }
    }
    return bIsWow64;
}

bool IsX86Process(HANDLE process)
{
    SYSTEM_INFO systemInfo = { 0 };
    GetNativeSystemInfo(&systemInfo);

    // x86 environment
    if (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
        return true;

    // Check if the process is an x86 process that is running on x64 environment.
    // IsWow64 returns true if the process is an x86 process
    return IsWow64(process);
}