How to detect on C++ is windows 32 or 64 bit?

谁都会走 提交于 2020-12-02 23:36:51

问题


How to detect on C++ is windows 32 or 64 bit? I see a lot of examples in .Net but I need C++. Also IsWow64Process() dosen't works for me, becouse "If the process is running under 32-bit Windows, the value is set to FALSE. If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE"

if I have 32 bit proc under 32 bit OS I have FALSE if I have 64 bit proc under 64 bit OS I have FALSE

BUT I dont care about process bit I need OS bit


回答1:


The Win32 API function to detect information about the underlying system is GetNativeSystemInfo. Call the function and read the wProcessorArchitecture member of the SYSTEM_INFO struct that the function populates.

Although it is actually possible to use IsWow64Process to detect this. If you call IsWow64Process and TRUE is returned, then you know that you are running on a 64 bit system. Otherwise, FALSE is returned. And then you just need to test the size of a pointer, for instance. A 32 bit pointer indicates a 32 bit system, and a 64 bit pointer indicates a 64 bit system. In fact, you can probably get the information from a conditional supplied by the compiler, depending on which compiler you use, since the size of the pointer is known at compile time.

Raymond Chen described this approach in a blog article. He helpfully included code which I reproduce here:

BOOL Is64BitWindows()
{
#if defined(_WIN64)
 return TRUE;  // 64-bit programs run only on Win64
#elif defined(_WIN32)
 // 32-bit programs run on both 32-bit and 64-bit Windows
 // so must sniff
 BOOL f64 = FALSE;
 return IsWow64Process(GetCurrentProcess(), &f64) && f64;
#else
 return FALSE; // Win64 does not support Win16
#endif
}



回答2:


An alternative method is to check for the existence of the "Program Files (x86)" root folder on the system partition. It will be present on x64 windows installations, and not on x86 installations.




回答3:


on windows, C++, you can use this code - it determines which type of windows that process is running on

I have used this and correct on windows xp, windows 7 64 & 32 bit, windows 8 64 & 32, windows 10

(sorry for my poor english)

#include <windows.h>
#include <iostream>
#include <tchar.h>

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.

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

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

int main( void )
{
    if(IsWow64())
        printf(TEXT("The process is running under Windows 64.\n"));
    else
        printf(TEXT("The process is NOT running under Windows 64.\n"));

    return 0;
}


来源:https://stackoverflow.com/questions/23696749/how-to-detect-on-c-is-windows-32-or-64-bit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!