獲取作業系統的詳細資訊--程式碼實現
阿新 • • 發佈:2018-11-09
1、GetVersionExA的使用
OSVERSIONINFO 結構體獲取
標頭檔案:#include <windows.h>
OSVERSIONINFO osvi;
ZeroMemory(osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx((OSVERSIONINFO *)&osvi);
然後根據獲取的主版本,次版本
最後給出獲取OS的程式碼,沒有單獨試過是不是可以獲取win10版本資訊,修改後的確可以
BOOL ClassName::SetOperateSystemTypeAndCoreVersion() { TCHAR pszOS[256+1] = {0}; TCHAR pszOSCore[64] = {0}; UINT uBufSize = 256; OSVERSIONINFOEX osvi; SYSTEM_INFO si; PGNSI pGNSI; PGPI pGPI; BOOL bOsVersionInfoEx; DWORD dwType; ZeroMemory(&si, sizeof(SYSTEM_INFO)); ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi); if (! bOsVersionInfoEx) { return FALSE; } pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),"GetNativeSystemInfo"); if (NULL != pGNSI) { pGNSI(&si); } else { GetSystemInfo(&si); } if (VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && osvi.dwMajorVersion > 4) { StringCchCopy(pszOS, uBufSize, TEXT("Microsoft ")); if (osvi.dwMajorVersion >= 6) { // after Windows 8.1 or later, GetVersionEx function cannot get correct version... OSVERSIONINFOEXW ovi; ZeroMemory(&ovi, sizeof(OSVERSIONINFOEXW)); if (!GetVersionEx2((LPOSVERSIONINFOW)&ovi)) return FALSE; // GetVersionEx2/RtlGetVersion cannot get correct wProductType, so copy it to osvi osvi.dwMajorVersion = ovi.dwMajorVersion; osvi.dwMinorVersion = ovi.dwMinorVersion; osvi.dwBuildNumber = ovi.dwBuildNumber; //_tprintf(TEXT("RtlGetVersion=%d.%d.%d\n"), osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber); //output version info directly } LogPrint::PrintLogA("osvi.dwMajorVersion === %d",osvi.dwMajorVersion); LogPrint::PrintLogA("osvi.dwMinorVersion === :%d",osvi.dwMinorVersion); if (osvi.dwMajorVersion > 6) { if ( osvi.dwMajorVersion > 10 || osvi.dwMinorVersion > 0 ) { //Reserve for new OS StringCchCat(pszOS, uBufSize, TEXT("Windows 10 Later ")); } else { if( osvi.wProductType == VER_NT_WORKSTATION ) StringCchCat(pszOS, uBufSize, TEXT("Windows 10 ")); else StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2016 " )); } } else if (osvi.dwMajorVersion == 6) { StringCchCopy(pszOSCore,64,TEXT("6")); if (osvi.dwMinorVersion == 0) { StringCchCat(pszOSCore,64,TEXT(".0")); if(osvi.wProductType == VER_NT_WORKSTATION) { StringCchCat(pszOS, uBufSize, TEXT("Windows Vista ")); } else { StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2008 " )); } } else if (osvi.dwMinorVersion == 1 ) { StringCchCat(pszOSCore,64,TEXT(".1")); if (osvi.wProductType == VER_NT_WORKSTATION) { StringCchCat(pszOS, uBufSize, TEXT("Windows 7 ")); } else { StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2008 R2 " )); } } else if(osvi.dwMinorVersion == 2) { StringCchCat(pszOSCore,64,TEXT(".2")); if (osvi.wProductType == VER_NT_WORKSTATION) { StringCchCat(pszOS, uBufSize, TEXT("Windows 8 ")); } else { StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2012")); } } else if (osvi.dwMinorVersion == 3) { StringCchCat(pszOSCore,64,TEXT(".3")); if (osvi.wProductType == VER_NT_WORKSTATION ) { StringCchCat(pszOS, uBufSize, TEXT("Windows 8.1")); } else { StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2012 R2")); } } pGPI = (PGPI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo"); pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType); switch(dwType) { case PRODUCT_ULTIMATE: StringCchCat(pszOS, uBufSize, TEXT("Ultimate Edition")); break; case PRODUCT_PROFESSIONAL: StringCchCat(pszOS, uBufSize, TEXT("Professional")); break; case PRODUCT_HOME_PREMIUM: StringCchCat(pszOS, uBufSize, TEXT("Home Premium Edition")); break; case PRODUCT_HOME_BASIC: StringCchCat(pszOS, uBufSize, TEXT("Home Basic Edition")); break; case PRODUCT_ENTERPRISE: StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition")); break; case PRODUCT_BUSINESS: StringCchCat(pszOS, uBufSize, TEXT("Business Edition")); break; case PRODUCT_STARTER: StringCchCat(pszOS, uBufSize, TEXT("Starter Edition")); break; case PRODUCT_CLUSTER_SERVER: StringCchCat(pszOS, uBufSize, TEXT("Cluster Server Edition")); break; case PRODUCT_DATACENTER_SERVER: StringCchCat(pszOS, uBufSize, TEXT("Datacenter Edition")); break; case PRODUCT_DATACENTER_SERVER_CORE: StringCchCat(pszOS, uBufSize, TEXT("Datacenter Edition (core installation)")); break; case PRODUCT_ENTERPRISE_SERVER: StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition" )); break; case PRODUCT_ENTERPRISE_SERVER_CORE: StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition (core installation)")); break; case PRODUCT_ENTERPRISE_SERVER_IA64: StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition for Itanium-based Systems")); break; case PRODUCT_SMALLBUSINESS_SERVER: StringCchCat(pszOS, uBufSize, TEXT("Small Business Server")); break; case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM: StringCchCat(pszOS, uBufSize, TEXT("Small Business Server Premium Edition")); break; case PRODUCT_STANDARD_SERVER: StringCchCat(pszOS, uBufSize, TEXT("Standard Edition")); break; case PRODUCT_STANDARD_SERVER_CORE: StringCchCat(pszOS, uBufSize, TEXT("Standard Edition (core installation)")); break; case PRODUCT_WEB_SERVER: StringCchCat(pszOS, uBufSize, TEXT("Web Server Edition")); break; } } else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2) { StringCchCopy(pszOSCore,64,TEXT("5")); StringCchCat(pszOSCore,64,TEXT(".2")); if(GetSystemMetrics(SM_SERVERR2)) { StringCchCat(pszOS, uBufSize, TEXT( "Windows Server 2003 R2, ")); } else if (osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER) { StringCchCat(pszOS, uBufSize, TEXT( "Windows Storage Server 2003")); } else if (osvi.wSuiteMask & VER_SUITE_WH_SERVER) { StringCchCat(pszOS, uBufSize, TEXT( "Windows Home Server")); } else if(osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) { StringCchCat(pszOS, uBufSize, TEXT("Windows XP Professional x64 Edition")); } else { StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2003, ")); } if (osvi.wProductType != VER_NT_WORKSTATION) { if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64) { if(osvi.wSuiteMask & VER_SUITE_DATACENTER) { StringCchCat(pszOS, uBufSize, TEXT("Datacenter Edition for Itanium-based Systems")); } else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE) { StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition for Itanium-based Systems")); } } else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) { if(osvi.wSuiteMask & VER_SUITE_DATACENTER) { StringCchCat(pszOS, uBufSize, TEXT("Datacenter x64 Edition")); } else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) { StringCchCat(pszOS, uBufSize, TEXT("Enterprise x64 Edition")); } else { StringCchCat(pszOS, uBufSize, TEXT("Standard x64 Edition")); } } else { if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER) { StringCchCat(pszOS, uBufSize, TEXT("Compute Cluster Edition")); } else if(osvi.wSuiteMask & VER_SUITE_DATACENTER) { StringCchCat(pszOS, uBufSize, TEXT("Datacenter Edition")); } else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE) { StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition")); } else if (osvi.wSuiteMask & VER_SUITE_BLADE) { StringCchCat(pszOS, uBufSize, TEXT("Web Edition")); } else { StringCchCat(pszOS, uBufSize, TEXT("Standard Edition")); } } } } else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) { StringCchCopy(pszOSCore, 64, TEXT("5.1")); StringCchCat(pszOS, uBufSize, TEXT("Windows XP ")); if(osvi.wSuiteMask & VER_SUITE_PERSONAL) { StringCchCat(pszOS, uBufSize, TEXT("Home Edition")); } else { StringCchCat(pszOS, uBufSize, TEXT( "Professional")); } } else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) { StringCchCat(pszOS, uBufSize, TEXT("Windows 2000 ")); StringCchCopy(pszOSCore, 64, TEXT("5.0")); if ( osvi.wProductType == VER_NT_WORKSTATION ) { StringCchCat(pszOS, uBufSize, TEXT("Professional")); } else { if(osvi.wSuiteMask & VER_SUITE_DATACENTER) { StringCchCat(pszOS, uBufSize, TEXT("Datacenter Server")); } else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE) { StringCchCat(pszOS, uBufSize, TEXT("Advanced Server")); } else { StringCchCat(pszOS, uBufSize, TEXT("Server")); } } } if(_tcslen(osvi.szCSDVersion) > 0) { StringCchCat(pszOS, uBufSize, TEXT(" ")); StringCchCat(pszOS, uBufSize, osvi.szCSDVersion); } TCHAR bufCore[64]; StringCchPrintf(bufCore, 64, TEXT("%d"), osvi.dwBuildNumber); StringCchCat(pszOSCore, 64, TEXT(".")); StringCchCat(pszOSCore, 64, bufCore); m_strOStype =global::WCharToMultiChar(pszOS); m_strOSCoreType = global::WCharToMultiChar(pszOSCore); return TRUE; } else { return FALSE; } }