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++中list的用法实例讲解

    C++中list的用法实例讲解

    list是顺序容器的一种,list是一个双向链表,使用list需要包含头文件list,这篇文章主要给大家介绍了关于C++中list的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2021-11-11
  • Qt连接数据库并实现数据库增删改查的图文教程

    Qt连接数据库并实现数据库增删改查的图文教程

    QT连接数据库是应用开发的常用基础操作,经过实验我总结了一些例程,下面这篇文章主要给大家介绍了关于Qt连接数据库并实现数据库增删改查的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-04-04
  • C/C++中关于字符串的常见函数操作大全

    C/C++中关于字符串的常见函数操作大全

    这篇文章主要介绍了C/C++中关于字符串的常见函数操作,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • C语言变长数组使用详解

    C语言变长数组使用详解

    这篇文章主要介绍了C语言变长数组使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • C++中String类型的逆序方式

    C++中String类型的逆序方式

    这篇文章主要介绍了C++中String类型的逆序方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • C语言带头双向循环链表的示例代码

    C语言带头双向循环链表的示例代码

    这篇文章主要介绍了如何利用C语言实现带头双向循环链表,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-11-11
  • C语言 字符串指针详解及示例代码

    C语言 字符串指针详解及示例代码

    本文主要介绍C语言 字符串指针,这里整理了详细资料,并附示例代码及实现结果,有兴趣的小伙伴可以参考下
    2016-08-08
  • C++之构造函数默认值设置方式

    C++之构造函数默认值设置方式

    这篇文章主要介绍了C++之构造函数默认值设置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • C/C++内存管理基础与面试

    C/C++内存管理基础与面试

    本章主要介绍C语言与C++的内存管理,以C++的内存分布作为引入,介绍C++不同于C语言的内存管理方式(new delete对比 malloc free),感兴趣的朋友来看看吧
    2022-07-07
  • C/C++如何获取当前系统时间的实例详解

    C/C++如何获取当前系统时间的实例详解

    这篇文章主要介绍了 C/C++如何获取当前系统时间的实例详解的相关资料,这里提供了几种实现方法,帮助大家实现这样的功能,需要的朋友可以参考下
    2017-08-08

最新评论