国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

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

2020-01-26 13:51:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

場(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.通過判斷程序是32bit或64bit并沒有什么用,因?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;}

輸出:

32bit Program Files: C:/Program Files (x86)

參考

SHGetSpecialFolderPath function

如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吴桥县| 青河县| 翁牛特旗| 德惠市| 新建县| 米林县| 美姑县| 兰溪市| 北川| 达拉特旗| 清镇市| 宜君县| 沅江市| 纳雍县| 鄂州市| 泸定县| 绵竹市| 青州市| 博野县| 武陟县| 开鲁县| 同心县| 民权县| 韩城市| 鄂温| 丰顺县| 红桥区| 高淳县| 阳信县| 梧州市| 湖北省| 招远市| 滕州市| 偏关县| 镇原县| 永嘉县| 郸城县| 广东省| 英吉沙县| 涟水县| 长治市|