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语言
本文标题:C/C++ 获取Windows系统的位数32位或64位的实现代码
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/1079.html
您可能感兴趣的文章
- 04-02c语言没有round函数 round c语言
- 01-10深入理解C++中常见的关键字含义
- 01-10使用C++实现全排列算法的方法详解
- 01-10c++中inline的用法分析
- 01-10用C++实现DBSCAN聚类算法
- 01-10全排列算法的非递归实现与递归实现的方法(C++)
- 01-10C++大数模板(推荐)
- 01-10浅谈C/C++中的static与extern关键字的使用详解
- 01-10深入C/C++浮点数在内存中的存储方式详解
- 01-10Linux C 获取进程退出值的实现代码
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 04-02jquery与jsp,用jquery
- 01-10SublimeText编译C开发环境设置
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结