Udostępnij za pośrednictwem


Pobieranie wersji systemu

W poniższym przykładzie użyto funkcji pomocnika interfejsu API wersji w celu określenia wersji bieżącego systemu operacyjnego oraz ustalenia, czy jest to wersja serwerowa czy kliencka, a następnie informacje te zostają wyświetlone w konsoli. Jeśli tryb zgodności jest aktywny, w przykładzie zostanie wyświetlony system operacyjny wybrany dla zgodności aplikacji.

Poleganie na informacjach o wersji nie jest najlepszym sposobem testowania funkcji. Zamiast tego zapoznaj się z dokumentacją dotyczącą interesującej cię funkcji. Aby uzyskać więcej informacji na temat typowych technik wykrywania funkcji, zobacz wersja systemu operacyjnego.

#include <windows.h>
#include <stdio.h>
#include <VersionHelpers.h>

int 
__cdecl
wmain(
    __in int argc, 
    __in_ecount(argc) PCWSTR argv[]
    )
{
    UNREFERENCED_PARAMETER(argc);
    UNREFERENCED_PARAMETER(argv);

    if (IsWindowsXPOrGreater())
    {
        printf("XPOrGreater\n");
    }

    if (IsWindowsXPSP1OrGreater())
    {
        printf("XPSP1OrGreater\n");
    }

    if (IsWindowsXPSP2OrGreater())
    {
        printf("XPSP2OrGreater\n");
    }

    if (IsWindowsXPSP3OrGreater())
    {
        printf("XPSP3OrGreater\n");
    }

    if (IsWindowsVistaOrGreater())
    {
        printf("VistaOrGreater\n");
    }

    if (IsWindowsVistaSP1OrGreater())
    {
        printf("VistaSP1OrGreater\n");
    }

    if (IsWindowsVistaSP2OrGreater())
    {
        printf("VistaSP2OrGreater\n");
    }

    if (IsWindows7OrGreater())
    {
        printf("Windows7OrGreater\n");
    }

    if (IsWindows7SP1OrGreater())
    {
        printf("Windows7SP1OrGreater\n");
    }

    if (IsWindows8OrGreater())
    {
        printf("Windows8OrGreater\n");
    }

    if (IsWindows8Point1OrGreater())
    {
        printf("Windows8Point1OrGreater\n");
    }

    if (IsWindows10OrGreater())
    {
        printf("Windows10OrGreater\n");
    }

    if (IsWindowsServer())
    {
        printf("Server\n");
    }
    else
    {
        printf("Client\n");
    }
}