Created
January 23, 2026 00:53
-
-
Save EncodeTheCode/d581224e85a49da15b4fd03628cfdd82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // concise_safe_get_windows_edition.cpp | |
| // Build (MSVC): cl /O2 /W4 concise_safe_get_windows_edition.cpp | |
| #include <windows.h> | |
| #include <cstdio> | |
| static void print_last_error(const char *ctx) { | |
| DWORD err = GetLastError(); | |
| LPSTR msg = nullptr; | |
| FormatMessageA( | |
| FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | |
| nullptr, err, 0, reinterpret_cast<LPSTR>(&msg), 0, nullptr | |
| ); | |
| fprintf(stderr, "%s: error %lu: %s\n", ctx, (unsigned long)err, msg ? msg : "(no message)"); | |
| if (msg) LocalFree(msg); | |
| } | |
| int main(void) { | |
| const char LABEL[] = "Windows edition: "; | |
| OSVERSIONINFOEXW osv = {}; osv.dwOSVersionInfoSize = sizeof(osv); | |
| HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); | |
| if (!ntdll) { print_last_error("GetModuleHandleW"); return 1; } | |
| using RtlFn = LONG (WINAPI *)(OSVERSIONINFOW*); | |
| auto rtl = reinterpret_cast<RtlFn>(GetProcAddress(ntdll, "RtlGetVersion")); | |
| if (!rtl) { print_last_error("GetProcAddress(RtlGetVersion)"); return 2; } | |
| if (rtl(reinterpret_cast<OSVERSIONINFOW*>(&osv)) != 0) { | |
| fprintf(stderr, "RtlGetVersion failed\n"); return 3; | |
| } | |
| const char *edition = (osv.wProductType == VER_NT_WORKSTATION) | |
| ? ((osv.wSuiteMask & VER_SUITE_PERSONAL) ? "Home" : "Pro") | |
| : "Server"; | |
| printf("%s%s\n", LABEL, edition); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment