C/C++獲取Windows系統(tǒng)的位數(shù)32位或64位的實(shí)現(xiàn)代碼-創(chuàng)新互聯(lián)

C/C++ 獲取Windows系統(tǒng)的位數(shù)32位或64位的實(shí)現(xiàn)代碼

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比淮陽(yáng)網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式淮陽(yáng)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋淮陽(yáng)地區(qū)。費(fèi)用合理售后完善,10年實(shí)體公司更值得信賴。

場(chǎng)景

1.在Windows 64bit系統(tǒng)開發(fā)程序時(shí), 某些情況需要判斷Program Files路徑, 但是64bit系統(tǒng)有兩個(gè)Program Files或 Program Files(x86), 這時(shí)候就需要根據(jù)當(dāng)前系統(tǒng)的位數(shù)來(lái)獲取路徑了.

說(shuō)明

1.通過(guò)判斷程序是32bit或64bit并沒(méi)有什么用,因?yàn)?4bit系統(tǒng)可以運(yùn)行32bit和64bit程序.

2.64bit系統(tǒng)的kernel32.dll 里有一個(gè)函數(shù)接口 IsWow64Process,只需要判斷這個(gè)dll是否有這個(gè)導(dǎo)出函數(shù)即可.

例子

// test-OSBit.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//

#include "stdafx.h"
#include <Windows.h>
#include "Shlobj.h"
#include <iostream>


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

static LPFN_ISWOW64PROCESS fnIsWow64Process = NULL;

//
//  FUNCTION: SafeIsWow64Process(HANDLE, PBOOL)
//
//  PURPOSE: This is a wrapper of the IsWow64Process API. It determines 
//  whether the specified process is running under WOW64. IsWow64Process 
//  does not exist prior to Windows XP with SP2 and Window Server 2003 with 
//  SP1. For compatibility with operating systems that do not support 
//  IsWow64Process, call GetProcAddress to detect whether IsWow64Process is 
/// implemented in Kernel32.dll. If GetProcAddress succeeds, it is safe to 
//  call IsWow64Process dynamically. Otherwise, WOW64 is not present.
//
//  PARAMETERS:
//  * hProcess - A handle to the process. 
//  * Wow64Process - A pointer to a value that is set to TRUE if the process 
//   is running under WOW64. 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.
//
//  RETURN VALUE: If the function succeeds, the return value is TRUE.If 
//  IsWow64Process does not exist in kernel32.dll, or the function fails, 
//  the return value is FALSE. 
//
static BOOL WINAPI SafeIsWow64Process(HANDLE hProcess, PBOOL Wow64Process)
{
  if (fnIsWow64Process == NULL)
  {
    // 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.
    HMODULE hModule = GetModuleHandle(L"kernel32.dll");
    if (hModule == NULL)
    {
      return FALSE;
    }

    fnIsWow64Process = reinterpret_cast<LPFN_ISWOW64PROCESS>(
      GetProcAddress(hModule, "IsWow64Process"));
    if (fnIsWow64Process == NULL)
    {
      return FALSE;
    }
  }
  return fnIsWow64Process(hProcess, Wow64Process);
}

//
//  FUNCTION: Is64BitOS()
//
//  PURPOSE: The function determines whether the current operating system is 
//  a 64-bit operating system.
//
//  RETURN VALUE: The function returns TRUE if the operating system is 
//  64-bit; otherwise, it returns FALSE.
//
static BOOL Is64BitOS()
{
#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
  BOOL f64bitOS = FALSE;
  return (SafeIsWow64Process(GetCurrentProcess(), &f64bitOS) && f64bitOS);
#else
  return FALSE; // 64-bit Windows does not support Win16
#endif
}

int _tmain(int argc, _TCHAR* argv[])
{
  TCHAR folder[MAX_PATH] = {0};
  if(Is64BitOS())
    ::SHGetSpecialFolderPath(NULL,folder,CSIDL_PROGRAM_FILESX86,FALSE);
  else
    ::SHGetSpecialFolderPath(NULL,folder,CSIDL_PROGRAM_FILES,FALSE);
  std::wcout << "32bit Program Files: " << folder << std::endl;
  system("pause");
  return 0;
}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站題目:C/C++獲取Windows系統(tǒng)的位數(shù)32位或64位的實(shí)現(xiàn)代碼-創(chuàng)新互聯(lián)
本文URL:http://muchs.cn/article36/cosisg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、虛擬主機(jī)建站公司、域名注冊(cè)網(wǎng)站制作、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站