forked from microsoft/cpprestsdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironmentWindows.cpp
More file actions
113 lines (98 loc) · 3.33 KB
/
EnvironmentWindows.cpp
File metadata and controls
113 lines (98 loc) · 3.33 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <string>
#include <sstream>
#include "Environment.h"
//#include "Exception.h"
//#include "ion/Logger.h"
NS_FK_BEGIN
std::string Environment::GetEnvironmentVariable(const std::string& variable)
{
DWORD len = GetEnvironmentVariableA(variable.c_str(), 0, 0);
if (len == 0) return ""; //throw NotFoundException(variable);
char* buffer = new char[len];
GetEnvironmentVariableA(variable.c_str(), buffer, len);
std::string result(buffer);
delete[] buffer;
return result;
}
bool Environment::HasEnvironmentVariable(const std::string& variable)
{
DWORD len = GetEnvironmentVariableA(variable.c_str(), 0, 0);
return len > 0;
}
void Environment::SetEnvironmentVariable(const std::string& variable, const std::string& value)
{
if (SetEnvironmentVariableA(variable.c_str(), value.c_str()) == 0)
{
std::string msg = "cannot set environment variable: ";
msg.append(variable);
// throw SystemException(msg);
}
}
typedef LONG(NTAPI* fnRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation);
std::string Environment::GetMachineName()
{
RTL_OSVERSIONINFOEXW verInfo = { 0 };
verInfo.dwOSVersionInfoSize = sizeof(verInfo);
static auto RtlGetVersion = (fnRtlGetVersion)GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlGetVersion");
if (RtlGetVersion != 0)
RtlGetVersion((PRTL_OSVERSIONINFOW)&verInfo);
switch (verInfo.dwMajorVersion)
{
case 10:
switch (verInfo.dwMinorVersion)
{
case 0:
return verInfo.wProductType == VER_NT_WORKSTATION ? "Windows 10" : "Windows Server 2016";
}
case 6:
switch (verInfo.dwMinorVersion)
{
case 0:
return verInfo.wProductType == VER_NT_WORKSTATION ? "Windows Vista" : "Windows Server 2008";
case 1:
return verInfo.wProductType == VER_NT_WORKSTATION ? "Windows 7" : "Windows Server 2008 R2";
case 2:
return verInfo.wProductType == VER_NT_WORKSTATION ? "Windows 8" : "Windows Server 2012";
case 3:
return verInfo.wProductType == VER_NT_WORKSTATION ? "Windows 8.1" : "Windows Server 2012 R2";
default:
return "Unknown";
}
case 5:
switch (verInfo.dwMinorVersion)
{
case 0:
return "Windows 2000";
case 1:
return "Windows XP";
case 2:
return "Windows Server 2003/Windows Server 2003 R2";
default:
return "Unknown";
}
default:
return "Unknown";
}
}
std::string Environment::GetOSVersion()
{
RTL_OSVERSIONINFOEXW verInfo = { 0 };
verInfo.dwOSVersionInfoSize = sizeof(verInfo);
static auto RtlGetVersion = (fnRtlGetVersion)GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlGetVersion");
if (RtlGetVersion != 0 && RtlGetVersion((PRTL_OSVERSIONINFOW)&verInfo) == 0)
{
std::ostringstream str;
str << verInfo.dwMajorVersion << "." << verInfo.dwMinorVersion << " (Build " << (verInfo.dwBuildNumber & 0xFFFF);
if (verInfo.szCSDVersion[0]) str << ": " << verInfo.szCSDVersion;
str << ")";
return str.str();
}
else
{
//throw SystemException("Cannot get OS version information");
// LOG_ERROR
std::cout << ("***** Cannot get OS version information.") << std::endl;
}
return "";
}
NS_FK_END