Skip to content

Commit 392c212

Browse files
PavelAnabijaczleweli
authored andcommitted
os_info_windows.cpp - in OS_info(): get address of RtlGetVersion via GetProcAddress because some of our projects fail to build if ntdll.dll is added as link input
1 parent eac1111 commit 392c212

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/system/OS_info/os_info_windows.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include <winnt.h>
1818
#include <winternl.h>
1919

20-
extern "C" NTSYSAPI NTSTATUS NTAPI RtlGetVersion(_Out_ PRTL_OSVERSIONINFOW lpVersionInformation);
21-
2220
#ifdef _MSC_VER
2321
#define strtok_r(...) strtok_s(__VA_ARGS__)
2422
#endif
@@ -110,12 +108,19 @@ static unsigned int build_number() {
110108
return token ? std::strtoul(token, nullptr, 10) : 0;
111109
}
112110

113-
// Get OS version via RtlGetVersion which still works well in Windows 8 and above
111+
// RtlGetVersion() started being phased out in Windows 8; need to load it dynamically for Windows 11(?)
114112
// https://docs.microsoft.com/en-us/windows/win32/devnotes/rtlgetversion
115113
iware::system::OS_info_t iware::system::OS_info() {
114+
static NTSTATUS(NTAPI * pRtlGetVersion)(_Out_ PRTL_OSVERSIONINFOW lpVersionInformation) = [] {
115+
decltype(pRtlGetVersion) ret{};
116+
if(HMODULE ntdllh = GetModuleHandleA("ntdll.dll"))
117+
ret = reinterpret_cast<decltype(pRtlGetVersion)>(GetProcAddress(ntdllh, "RtlGetVersion"));
118+
return ret;
119+
}();
116120
RTL_OSVERSIONINFOW os_version_info{};
117121
os_version_info.dwOSVersionInfoSize = sizeof(os_version_info);
118-
RtlGetVersion(&os_version_info);
122+
if(pRtlGetVersion)
123+
pRtlGetVersion(&os_version_info);
119124

120125
return {"Windows NT", version_name(), os_version_info.dwMajorVersion, os_version_info.dwMinorVersion, os_version_info.dwBuildNumber, build_number()};
121126
}

0 commit comments

Comments
 (0)