C/C++ 获取Windows系统的位数32位或64位的实现代码

 更新时间:2017年10月27日 08:52:48   作者:infoworld  
这篇文章主要介绍了C/C++ 获取Windows系统的位数32位或64位的实现代码的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下

C/C++ 获取Windows系统的位数32位或64位的实现代码

场景

1.在Windows 64bit系统开发程序时, 某些情况需要判断Program Files路径, 但是64bit系统有两个Program Files或 Program Files(x86), 这时候就需要根据当前系统的位数来获取路径了.

说明

1.通过判断程序是32bit或64bit并没有什么用,因为64bit系统可以运行32bit和64bit程序.

2.64bit系统的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

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:

相关文章

  • C++中函数模板的用法详细解析

    C++中函数模板的用法详细解析

    所谓函数模板实际上是建立一个通用函数,其涵涵素类型额形参类型不具体指定,用一个虚拟的类型来代表,这个通用函数就称为函数模板
    2013-10-10
  • C语言 程序的编译系统解析

    C语言 程序的编译系统解析

    编译程序的基本功能是把源程序(高级语言)翻译成目标程序。但是,作为一个具有实际应用价值的编译系统,除了基本功能之外,还应具备语法检查、调试措施、修改手段、覆盖处理、目标程序优化、不同语言合用以及人-机联系等重要功能
    2022-02-02
  • C语言实现任何文件的加密解密功能

    C语言实现任何文件的加密解密功能

    这篇文章主要为大家详细介绍了C语言实现任何文件的加密解密功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-03-03
  • C++中sprintf使用的方法与printf的区别分析

    C++中sprintf使用的方法与printf的区别分析

    这篇文章主要介绍了C++中sprintf使用的方法与printf的区别,实例分析了sprintf与printf的具体用法及相关注意事项,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • C语言实现三子棋游戏附注释

    C语言实现三子棋游戏附注释

    这篇文章主要为大家详细介绍了C语言实现三子棋游戏附注释,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • C++中的volatile关键字及其作用

    C++中的volatile关键字及其作用

    本文介绍了C++中的volatile关键字,它用于标识变量可能被意外修改,以及编译器不应进行优化。本文通过具体的代码示例,阐述了volatile关键字的作用和使用方法,帮助读者更好地了解该关键字在C++语言中的应用场景和实现原理
    2023-04-04
  • EasyC++全局变量

    EasyC++全局变量

    这篇文章主要介绍了EasyC++全局变量
    2021-12-12
  • C/C++实现词法分析程序的示例代码

    C/C++实现词法分析程序的示例代码

    这篇文章主要为大家详细介绍了如何基于C/C++实现一个简单的词法分析程序,并通过完成词法分析程序,了解词法分析的过程,感兴趣的小伙伴可以跟随小编一起学习学习
    2023-05-05
  • C++如何实现BitMap数据结构

    C++如何实现BitMap数据结构

    这篇文章主要介绍了C++如何实现BitMap数据结构,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • c++支持coroutine的简单示例

    c++支持coroutine的简单示例

    这篇文章主要介绍了c++支持coroutine的简单示例,使用的是linux 平台做的,需要的朋友可以参考下
    2014-03-03

最新评论