Last active
December 18, 2020 13:47
-
-
Save hotwatermorning/52ebc284cc835ba9c28cc70f86535963 to your computer and use it in GitHub Desktop.
Get Full Version String (win)
This file contains 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
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724429(v=vs.85).aspx | |
#include <vector> | |
#include <cstdio> | |
#include <windows.h> | |
std::wstring GetFullVersionString() | |
{ | |
struct LANGCODEPAGE { | |
WORD wLang; | |
WORD wCode; | |
} *lpTranslate; | |
UINT cbTranslate; | |
std::vector<wchar_t> path_buffer(MAX_PATH, L'\0'); | |
auto result = GetSystemDirectory(path_buffer.data(), path_buffer.size()); | |
if(result == 0) { | |
return L""; | |
} | |
auto dll_path = std::wstring(path_buffer.begin(), path_buffer.begin() + result) + L"\\Kernel32.dll"; | |
auto size = GetFileVersionInfoSize(dll_path.c_str(), NULL); | |
std::vector<char> buffer(size); | |
GetFileVersionInfo(dll_path.c_str(), NULL, size, buffer.data()); | |
VerQueryValue(buffer.data(), L"VarFileInfo\\Translation", (LPVOID*)&lpTranslate, &cbTranslate); | |
if(cbTranslate < sizeof(LANGCODEPAGE)) { | |
return L""; | |
} | |
wchar_t query[512]; | |
_snwprintf(query, sizeof(query), L"\\StringFileInfo\\%04x%04x\\ProductVersion", lpTranslate[0].wLang, lpTranslate[0].wCode); | |
wchar_t const *p; | |
UINT n_chars; | |
VerQueryValue(buffer.data(), query, (LPVOID *)&p, &n_chars); | |
return std::wstring(p, p + n_chars); | |
} | |
int main() | |
{ | |
std::wstring full_version_string = GetFullVersionString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment