欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C/C++ 獲取Windows系統(tǒng)的位數32位或64位的實現代碼

 更新時間:2017年10月27日 08:52:48   作者:infoworld  
這篇文章主要介紹了C/C++ 獲取Windows系統(tǒng)的位數32位或64位的實現代碼的相關資料,希望通過本文能幫助到大家,讓大家實現這樣的功能,需要的朋友可以參考下

C/C++ 獲取Windows系統(tǒng)的位數32位或64位的實現代碼

場景

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

說明

1.通過判斷程序是32bit或64bit并沒有什么用,因為64bit系統(tǒng)可以運行32bit和64bit程序.

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

例子

// test-OSBit.cpp : 定義控制臺應用程序的入口點。
//

#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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • C++設計類不能被繼承的方法實例講解

    C++設計類不能被繼承的方法實例講解

    在Java 中定義了關鍵字final,被final修飾的類不能被繼承,C++中如何實現,下面我們來看一個例子
    2013-12-12
  • C++ 實現哈希表的實例

    C++ 實現哈希表的實例

    這篇文章主要介紹了C++ 實現哈希表的實例的相關資料,這里使用C++實現哈希表的實例幫助大家徹底理解哈希表的原理,需要的朋友可以參考下
    2017-08-08
  • C++淺析內存分區(qū)模型概念與示例

    C++淺析內存分區(qū)模型概念與示例

    在了解內存分區(qū)之前,我們先來聊一聊為什么要進行內存分區(qū)。在進行了內存分區(qū)之后,在不同的區(qū)域存放的數據,會有不同的生命周期,從而會讓程序員的編程變得更加靈活
    2022-09-09
  • 如何用C寫一個web服務器之GCC項目編譯

    如何用C寫一個web服務器之GCC項目編譯

    本文主要介紹了,如何用C寫一個web服務器,Linux下用GCC進行項目編譯,對此感興趣的同學,可以參考下。
    2021-05-05
  • Qt控件點擊消息獲取的方法詳解

    Qt控件點擊消息獲取的方法詳解

    本文將利用Qt中的QLabel、QPushButton這兩個控件,為大家詳細介紹一下Qt控件點擊消息獲取的方法,文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-06-06
  • C++模板Template詳解及其作用介紹

    C++模板Template詳解及其作用介紹

    這篇文章主要介紹了C++中模板(Template)的詳解及其作用介紹,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C++利用鏈表寫一個簡單的棧實例詳解

    C++利用鏈表寫一個簡單的棧實例詳解

    這篇文章主要介紹了C++利用鏈表寫一個簡單的棧實例詳解的相關資料,需要的朋友可以參考下
    2017-05-05
  • C/C++內存管理基礎與面試

    C/C++內存管理基礎與面試

    本章主要介紹C語言與C++的內存管理,以C++的內存分布作為引入,介紹C++不同于C語言的內存管理方式(new?delete對比?malloc?free),感興趣的朋友來看看吧
    2022-07-07
  • C語言實現修改文本文件中特定行的實現代碼

    C語言實現修改文本文件中特定行的實現代碼

    最近由于項目需要實現修改文件的功能,所以,博主認真查閱了一些資料,但是,很遺憾,并沒有太多的收獲
    2013-06-06
  • 基于條件變量的消息隊列 說明介紹

    基于條件變量的消息隊列 說明介紹

    本篇文章小編為大家介紹,基于條件變量的消息隊列 說明介紹。需要的朋友參考一下
    2013-04-04

最新評論