1. 程式人生 > >C++獲得當前windows系統的版本號的代碼

C++獲得當前windows系統的版本號的代碼

nor num codes eof win size amp sp2 sys

在開發過程中中,把開發過程中比較好的內容片段備份一次,下邊內容段是關於C++獲得當前windows系統的版本號的內容,應該能對碼農們也有好處。

#include "InitWindowsVersion.h"

      無
 WIN_VER_DETAIL GetWindowsVersion()
{
    UNICODE_STRING ustrFuncName = { 0 }; 
    RTL_OSVERSIONINFOEXW osverinfo = { sizeof(osverinfo) }; 
    PFN_RtlGetVersion pfnRtlGetVersion = NULL;

    if (WinVersion)
        return WinVersion;

    RtlInitUnicodeString(&ustrFuncName, L"RtlGetVersion"); 

    if (pfnRtlGetVersion)
    { 
        pfnRtlGetVersion((PRTL_OSVERSIONINFOW)&osverinfo); 
    } 
    else 
    {

        PsGetVersion(&osverinfo.dwMajorVersion, &osverinfo.dwMinorVersion, &osverinfo.dwBuildNumber, NULL);
    }

    if (osverinfo.dwMajorVersion == 5 && osverinfo.dwMinorVersion == 0) 
    {
        WinVersion = WINDOWS_VERSION_2K;
    } 
    else if (osverinfo.dwMajorVersion == 5 && osverinfo.dwMinorVersion == 1) 
    {
        WinVersion = WINDOWS_VERSION_XP;
    } 
    else if (osverinfo.dwMajorVersion == 5 && osverinfo.dwMinorVersion == 2) 
    {
        if (osverinfo.wServicePackMajor==0) 
        { 
            WinVersion = WINDOWS_VERSION_2K3;
        } 
        else 
        {
            WinVersion = WINDOWS_VERSION_2K3_SP1_SP2;
        }
    } 
    else if (osverinfo.dwMajorVersion == 6 && osverinfo.dwMinorVersion == 0) 
    {
        WinVersion = WINDOWS_VERSION_2K3_SP1_SP2;
    }
    else if (osverinfo.dwMajorVersion == 6 && osverinfo.dwMinorVersion == 1)
    {
        WinVersion = WINDOWS_VERSION_7;
    }

    return WinVersion;
}

InitWindowsVersion.h

#ifndef _SYSTEM_H
#define _SYSTEM_H

#include "ntifs.h"

typedef enum WIN_VER_DETAIL {
    WINDOWS_VERSION_2K,
    WINDOWS_VERSION_XP,
    WINDOWS_VERSION_2K3,
    WINDOWS_VERSION_2K3_SP1_SP2,
    WINDOWS_VERSION_VISTA_2008,
    WINDOWS_VERSION_7
} WIN_VER_DETAIL;

WIN_VER_DETAIL WinVersion;

 WIN_VER_DETAIL GetWindowsVersion();
#endif

C++獲得當前windows系統的版本號的代碼