Skip to content

Utility::GetPlatform*(): on Windows query the registry for version info#9699

Open
Al2Klimov wants to merge 1 commit intomasterfrom
windows-version-detection
Open

Utility::GetPlatform*(): on Windows query the registry for version info#9699
Al2Klimov wants to merge 1 commit intomasterfrom
windows-version-detection

Conversation

@Al2Klimov
Copy link
Member

@Al2Klimov Al2Klimov commented Feb 17, 2023

not to have to adjust the code (see removed lines) for every new version.

closes #7743
closes #9646

Windows 10

Works even as unpriv. user:

Bildschirm­foto 2023-02-17 um 13 22 03+

TODO

Output of & "C:\Program Files\ICINGA2\sbin\icinga2.exe" --version with https://git.icinga.com/packaging/windows-icinga2/-/jobs/272428/artifacts/download (not necessarily as a particular user)

  • Windows 11 – Yonas
  • Windows S2012 – Christian

@Al2Klimov Al2Klimov added bug Something isn't working area/windows Windows agent and plugins labels Feb 17, 2023
@cla-bot cla-bot bot added the cla/signed label Feb 17, 2023
@icinga-probot icinga-probot bot added the area/cli Command line helpers label Feb 17, 2023
@Al2Klimov Al2Klimov force-pushed the windows-version-detection branch from fe63342 to b381210 Compare February 17, 2023 12:34
@Al2Klimov
Copy link
Member Author

@julianbrost I prefer this PR in favor of the OP-closes ones. And you?

@yhabteab yhabteab removed their request for review June 12, 2023 08:55
@Al2Klimov
Copy link
Member Author

@LordHepipud What do you -as an expert- think of the concept in general? Of querying Registry instead of relying on the OSes the binary is aware of at compile time?

Comment on lines +1542 to +1545
union {
BYTE AsBytes[512];
char AsChars[1] = {0};
} Data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have documentation on what this type is used for. I was struggling to wrap my head around this until I found the term type punning about union types :).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw. using union types for type punning is UB. Also, in the cppreference explaination states this:

It is undefined behavior to read from the member of the union that wasn't most recently written.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer reinterpret_cast?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it.

if (err == ERROR_SUCCESS) {
Defer regCloseKey ([hKey]() { (void)RegCloseKey(hKey); });
RegistryString productName;
auto err (RegQueryValueExA(hKey, "ProductName", nullptr, nullptr, productName.Data.AsBytes, &productName.Size));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you need another subkey to search for instead of ProductName e.g OsName, otherwise, it's still printing false positive information (Tested on fresh installed Windows 11 Pro).

PS C:\Users\yhabteab\Workspace\icinga2\build> .\Bin\Release\Debug\icinga2.exe --version
icinga2.exe - The Icinga 2 network monitoring daemon (version: v2.14.0-94-g88a32cdd0; debug)

Copyright (c) 2012-2023 Icinga GmbH (https://icinga.com/)
License GPLv2+: GNU GPL version 2 or later <https://gnu.org/licenses/gpl2.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

System information:
  Platform: Windows
  Platform version: Windows 10 Pro 23H2
  Kernel: Windows
  Kernel version: 6.3.22631
  Architecture: x86_64

Build information:
  Compiler: MSVC 19.29.30153.0                                                                                            Build host: YONASHABTEAF0EE                                                                                             OpenSSL version: OpenSSL 3.0.12 24 Oct 2023

Copy link
Member

@yhabteab yhabteab Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already way better than the master branch though: Platform version: 8 xD

System information:
  Platform: Windows
  Platform version: 8
  Kernel: Windows
  Kernel version: 6.2
  Architecture: x86_64

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, Windows 11 says "Windows 11" in one key, but "Windows 10" in another? Please could you screen your SOFTWARE\Microsoft\Windows NT\CurrentVersion in RegEdit?

It's already way better than the master branch though: Platform version: 8 xD

That's the point.👍

another subkey to search for instead of ProductName e.g OsName

Unfortunately there's no such thing in Server 2012 R2.
Also, "23H2" and the kernel version make the difference.

@Al2Klimov Al2Klimov force-pushed the windows-version-detection branch from b381210 to 1bf4159 Compare January 31, 2025 16:00
@Al2Klimov Al2Klimov force-pushed the windows-version-detection branch from 1bf4159 to 1e4aab1 Compare April 30, 2025 15:33
@Al2Klimov Al2Klimov force-pushed the windows-version-detection branch from 1e4aab1 to d88ce8f Compare March 10, 2026 12:38
@Al2Klimov Al2Klimov force-pushed the windows-version-detection branch from d88ce8f to 42ff74e Compare March 19, 2026 11:38
not to have to adjust the code (see removed lines) for every new version.
@Al2Klimov Al2Klimov force-pushed the windows-version-detection branch from 42ff74e to 88fccc5 Compare March 19, 2026 11:45
@Al2Klimov Al2Klimov requested a review from yhabteab March 19, 2026 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/cli Command line helpers area/windows Windows agent and plugins bug Something isn't working cla/signed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants