Environment_WINCE.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // Environment_WINCE.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Environment_WINCE.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: Environment
  9. //
  10. // Copyright (c) 2009-2010, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Environment_WINCE.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/UnicodeConverter.h"
  18. #include "Poco/String.h"
  19. #include "Poco/Path.h"
  20. #include "Poco/NumberFormatter.h"
  21. #include <sstream>
  22. #include <cstring>
  23. #include <windows.h>
  24. #include <iphlpapi.h>
  25. namespace Poco {
  26. const std::string EnvironmentImpl::TEMP("TEMP");
  27. const std::string EnvironmentImpl::TMP("TMP");
  28. const std::string EnvironmentImpl::HOMEPATH("HOMEPATH");
  29. const std::string EnvironmentImpl::COMPUTERNAME("COMPUTERNAME");
  30. const std::string EnvironmentImpl::OS("OS");
  31. const std::string EnvironmentImpl::NUMBER_OF_PROCESSORS("NUMBER_OF_PROCESSORS");
  32. const std::string EnvironmentImpl::PROCESSOR_ARCHITECTURE("PROCESSOR_ARCHITECTURE");
  33. std::string EnvironmentImpl::getImpl(const std::string& name)
  34. {
  35. std::string value;
  36. if (!envVar(name, &value)) throw NotFoundException(name);
  37. return value;
  38. }
  39. bool EnvironmentImpl::hasImpl(const std::string& name)
  40. {
  41. return envVar(name, 0);
  42. }
  43. void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
  44. {
  45. throw NotImplementedException("Cannot set environment variables on Windows CE");
  46. }
  47. std::string EnvironmentImpl::osNameImpl()
  48. {
  49. return "Windows CE";
  50. }
  51. std::string EnvironmentImpl::osDisplayNameImpl()
  52. {
  53. return osNameImpl();
  54. }
  55. std::string EnvironmentImpl::osVersionImpl()
  56. {
  57. OSVERSIONINFOW vi;
  58. vi.dwOSVersionInfoSize = sizeof(vi);
  59. if (GetVersionExW(&vi) == 0) throw SystemException("Cannot get OS version information");
  60. std::ostringstream str;
  61. str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF);
  62. std::string version;
  63. UnicodeConverter::toUTF8(vi.szCSDVersion, version);
  64. if (!version.empty()) str << ": " << version;
  65. str << ")";
  66. return str.str();
  67. }
  68. std::string EnvironmentImpl::osArchitectureImpl()
  69. {
  70. SYSTEM_INFO si;
  71. GetSystemInfo(&si);
  72. switch (si.wProcessorArchitecture)
  73. {
  74. case PROCESSOR_ARCHITECTURE_INTEL:
  75. return "IA32";
  76. case PROCESSOR_ARCHITECTURE_MIPS:
  77. return "MIPS";
  78. case PROCESSOR_ARCHITECTURE_ALPHA:
  79. return "ALPHA";
  80. case PROCESSOR_ARCHITECTURE_PPC:
  81. return "PPC";
  82. case PROCESSOR_ARCHITECTURE_IA64:
  83. return "IA64";
  84. #ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
  85. case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
  86. return "IA64/32";
  87. #endif
  88. #ifdef PROCESSOR_ARCHITECTURE_AMD64
  89. case PROCESSOR_ARCHITECTURE_AMD64:
  90. return "AMD64";
  91. #endif
  92. case PROCESSOR_ARCHITECTURE_SHX:
  93. return "SHX";
  94. case PROCESSOR_ARCHITECTURE_ARM:
  95. return "ARM";
  96. default:
  97. return "Unknown";
  98. }
  99. }
  100. std::string EnvironmentImpl::nodeNameImpl()
  101. {
  102. HKEY hKey;
  103. DWORD dwDisposition;
  104. if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"\\Ident", 0, 0, 0, 0, 0, &hKey, &dwDisposition) != ERROR_SUCCESS)
  105. throw SystemException("Cannot get node name", "registry key not found");
  106. std::string value;
  107. DWORD dwType;
  108. BYTE bData[1026];
  109. DWORD dwData = sizeof(bData);
  110. if (RegQueryValueExW(hKey, L"Name", 0, &dwType, bData, &dwData) == ERROR_SUCCESS)
  111. {
  112. switch (dwType)
  113. {
  114. case REG_SZ:
  115. UnicodeConverter::toUTF8(reinterpret_cast<wchar_t*>(bData), value);
  116. break;
  117. default:
  118. RegCloseKey(hKey);
  119. throw SystemException("Cannot get node name", "registry value has wrong type");
  120. }
  121. }
  122. else
  123. {
  124. RegCloseKey(hKey);
  125. throw SystemException("Cannot get node name", "registry value not found");
  126. }
  127. RegCloseKey(hKey);
  128. return value;
  129. }
  130. void EnvironmentImpl::nodeIdImpl(NodeId& id)
  131. {
  132. PIP_ADAPTER_INFO pAdapterInfo;
  133. PIP_ADAPTER_INFO pAdapter = 0;
  134. ULONG len = sizeof(IP_ADAPTER_INFO);
  135. pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
  136. // Make an initial call to GetAdaptersInfo to get
  137. // the necessary size into len
  138. DWORD rc = GetAdaptersInfo(pAdapterInfo, &len);
  139. if (rc == ERROR_BUFFER_OVERFLOW)
  140. {
  141. delete [] reinterpret_cast<char*>(pAdapterInfo);
  142. pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
  143. }
  144. else if (rc != ERROR_SUCCESS)
  145. {
  146. throw SystemException("cannot get network adapter list");
  147. }
  148. try
  149. {
  150. bool found = false;
  151. if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR)
  152. {
  153. pAdapter = pAdapterInfo;
  154. while (pAdapter && !found)
  155. {
  156. if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id))
  157. {
  158. std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
  159. found = true;
  160. }
  161. pAdapter = pAdapter->Next;
  162. }
  163. }
  164. else throw SystemException("cannot get network adapter list");
  165. if (!found) throw SystemException("no Ethernet adapter found");
  166. }
  167. catch (Exception&)
  168. {
  169. delete [] reinterpret_cast<char*>(pAdapterInfo);
  170. throw;
  171. }
  172. delete [] reinterpret_cast<char*>(pAdapterInfo);
  173. }
  174. unsigned EnvironmentImpl::processorCountImpl()
  175. {
  176. SYSTEM_INFO si;
  177. GetSystemInfo(&si);
  178. return si.dwNumberOfProcessors;
  179. }
  180. bool EnvironmentImpl::envVar(const std::string& name, std::string* value)
  181. {
  182. if (icompare(name, TEMP) == 0)
  183. {
  184. if (value) *value = Path::temp();
  185. }
  186. else if (icompare(name, TMP) == 0)
  187. {
  188. if (value) *value = Path::temp();
  189. }
  190. else if (icompare(name, HOMEPATH) == 0)
  191. {
  192. if (value) *value = Path::home();
  193. }
  194. else if (icompare(name, COMPUTERNAME) == 0)
  195. {
  196. if (value) *value = nodeNameImpl();
  197. }
  198. else if (icompare(name, OS) == 0)
  199. {
  200. if (value) *value = osNameImpl();
  201. }
  202. else if (icompare(name, NUMBER_OF_PROCESSORS) == 0)
  203. {
  204. if (value) *value = NumberFormatter::format(processorCountImpl());
  205. }
  206. else if (icompare(name, PROCESSOR_ARCHITECTURE) == 0)
  207. {
  208. if (value) *value = osArchitectureImpl();
  209. }
  210. else return false;
  211. return true;
  212. }
  213. } // namespace Poco