1. 程式人生 > >windows 獲取作業系統版本資訊

windows 獲取作業系統版本資訊

做windows 程式開發 常常使用關於OS的資訊的函式

相關的MSDN的links:

http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724834(v=vs.85).aspx

http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724381(v=vs.85).aspx

http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724429(v=vs.85).aspx

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

#pragma
comment(lib, "User32.lib") #define BUFSIZE 256 typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO); typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD); BOOL GetOSDisplayString( LPTSTR pszOS) { 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 != NULL ) return 1; // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
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, BUFSIZE, TEXT("Microsoft ")); // Test for the specific product. if ( osvi.dwMajorVersion == 6 ) { if( osvi.dwMinorVersion == 0 ) { if( osvi.wProductType == VER_NT_WORKSTATION ) StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista ")); else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 " )); } if ( osvi.dwMinorVersion == 1 ) { if( osvi.wProductType == VER_NT_WORKSTATION ) StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 ")); else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 " )); } pGPI = (PGPI) GetProcAddress( GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo"); pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType); switch( dwType ) { case PRODUCT_ULTIMATE: StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition" )); break; case PRODUCT_PROFESSIONAL: StringCchCat(pszOS, BUFSIZE, TEXT("Professional" )); break; case PRODUCT_HOME_PREMIUM: StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition" )); break; case PRODUCT_HOME_BASIC: StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition" )); break; case PRODUCT_ENTERPRISE: StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" )); break; case PRODUCT_BUSINESS: StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition" )); break; case PRODUCT_STARTER: StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition" )); break; case PRODUCT_CLUSTER_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition" )); break; case PRODUCT_DATACENTER_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition" )); break; case PRODUCT_DATACENTER_SERVER_CORE: StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)" )); break; case PRODUCT_ENTERPRISE_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" )); break; case PRODUCT_ENTERPRISE_SERVER_CORE: StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)" )); break; case PRODUCT_ENTERPRISE_SERVER_IA64: StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems" )); break; case PRODUCT_SMALLBUSINESS_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server" )); break; case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM: StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition" )); break; case PRODUCT_STANDARD_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition" )); break; case PRODUCT_STANDARD_SERVER_CORE: StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)" )); break; case PRODUCT_WEB_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition" )); break; } } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) { if( GetSystemMetrics(SM_SERVERR2) ) StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Server 2003 R2, ")); else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Storage Server 2003")); else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Home Server")); else if( osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) { StringCchCat(pszOS, BUFSIZE, TEXT( "Windows XP Professional x64 Edition")); } else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, ")); // Test for the server type. if ( osvi.wProductType != VER_NT_WORKSTATION ) { if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition for Itanium-based Systems" )); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition for Itanium-based Systems" )); } else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 ) { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter x64 Edition" )); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise x64 Edition" )); else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard x64 Edition" )); } else { if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Compute Cluster Edition" )); else if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition" )); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition" )); else if ( osvi.wSuiteMask & VER_SUITE_BLADE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Web Edition" )); else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard Edition" )); } } } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) { StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP ")); if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) StringCchCat(pszOS, BUFSIZE, TEXT( "Home Edition" )); else StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" )); } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) { StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 ")); if ( osvi.wProductType == VER_NT_WORKSTATION ) { StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" )); } else { if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Server" )); else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) StringCchCat(pszOS, BUFSIZE, TEXT( "Advanced Server" )); else StringCchCat(pszOS, BUFSIZE, TEXT( "Server" )); } } // Include service pack (if any) and build number. if( _tcslen(osvi.szCSDVersion) > 0 ) { StringCchCat(pszOS, BUFSIZE, TEXT(" ") ); StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion); } TCHAR buf[80]; StringCchPrintf( buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber); StringCchCat(pszOS, BUFSIZE, buf); if ( osvi.dwMajorVersion >= 6 ) { if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 ) StringCchCat(pszOS, BUFSIZE, TEXT( ", 64-bit" )); else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL ) StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit")); } return TRUE; } else { printf( "This sample does not support this version of Windows.\n"); return FALSE; } } int __cdecl _tmain() { TCHAR szOS[BUFSIZE]; if( GetOSDisplayString( szOS ) ) _tprintf( TEXT("\n%s\n"), szOS ); }

相關推薦

windows 獲取作業系統版本資訊

做windows 程式開發 常常使用關於OS的資訊的函式 相關的MSDN的links: http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724834(v=vs.85).aspx http://msdn.m

批處理命令之精確獲取作業系統版本資訊

                作者:朱金燦為了使得批處理命令獲取更大的通用性,有時需要獲取作業系統版本。今天為此搜了一些資料,網上的說法是Ver、登錄檔、WMI都可以。我研究了一下,發現還是Ver命令最為成熟,為此綜合各家做法寫下了下面一個批處理檔案:@echo offclsver | find "4.0.

獲取 Windows 作業系統版本資訊

// OSinfo.cpp : 定義控制檯應用程式的入口點。#include "stdafx.h"#include <windows.h>#include <stdio.h>#include "winsock.h"#include <string

Js獲取作業系統版本 && 獲得瀏覽器版本

//利用原生Js獲取作業系統版本function getOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windo

QT獲取作業系統版本

static QString GetSysVersion() {     DWORD dwVersion = 0;     WKSTA_INFO_100 *wkstaInfo = NULL;     NET_API_STATUS netStatus = NetWkstaGe

獲取應用版本資訊AppUtils

public class AppUtils { private AppUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be in

linux centos 如何檢視作業系統版本資訊

本文介紹常用的四種檢視linux下檢視系統版本資訊的方法:一、uname -a[[email protected]_11_211_centos ~]$ uname -a Linux VM_11_211_centos 2.6.32-504.30.3.el6.x86_6

WindowsAPI使用詳解——GetVersion|GetVersionEx 獲取作業系統版本和名稱

Windows API 中有兩個函式可以得到系統版本資訊:GetVersion和GetVersionEx。       GetVersion這個函式曾經困擾了很多程式設計師,其本來設計的是在DWORD返回值中用低位的那個字表示MS-DOS的版本號,高位的那個字表示Windows版本號。對於每個字來說,

批處理獲取系統版本資訊

@echo off ::從系統檔案中獲取系統版本資訊 for /f "tokens=1* delims=[" %%a in ('ver') do set b=%%b ::將版本資訊賦值給變數b set b=%b:* =% ::呼叫指定值對應的cmd命令列 call:%b:~

關於獲取作業系統版本的方法

獲取本地執行程式的機器版本的方法如下: public  static void main(String args[]){ String sysName = System.getProperty("os.name"); System.out.println("sysName:

利用WinAPI(GetFileVersionInfo)獲取檔案版本資訊

1)檔案版本資訊在Windows內部,按照以下格式儲存:例如:2.10.0.256->dwFileVersionMS = 0x0002000A 高4位元組 = 2 = 0x0002; 低4位元組 = 10 = 0x000AdwFileVersionLS = 0x0000

C++ 通過登錄檔獲取Windows版本資訊

原理:通過訪問登錄檔 HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion 下的鍵值資訊來獲取windows版本資訊。由於是訪問登錄檔獲取的資訊,所以準確性一般。 #include <

獲取Windows 版本資訊 預備以後也用

bool IsWinSysInfo() { SYSTEM_INFO info; GetSystemInfo(&info); OSVERSIONINFOEX os; os.dwOSVersionInfoSize = siz

C/C++獲取作業系統、CPU、記憶體資訊、硬碟、IP和MAC(windows和linux)

有時候需要在工程裡面獲取一些系統或者硬體的資訊,比如系統版本,cpu,記憶體,顯示卡,硬碟,網路等,作為後續軟體功能判斷的依據,甚至參與效能演算法自適應建模 Windows 作業系統和記憶體資訊在windows下通過系統的API來獲取,CPU資訊則需要需要通過底層CPUI

使用Windows API實現執行時動態獲取程式檔案版本資訊

檔案版本資訊的存在使得應用程式正確的安裝檔案變得簡單,並且使安裝程式能夠分析檔案的當前安裝狀態。通常,版本資訊應該包括檔案的版本號、檔案功能描述以及檔案作者等多項重要內容。   在應用層面上,程式設計師可以通過儲存在應用程式檔案或動態連結庫檔案中的版本資訊判斷一個檔案是否

獲取Windows作業系統版本和位數

(1)獲取系統位數32bit 64bit void CXXXDlg::GetSysBit(bool& bIs32Bit) { bIs32Bit=true; SYSTEM_INFO si;

獲取windows版本資訊的做法

作者:朱金燦        個人不建議用 GetVersion 或GetVersionEx 之類的 API 來獲取系統版本號。注意微軟也說過,這個結果會受到相容模式的影響,即如果程式設定相容模式為 XP 那麼拿到的也可能是 5.1 的。所以要準確的獲取版本號,我建議直接獲取

獲取windows操作系統版本

blog system oca char bool 操作系統 argv type ros 1 #include <windows> 2 int main(int argc, char* argv[]) 3 { 4 HKEY hKey; 5

獲取Windows操作系統版本名稱

Windows版本名稱HKEY hKey; DWORD dwType = REG_SZ; DWORD dwSize = 255;//須指定長度,否則第一次讀取RegQueryValueEx不成功,僅返回長度 TCHAR data[MAX_PATH]; bool ret; if (RegOpenKey(HK

獲取某個版本軟體存在的漏洞資訊

  之前介紹了:如何檢視某個特定版本軟體具有哪些漏洞   現在我們需要將這些漏洞資訊爬取到本地,並以一定的形式展示出來,這裡就是離線HTML格式。   這裡簡單介紹下使用方法,原始碼會放在Github:   Step 1: 查詢     我們在NVD查詢:mysql 5.7.21   Step