2014-05-16 33 views
3

如何檢測C + +是Windows 32或64位? 我在.Net中看到很多例子,但我需要C++。另外,IsWow64Process()對我來說不適用,因爲「如果進程在32位Windows下運行,則該值設置爲FALSE。如果進程是在64位Windows下運行的64位應用程序,則值爲也設置爲FALSE」如何檢測C + +是Windows 32或64位?

,如果我有32位PROC在32位操作系統我有假 如果我有64位PROC在64位操作系統我有假

,但我不關心過程位,我需要OS bit

回答

13

檢測底層系統信息的Win32 API函數是GetNativeSystemInfo。調用該函數並閱讀該函數填充的SYSTEM_INFO結構的wProcessorArchitecture成員。

雖然它實際上可以使用IsWow64Process來檢測此。如果您致電IsWow64Process並返回TRUE,那麼您知道您正在64位系統上運行。否則,返回FALSE。然後你只需要測試一個指針的大小,例如。 32位指針指示32位系統,而64位指針指示64位系統。實際上,根據所使用的編譯器,您可能會從編譯器提供的條件中獲取信息,因爲指針的大小在編譯時是已知的。

Raymond Chen在blog article中描述了這種方法。他幫忙,包括我這裏重現代碼:

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 
} 
+0

爲什麼過去的「&& f64」?看起來編譯器可以優化整行以「返回false」,因爲「A && false」是錯誤的。 – vz0

+1

@ vz0不,調用'IsWow64Process()'修改'f64' –

+1

@ vz0:'IsWow64Process()'返回TRUE/FALSE來指示'BOOL'變量是否被更新。如果爲TRUE,則該變量將指示請求的進程是否是WOW64進程(32位在64位上運行)。這就是爲什麼需要額外的檢查:'返回((查詢成功)&&(查詢結果爲真));' –

0

在Windows,C++,您可以使用此代碼 - 它確定過程是在

運行Windows哪種類型的我已經使用這個正確的窗口XP中,Windows 7 64 & 32位,窗8 64 & 32,窗10

(對不起,我英文不好)

#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; 
} 
相關問題