2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows API で Windows のバージョンを取得する

Last updated at Posted at 2024-03-31

Windows のバージョンを取得するための API として GetVersion, GetVersionEx が用意されていますが、Windows 8.1 以降実質的に使えなくなっています。バージョンに依存した処理を書かないようにというマイクロソフトのメッセージだと思いますが、それでもチェックしたい場面もあると思います。

いくつか方法はありますが、ここでは一番単純と思われる RtlGetVersion を使って取得します。

#include <stdio.h>
#include <Windows.h>

void GetWindowsVersion(DWORD* majorVersion, DWORD* minorVersion, DWORD* buildNumber) {
    DWORD major = 0;
    DWORD minor = 0;
    DWORD build = 0;
    auto ntdll = LoadLibraryExW(L"ntdll.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
    if (ntdll != nullptr) {
        using RtlGetVersion = NTSTATUS(WINAPI*)(PRTL_OSVERSIONINFOW lpVersionInformation);
        auto rtlGetVersion = reinterpret_cast<RtlGetVersion>(GetProcAddress(ntdll, "RtlGetVersion"));
        if (rtlGetVersion != nullptr) {
            RTL_OSVERSIONINFOW versionInfo{};
            versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
            rtlGetVersion(&versionInfo);
            major = versionInfo.dwMajorVersion;
            minor = versionInfo.dwMinorVersion;
            build = versionInfo.dwBuildNumber;
        }
        FreeLibrary(ntdll);
    }
    if (majorVersion != nullptr) *majorVersion = major;
    if (minorVersion != nullptr) *minorVersion = minor;
    if (buildNumber != nullptr) *buildNumber = build;
}

int main()
{
    DWORD majorVersion = 0;
    DWORD minorVersion = 0;
    DWORD buildNumber = 0;
    GetWindowsVersion(&majorVersion, &minorVersion, &buildNumber);
    printf("majorVersion:%d, minorVersion:%d, buildNumber:%d\n", majorVersion, minorVersion, buildNumber);
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?