123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- // 必须与系统的宏相同,否则使用VS2高版本时会出现重命名;
- #ifndef _versionhelpers_H_INCLUDED_
- #define _versionhelpers_H_INCLUDED_
- #ifdef _MSC_VER
- #pragma once
- #endif // _MSC_VER
- #pragma region Application Family
- #if (NTDDI_VERSION >= NTDDI_WINXP)
- #ifdef __cplusplus
- #define VERSIONHELPERAPI inline bool
- #else // __cplusplus
- #define VERSIONHELPERAPI FORCEINLINE BOOL
- #endif // __cplusplus
- #define _WIN32_WINNT_NT4 0x0400
- #define _WIN32_WINNT_WIN2K 0x0500 // windows 2000
- #define _WIN32_WINNT_WINXP 0x0501 // windows xp
- #define _WIN32_WINNT_WS03 0x0502 // widnows xp sp3
- #define _WIN32_WINNT_WIN6 0x0600 //
- #define _WIN32_WINNT_VISTA 0x0600 // windows vista
- #define _WIN32_WINNT_WS08 0x0600 // windows server 2008
- #define _WIN32_WINNT_LONGHORN 0x0600 //
- #define _WIN32_WINNT_WIN7 0x0601 // windows 7
- #define _WIN32_WINNT_WIN8 0x0602 // windows 8
- #define _WIN32_WINNT_WINBLUE 0x0603 //
- #define _WIN32_WINNT_WINTHRESHOLD 0x0A00 /* ABRACADABRA_THRESHOLD*/
- #define _WIN32_WINNT_WIN10 0x0A00 /* ABRACADABRA_THRESHOLD*/
- VERSIONHELPERAPI IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
- {
- OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0 };
- DWORDLONG const dwlConditionMask = VerSetConditionMask(
- VerSetConditionMask(
- VerSetConditionMask(
- 0, VER_MAJORVERSION, VER_GREATER_EQUAL),
- VER_MINORVERSION, VER_GREATER_EQUAL),
- VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
- osvi.dwMajorVersion = wMajorVersion;
- osvi.dwMinorVersion = wMinorVersion;
- osvi.wServicePackMajor = wServicePackMajor;
- // 注意:VerifyVersionInfoW只适用于Win 8以下操作系统。Win8以上该函数无效,需要加上特殊清单文件;
- return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
- }
- VERSIONHELPERAPI IsWindowsXPOrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 0);
- }
- VERSIONHELPERAPI IsWindowsXPSP1OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 1);
- }
- VERSIONHELPERAPI IsWindowsXPSP2OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 2);
- }
- VERSIONHELPERAPI IsWindowsXPSP3OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP), 3);
- }
- VERSIONHELPERAPI IsWindowsVistaOrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
- }
- VERSIONHELPERAPI IsWindowsVistaSP1OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 1);
- }
- VERSIONHELPERAPI IsWindowsVistaSP2OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 2);
- }
- VERSIONHELPERAPI IsWindows7OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 0);
- }
- VERSIONHELPERAPI IsWindows7SP1OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7), 1);
- }
- VERSIONHELPERAPI IsWindows8OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8), 0);
- }
- VERSIONHELPERAPI IsWindows8Point1OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINBLUE), LOBYTE(_WIN32_WINNT_WINBLUE), 0);
- }
- VERSIONHELPERAPI IsWindowsThresholdOrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINTHRESHOLD), LOBYTE(_WIN32_WINNT_WINTHRESHOLD), 0);
- }
- VERSIONHELPERAPI IsWindows10OrGreater()
- {
- return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_WINTHRESHOLD), LOBYTE(_WIN32_WINNT_WINTHRESHOLD), 0);
- }
- VERSIONHELPERAPI IsWindowsServer()
- {
- OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0, 0, VER_NT_WORKSTATION };
- DWORDLONG const dwlConditionMask = VerSetConditionMask( 0, VER_PRODUCT_TYPE, VER_EQUAL );
- return !VerifyVersionInfoW(&osvi, VER_PRODUCT_TYPE, dwlConditionMask);
- }
- /************************************************************************/
- /* 函数:[9/13/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- VERSIONHELPERAPI IsWow64()
- {
- typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
- LPFN_ISWOW64PROCESS fnIsWow64Process;
- BOOL bIsWow64 = FALSE;
- fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
- if (NULL != fnIsWow64Process)
- {
- if (!fnIsWow64Process(GetCurrentProcess(), &bIsWow64))
- {
- // 句柄错误,则系统不是64位;
- //AfxMessageBox(_T("句柄错误"));
- }
- }
- return bIsWow64;
- }
- /************************************************************************/
- /* 函数:[9/13/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- VERSIONHELPERAPI IsOSVerIs64()
- {
- typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
- typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
- BOOL bOSVerIs64 = FALSE;
- SYSTEM_INFO si;
- PGNSI pGNSI;
- pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
- if (NULL != pGNSI)
- pGNSI(&si);
- else
- GetSystemInfo(&si);
- if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||
- si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
- {
- bOSVerIs64 = TRUE;
- }
- return bOSVerIs64;
- }
- /************************************************************************/
- /* 函数:[9/13/2016 IT];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- VERSIONHELPERAPI IsWow64Process(HANDLE hprocess)
- {
- typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
- LPFN_ISWOW64PROCESS fnIsWow64Process;
- INT nRet = 0;
- BOOL bIsWow64 = FALSE;
- fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
- if (NULL != fnIsWow64Process)
- {
- if (!fnIsWow64Process(hprocess, &bIsWow64))
- {
- // 句柄错误;
- //AfxMessageBox(_T("句柄错误"));
- //DWORD dwError = GetLastError();
- return false;
- }
- }
- return bIsWow64;
- }
- /************************************************************************/
- /* 函数:IsWinVerEqualTo[5/3/2017 Jeff];
- /* 描述:判断是否=某个特定的系统版本;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- VERSIONHELPERAPI IsWinVerEqualTo(DWORD dwMajorVersion, DWORD dwMinorVersion)
- {
- OSVERSIONINFOEXW osvi = {0};
- DWORDLONG dwlConditionMask = 0;
- // 1、初始化系统版本信息数据结构
- ZeroMemory(&osvi, sizeof(osvi));
- osvi.dwOSVersionInfoSize= sizeof(osvi);
- osvi.dwMajorVersion= dwMajorVersion;
- osvi.dwMinorVersion= dwMinorVersion;
- // 2、初始化条件掩码
- VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
- VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);
- return ::VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask);
- }
- /************************************************************************/
- /* 函数:GetNtVersionNumbers[5/3/2017 Jeff];
- /* 描述:调用RtlGetNtVersionNumbers获取系统版本信息;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- VERSIONHELPERAPI GetNtVersionNumbers(DWORD dwMajorVer, DWORD dwMinorVer, DWORD dwBuildNumber)
- {
- BOOL bRet= FALSE;
- HMODULE hModNtdll= NULL;
- if (hModNtdll= ::LoadLibrary(_T("ntdll.dll")))
- {
- typedef void (WINAPI *pfRTLGETNTVERSIONNUMBERS)(DWORD*,DWORD*, DWORD*);
- pfRTLGETNTVERSIONNUMBERS pfRtlGetNtVersionNumbers;
- pfRtlGetNtVersionNumbers = (pfRTLGETNTVERSIONNUMBERS)::GetProcAddress(hModNtdll, "RtlGetNtVersionNumbers");
- if (pfRtlGetNtVersionNumbers)
- {
- pfRtlGetNtVersionNumbers(&dwMajorVer, &dwMinorVer,&dwBuildNumber);
- dwBuildNumber&= 0x0ffff;
- bRet = TRUE;
- }
- ::FreeLibrary(hModNtdll);
- hModNtdll = NULL;
- }
- return bRet;
- }
- void test()
- {
- TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
- _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
- IsWindowsXPOrGreater() ? _tprintf(_T("是IsWindowsXPOrGreater\n")) : _tprintf(_T("不是IsWindowsXPOrGreater\n"));
- IsWindowsXPSP1OrGreater() ? _tprintf(_T("是IsWindowsXPSP1OrGreater\n")) : _tprintf(_T("不是IsWindowsXPSP1OrGreater\n"));
- IsWindowsXPSP2OrGreater() ? _tprintf(_T("是IsWindowsXPSP2OrGreater\n")) : _tprintf(_T("不是IsWindowsXPSP2OrGreater\n"));
- IsWindowsXPSP3OrGreater() ? _tprintf(_T("是IsWindowsXPSP3OrGreater\n")) : _tprintf(_T("不是IsWindowsXPSP3OrGreater\n"));
- IsWindowsVistaOrGreater() ? _tprintf(_T("是IsWindowsVistaOrGreater\n")) : _tprintf(_T("不是IsWindowsVistaOrGreater\n"));
- IsWindowsVistaSP1OrGreater() ? _tprintf(_T("是IsWindowsVistaSP1OrGreater\n")) : _tprintf(_T("不是IsWindowsVistaSP1OrGreater\n"));
- IsWindowsVistaSP2OrGreater() ? _tprintf(_T("是IsWindowsVistaSP2OrGreater\n")) : _tprintf(_T("不是IsWindowsVistaSP2OrGreater\n"));
- IsWindows7OrGreater() ? _tprintf(_T("是IsWindows7OrGreater\n")) : _tprintf(_T("不是IsWindows7OrGreater\n"));
- IsWindows7SP1OrGreater() ? _tprintf(_T("是IsWindows7SP1OrGreater\n")) : _tprintf(_T("不是IsWindows7SP1OrGreater\n"));
- IsWindows8OrGreater() ? _tprintf(_T("是IsWindows8OrGreater\n")) : _tprintf(_T("不是IsWindows8OrGreater\n"));
- IsWindows8Point1OrGreater() ? _tprintf(_T("是IsWindows8Point1OrGreater\n")) : _tprintf(_T("不是IsWindows8Point1OrGreater\n"));
- IsWindowsThresholdOrGreater() ? _tprintf(_T("是IsWindowsThresholdOrGreater\n")) : _tprintf(_T("不是IsWindowsThresholdOrGreater\n"));
- IsWindows10OrGreater() ? _tprintf(_T("是IsWindows10OrGreater\n")) : _tprintf(_T("不是IsWindows10OrGreater\n"));
- IsWindowsServer() ? _tprintf(_T("是IsWindowsServer\n")) : _tprintf(_T("不是IsWindowsServer\n"));
- IsWinVerEqualTo(10, 1) ? _tprintf(_T("是IsWinVerEqualTo\n")) : _tprintf(_T("不是IsWinVerEqualTo\n"));
- GetNtVersionNumbers(10, 0, 14393) ? _tprintf(_T("是GetNtVersionNumbers\n")) : _tprintf(_T("不是GetNtVersionNumbers\n"));
- if (IsWinVerEqualTo(5, 0))
- _tprintf(_T("是 Windows 2000\n"));
- if ( IsWinVerEqualTo(5, 1) )
- _tprintf(_T("是 Windows XP\n"));
- if ( IsWinVerEqualTo(5, 2) )
- {
- IsWindowsServer() ? _tprintf(_T("是 Windows Server 2003 或 Windows Server 2003 R2\n")) : _tprintf(_T("是 Windows XP 64-Bit Edition\n"));
- }
- if ( IsWinVerEqualTo(6, 0) )
- {
- IsWindowsServer() ? _tprintf(_T("是 Windows Server 2008\n")) : _tprintf(_T("是 Windows Vista\n"));
- }
- if ( IsWinVerEqualTo(6, 1) )
- {
- IsWindowsServer() ? _tprintf(_T("是 Windows Server 2008 R2\n")) : _tprintf(_T("是 Windows 7\n"));
- }
- if ( IsWinVerEqualTo(6, 2) )
- {
- IsWindowsServer() ? _tprintf(_T("是 Windows Server 2012\n")) : _tprintf(_T("是 Windows 8\n"));
- }
- if ( IsWinVerEqualTo(6, 3) )
- {
- IsWindowsServer() ? _tprintf(_T("是 Windows Server 2012 R2\n")) : _tprintf(_T("是 Windows 8.1\n"));
- }
- if ( IsWinVerEqualTo(10, 0) )
- {
- IsWindowsServer() ? _tprintf(_T("是 Windows Server 2016\n")) : _tprintf(_T("是 Windows 10\n"));
- }
- IsWow64() ? _tprintf(_T("函数:IsWow64验证过程:是64位\n")) : _tprintf(_T("函数:IsWow64验证过程:是32位\n"));
- IsOSVerIs64() ? _tprintf(_T("函数:IsOSVerIs64验证过程:是64位\n")) : _tprintf(_T("函数:IsOSVerIs64验证过程:是32位\n"));
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- }
- #endif // NTDDI_VERSION
- #pragma endregion
- #endif // _VERSIONHELPERS_H_INCLUDED_
|