-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsVersion.cpp
More file actions
65 lines (64 loc) · 2.22 KB
/
WindowsVersion.cpp
File metadata and controls
65 lines (64 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#define MAX_LINE_LENGTH 4096
#include <string.h>
#include <cstring>
#include <atlstr.h>
#include <cstdlib>
#include <windows.h>
#include <iostream>
#include <winver.h>
static CString GetFileVersion(LPCTSTR filePath)
{
CString ret = L"";
DWORD dummy;
DWORD dwSize = GetFileVersionInfoSizeEx(FILE_VER_GET_LOCALISED, filePath, &dummy);
if (dwSize == 0)
{
//wprintf(L"%d\n", dwSize);
DWORD error = ::GetLastError();
//std::string message = std::system_category().message(error);
//printf("%s %s\n",filePath, message.c_str());
ret = L"Not Found";
}
else
{
BYTE* data = new BYTE[dwSize];
BOOL err = GetFileVersionInfoEx(FILE_VER_GET_LOCALISED, filePath, 0, dwSize, &data[0]);
if (! err)
ret = L"Error retriving version (Version Data Not Retrived";
else
{
UINT infoLen = 0;
CString OS = L"";
VS_FIXEDFILEINFO* pFixedInfo = NULL;
err = VerQueryValue(&data[0], L"\\", (LPVOID*)&pFixedInfo, &infoLen);
if (!err)
ret = L"Error retriving version(Version Data Query Failed)";
else
{
DWORD dwFileVersionMS = pFixedInfo->dwFileVersionMS;
DWORD dwFileVersionLS = pFixedInfo->dwFileVersionLS;
DWORD dwLeftMost = HIWORD(dwFileVersionMS);
DWORD dwSecondLeft = LOWORD(dwFileVersionMS);
DWORD dwBuild = HIWORD(dwFileVersionLS);
if (dwBuild < 6000) OS = "XP or earlier";
else if (dwBuild < (unsigned int)7600) OS = "Vista";
else if (dwBuild < (unsigned int)9200) OS = "Windows 7";
else if (dwBuild < (unsigned int)9600) OS = "Windows 8";
else if (dwBuild < (unsigned int)10240) OS = "Windows 8.1";
else if (dwBuild < (unsigned int)22000) OS = "Windows 10";
else OS = "Windows 11";
DWORD dwRightMost = LOWORD(dwFileVersionLS);
ret.Format(L"%s Version: %d.%d.%d.%d %s\n", OS, dwLeftMost, dwSecondLeft, dwBuild, dwRightMost);
}
}
delete[] data;
}
return ret;
}
int main(int argc, char* argv[]) {
CString ans = GetFileVersion(L"C:\\Windows\\System32\\ntoskrnl.exe");
wprintf(L"C: %s\n", (LPCTSTR)GetFileVersion(L"C:\\Windows\\System32\\ntoskrnl.exe"));
wprintf(L"E: %s\n", (LPCTSTR)GetFileVersion(L"E:\\Windows\\System32\\ntoskrnl.exe"));
wprintf(L"X: %s\n", (LPCTSTR)GetFileVersion(L"X:\\Windows\\System32\\ntoskrnl.exe"));
//std::wcout << ans.GetString() << std::endl;
}