Environment_WIN32U.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //
  2. // Environment_WIN32U.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Environment_WIN32U.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: Environment
  9. //
  10. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Environment_WIN32U.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/UnicodeConverter.h"
  18. #include "Poco/Buffer.h"
  19. #include <sstream>
  20. #include <cstring>
  21. #include "Poco/UnWindows.h"
  22. #include <iphlpapi.h>
  23. namespace Poco {
  24. std::string EnvironmentImpl::getImpl(const std::string& name)
  25. {
  26. std::wstring uname;
  27. UnicodeConverter::toUTF16(name, uname);
  28. DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
  29. if (len == 0) throw NotFoundException(name);
  30. Buffer<wchar_t> buffer(len);
  31. GetEnvironmentVariableW(uname.c_str(), buffer.begin(), len);
  32. std::string result;
  33. UnicodeConverter::toUTF8(buffer.begin(), len - 1, result);
  34. return result;
  35. }
  36. bool EnvironmentImpl::hasImpl(const std::string& name)
  37. {
  38. std::wstring uname;
  39. UnicodeConverter::toUTF16(name, uname);
  40. DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
  41. return len > 0;
  42. }
  43. void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
  44. {
  45. std::wstring uname;
  46. std::wstring uvalue;
  47. UnicodeConverter::toUTF16(name, uname);
  48. UnicodeConverter::toUTF16(value, uvalue);
  49. if (SetEnvironmentVariableW(uname.c_str(), uvalue.c_str()) == 0)
  50. {
  51. std::string msg = "cannot set environment variable: ";
  52. msg.append(name);
  53. throw SystemException(msg);
  54. }
  55. }
  56. std::string EnvironmentImpl::osNameImpl()
  57. {
  58. OSVERSIONINFO vi;
  59. vi.dwOSVersionInfoSize = sizeof(vi);
  60. if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
  61. switch (vi.dwPlatformId)
  62. {
  63. case VER_PLATFORM_WIN32s:
  64. return "Windows 3.x";
  65. case VER_PLATFORM_WIN32_WINDOWS:
  66. return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98";
  67. case VER_PLATFORM_WIN32_NT:
  68. return "Windows NT";
  69. default:
  70. return "Unknown";
  71. }
  72. }
  73. std::string EnvironmentImpl::osDisplayNameImpl()
  74. {
  75. OSVERSIONINFO vi;
  76. vi.dwOSVersionInfoSize = sizeof(vi);
  77. if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
  78. switch(vi.dwMajorVersion)
  79. {
  80. case 6:
  81. switch (vi.dwMinorVersion)
  82. {
  83. case 0:
  84. return "Windows Vista/Server 2008";
  85. case 1:
  86. return "Windows 7/Server 2008 R2";
  87. case 2:
  88. return "Windows 8/Server 2012";
  89. default:
  90. return "Unknown";
  91. }
  92. case 5:
  93. switch (vi.dwMinorVersion)
  94. {
  95. case 0:
  96. return "Windows 2000";
  97. case 1:
  98. return "Windows XP";
  99. case 2:
  100. return "Windows Server 2003/Windows Server 2003 R2";
  101. default:
  102. return "Unknown";
  103. }
  104. case 4:
  105. switch (vi.dwMinorVersion)
  106. {
  107. case 0:
  108. return "Windows 95/Windows NT 4.0";
  109. case 10:
  110. return "Windows 98";
  111. case 90:
  112. return "Windows ME";
  113. default:
  114. return "Unknown";
  115. }
  116. default:
  117. return "Unknown";
  118. }
  119. }
  120. std::string EnvironmentImpl::osVersionImpl()
  121. {
  122. OSVERSIONINFOW vi;
  123. vi.dwOSVersionInfoSize = sizeof(vi);
  124. if (GetVersionExW(&vi) == 0) throw SystemException("Cannot get OS version information");
  125. std::ostringstream str;
  126. str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF);
  127. std::string version;
  128. UnicodeConverter::toUTF8(vi.szCSDVersion, version);
  129. if (!version.empty()) str << ": " << version;
  130. str << ")";
  131. return str.str();
  132. }
  133. std::string EnvironmentImpl::osArchitectureImpl()
  134. {
  135. SYSTEM_INFO si;
  136. GetSystemInfo(&si);
  137. switch (si.wProcessorArchitecture)
  138. {
  139. case PROCESSOR_ARCHITECTURE_INTEL:
  140. return "IA32";
  141. case PROCESSOR_ARCHITECTURE_MIPS:
  142. return "MIPS";
  143. case PROCESSOR_ARCHITECTURE_ALPHA:
  144. return "ALPHA";
  145. case PROCESSOR_ARCHITECTURE_PPC:
  146. return "PPC";
  147. case PROCESSOR_ARCHITECTURE_IA64:
  148. return "IA64";
  149. #ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
  150. case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
  151. return "IA64/32";
  152. #endif
  153. #ifdef PROCESSOR_ARCHITECTURE_AMD64
  154. case PROCESSOR_ARCHITECTURE_AMD64:
  155. return "AMD64";
  156. #endif
  157. default:
  158. return "Unknown";
  159. }
  160. }
  161. std::string EnvironmentImpl::nodeNameImpl()
  162. {
  163. wchar_t name[MAX_COMPUTERNAME_LENGTH + 1];
  164. DWORD size = MAX_COMPUTERNAME_LENGTH + 1;
  165. if (GetComputerNameW(name, &size) == 0) throw SystemException("Cannot get computer name");
  166. std::string result;
  167. UnicodeConverter::toUTF8(name, result);
  168. return result;
  169. }
  170. void EnvironmentImpl::nodeIdImpl(NodeId& id)
  171. {
  172. std::memset(&id, 0, sizeof(id));
  173. PIP_ADAPTER_INFO pAdapterInfo;
  174. PIP_ADAPTER_INFO pAdapter = 0;
  175. ULONG len = sizeof(IP_ADAPTER_INFO);
  176. pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
  177. // Make an initial call to GetAdaptersInfo to get
  178. // the necessary size into len
  179. DWORD rc = GetAdaptersInfo(pAdapterInfo, &len);
  180. if (rc == ERROR_BUFFER_OVERFLOW)
  181. {
  182. delete [] reinterpret_cast<char*>(pAdapterInfo);
  183. pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
  184. }
  185. else if (rc != ERROR_SUCCESS)
  186. {
  187. return;
  188. }
  189. if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR)
  190. {
  191. pAdapter = pAdapterInfo;
  192. bool found = false;
  193. while (pAdapter && !found)
  194. {
  195. if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id))
  196. {
  197. found = true;
  198. std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
  199. }
  200. pAdapter = pAdapter->Next;
  201. }
  202. }
  203. delete [] reinterpret_cast<char*>(pAdapterInfo);
  204. }
  205. unsigned EnvironmentImpl::processorCountImpl()
  206. {
  207. SYSTEM_INFO si;
  208. GetSystemInfo(&si);
  209. return si.dwNumberOfProcessors;
  210. }
  211. } // namespace Poco