C/C++ 获取Windows系统的位数32位或64位的实现代码
创新互联公司-专业网站定制、快速模板网站建设、高性价比淮阳网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式淮阳网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖淮阳地区。费用合理售后完善,10年实体公司更值得信赖。场景
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; }
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站题目:C/C++获取Windows系统的位数32位或64位的实现代码-创新互联
本文URL:https://www.cdcxhl.com/article36/cosisg.html
成都网站建设公司_创新互联,为您提供关键词优化、虚拟主机、建站公司、域名注册、网站制作、企业建站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联