GetOsVersion

Gets the Windows operating system version.

Archived content: This source code is currently inactive and may be outdated or no longer maintained or functional.

Nicht alle Windows-Versionen bieten den gleichen Funktionsumfang an, neuere können meist ein bisschen mehr. Entsprechend werden auch nicht alle API-Funktionen auf älteren Windows-Versionen unterstützt. Damit nichts schlimmes passiert, lässt sich z. B. die Windows-Version abfragen, anhand der dann weitere Entscheidungen getroffen werden können.

Eine CString-Implementierung ist ebenfalls verfügbar.

Informationen zum Betriebssystem in C#? Weiter zur MyEnvironment-Klasse…

Note on the code quality: The content on this page is possibly a bit outdated. I’m not using C++ anymore for quite some time, but I would like to keep the existing and once useful functions available.

Download

GetOsVersion.cpp3.4 KiBQuelltext der GetOsVersion-Funktion

Contents of the file GetOsVersion.cpp:

// Gets the Windows operating system version.
//
// Dependencies: CString (discardable)
// Status: OK, partially outdated, GetCPU not finished
//
// Web: http://unclassified.software/source/getosversion

// OS version defines
#define OSV_WIN32_BEGIN  1
#define OSV_WIN_BEGIN    2
#define OSV_WIN95        3   // Windows 95
#define OSV_WIN98        4   // Windows 98
#define OSV_WINME        5   // Windows Me
#define OSV_WIN_END      6
#define OSV_NT_BEGIN     7
#define OSV_NT3          8   // WinNT 3.x
#define OSV_NT4          9   // WinNT 4.0
#define OSV_2000         10  // Windows 2000
#define OSV_XP           11  // Windows XP
#define OSV_2003         12  // Windows Server 2003 or XP x64
#define OSV_VISTA        13  // Windows Vista
#define OSV_7            14  // Windows 7
#define OSV_8            15  // Windows 8
#define OSV_NT_END       16
#define OSV_WIN32_END    17

// CPU info defines
#define CPU_OTHER        0
#define CPU_386          1
#define CPU_486          2
#define CPU_P1           3
#define CPU_P2           4
#define CPU_P3           5
#define CPU_P4           6
#define CPU_ATHLON       7
#define CPU_ATHXP        8
// TODO: Add newer processors

int CSysInfo::GetOSVersion()
{
    OSVERSIONINFO ovi;

    ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&ovi);

    switch (ovi.dwPlatformId)
    {
        case VER_PLATFORM_WIN32_WINDOWS:
            switch (ovi.dwMajorVersion)
            {
                case 4:
                    switch (ovi.dwMinorVersion)
                    {
                        case 0: return OSV_WIN95;
                        case 10: return OSV_WIN98;
                        case 90: return OSV_WINME;
                    }
            }
        case VER_PLATFORM_WIN32_NT:
            switch (ovi.dwMajorVersion)
            {
                case 3: return OSV_NT3;
            }
            switch (ovi.dwMajorVersion)
            {
                case 4: return OSV_NT4;
            }
            switch (ovi.dwMajorVersion)
            {
                case 5:
                    switch (ovi.dwMinorVersion)
                    {
                        case 0: return OSV_2000;
                        case 1: return OSV_XP;
                        case 2: return OSV_2003;
                    }
                    break;
                case 6:
                    switch (ovi.dwMinorVersion)
                    {
                        case 0: return OSV_VISTA;
                        case 1: return OSV_7;
                        case 2: return OSV_8;
                    }
                    break;
            }
    }

    return -1;
}

CString GetOSSubVersion()
{
    OSVERSIONINFO ovi;
    CString str;

    ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&ovi);

    str = ovi.szCSDVersion;

    return str;
}

DWORD GetOSBuild()
{
    OSVERSIONINFO ovi;

    ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&ovi);

    switch (ovi.dwPlatformId)
    {
        case VER_PLATFORM_WIN32_WINDOWS:
            return ovi.dwBuildNumber & 0x0000FFFF;
        case VER_PLATFORM_WIN32_NT:
            return ovi.dwBuildNumber;
    }
    return 0;
}

CString GetUser()
{
    CString strUser;
    DWORD dw = 256;
    GetUserName(strUser.GetBuffer(dw), &dw);
    return strUser;
}

CString GetComputer()
{
    CString strComputer;
    DWORD dw = 256;
    GetComputerName(strComputer.GetBuffer(dw), &dw);
    return strComputer;
}

int GetScreenWidth()
{
    return GetSystemMetrics(SM_CXSCREEN);
}

int GetScreenHeight()
{
    return GetSystemMetrics(SM_CYSCREEN);
}

BOOL IsSafemode()
{
    return GetSystemMetrics(SM_CLEANBOOT) != 0;
}

int GetCPU()
{
    SYSTEM_INFO si;
    GetSystemInfo(&si);

    if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
    {
        switch (si.wProcessorLevel)
        {
            case 3: return CPU_386;
            case 4: return CPU_486;
            case 5: return CPU_P1;
            case 6:
                switch (si.wProcessorRevision)
                {
                    // TODO: get entire list of model/stepping values
                    case 0x0803: return CPU_P3;
                }
        }
    }
    return CPU_OTHER;
}

Licence and terms of use

This software is freely available as source code and compiled version, without restrictions (“public domain”). There is no warranty, not even for merchantability or fitness for a particular purpose. I am not liable for any damage caused through appropriate or inappropriate use.

Statistic data

  • Created on 2007-04-02, updated on 2012-10-22.
  • Ca. 140 lines of code, estimated development costs: 140 - 560 €