PageRenderTime 35ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/labo/2009.08.17/poco-1.3.5-mingw/Foundation/src/Environment_WIN32U.cpp

http://mingw-lib.googlecode.com/
C++ | 209 lines | 151 code | 22 blank | 36 comment | 29 complexity | a1908d9f8db24b2fa026157fdfb07106 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. //
  2. // Environment_WIN32U.cpp
  3. //
  4. // $Id: //poco/1.3/Foundation/src/Environment_WIN32U.cpp#4 $
  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. // Permission is hereby granted, free of charge, to any person or organization
  14. // obtaining a copy of the software and accompanying documentation covered by
  15. // this license (the "Software") to use, reproduce, display, distribute,
  16. // execute, and transmit the Software, and to prepare derivative works of the
  17. // Software, and to permit third-parties to whom the Software is furnished to
  18. // do so, all subject to the following:
  19. //
  20. // The copyright notices in the Software and this entire statement, including
  21. // the above license grant, this restriction and the following disclaimer,
  22. // must be included in all copies of the Software, in whole or in part, and
  23. // all derivative works of the Software, unless such copies or derivative
  24. // works are solely in the form of machine-executable object code generated by
  25. // a source language processor.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  30. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  31. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  32. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. //
  35. #include "Poco/Environment_WIN32U.h"
  36. #include "Poco/Exception.h"
  37. #include "Poco/UnicodeConverter.h"
  38. #include "Poco/Buffer.h"
  39. #include <sstream>
  40. #include <cstring>
  41. #include "Poco/UnWindows.h"
  42. #include <iphlpapi.h>
  43. namespace Poco {
  44. std::string EnvironmentImpl::getImpl(const std::string& name)
  45. {
  46. std::wstring uname;
  47. UnicodeConverter::toUTF16(name, uname);
  48. DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
  49. if (len == 0) throw NotFoundException(name);
  50. Buffer<wchar_t> buffer(len);
  51. GetEnvironmentVariableW(uname.c_str(), buffer.begin(), len);
  52. std::string result;
  53. UnicodeConverter::toUTF8(buffer.begin(), len - 1, result);
  54. return result;
  55. }
  56. bool EnvironmentImpl::hasImpl(const std::string& name)
  57. {
  58. std::wstring uname;
  59. UnicodeConverter::toUTF16(name, uname);
  60. DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
  61. return len > 0;
  62. }
  63. void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
  64. {
  65. std::wstring uname;
  66. std::wstring uvalue;
  67. UnicodeConverter::toUTF16(name, uname);
  68. UnicodeConverter::toUTF16(value, uvalue);
  69. if (SetEnvironmentVariableW(uname.c_str(), uvalue.c_str()) == 0)
  70. {
  71. std::string msg = "cannot set environment variable: ";
  72. msg.append(name);
  73. throw SystemException(msg);
  74. }
  75. }
  76. std::string EnvironmentImpl::osNameImpl()
  77. {
  78. OSVERSIONINFO vi;
  79. vi.dwOSVersionInfoSize = sizeof(vi);
  80. if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
  81. switch (vi.dwPlatformId)
  82. {
  83. case VER_PLATFORM_WIN32s:
  84. return "Windows 3.x";
  85. case VER_PLATFORM_WIN32_WINDOWS:
  86. return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98";
  87. case VER_PLATFORM_WIN32_NT:
  88. return "Windows NT";
  89. default:
  90. return "Unknown";
  91. }
  92. }
  93. std::string EnvironmentImpl::osVersionImpl()
  94. {
  95. OSVERSIONINFOW vi;
  96. vi.dwOSVersionInfoSize = sizeof(vi);
  97. if (GetVersionExW(&vi) == 0) throw SystemException("Cannot get OS version information");
  98. std::ostringstream str;
  99. str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF);
  100. std::string version;
  101. UnicodeConverter::toUTF8(vi.szCSDVersion, version);
  102. if (!version.empty()) str << ": " << version;
  103. str << ")";
  104. return str.str();
  105. }
  106. std::string EnvironmentImpl::osArchitectureImpl()
  107. {
  108. SYSTEM_INFO si;
  109. GetSystemInfo(&si);
  110. switch (si.wProcessorArchitecture)
  111. {
  112. case PROCESSOR_ARCHITECTURE_INTEL:
  113. return "IA32";
  114. case PROCESSOR_ARCHITECTURE_MIPS:
  115. return "MIPS";
  116. case PROCESSOR_ARCHITECTURE_ALPHA:
  117. return "ALPHA";
  118. case PROCESSOR_ARCHITECTURE_PPC:
  119. return "PPC";
  120. case PROCESSOR_ARCHITECTURE_IA64:
  121. return "IA64";
  122. #ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
  123. case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
  124. return "IA64/32";
  125. #endif
  126. #ifdef PROCESSOR_ARCHITECTURE_AMD64
  127. case PROCESSOR_ARCHITECTURE_AMD64:
  128. return "AMD64";
  129. #endif
  130. default:
  131. return "Unknown";
  132. }
  133. }
  134. std::string EnvironmentImpl::nodeNameImpl()
  135. {
  136. wchar_t name[MAX_COMPUTERNAME_LENGTH + 1];
  137. DWORD size = MAX_COMPUTERNAME_LENGTH + 1;
  138. if (GetComputerNameW(name, &size) == 0) throw SystemException("Cannot get computer name");
  139. std::string result;
  140. UnicodeConverter::toUTF8(name, result);
  141. return result;
  142. }
  143. void EnvironmentImpl::nodeIdImpl(NodeId& id)
  144. {
  145. PIP_ADAPTER_INFO pAdapterInfo;
  146. PIP_ADAPTER_INFO pAdapter = 0;
  147. ULONG len = sizeof(IP_ADAPTER_INFO);
  148. pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
  149. // Make an initial call to GetAdaptersInfo to get
  150. // the necessary size into len
  151. DWORD rc = GetAdaptersInfo(pAdapterInfo, &len);
  152. if (rc == ERROR_BUFFER_OVERFLOW)
  153. {
  154. delete [] reinterpret_cast<char*>(pAdapterInfo);
  155. pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
  156. }
  157. else if (rc != ERROR_SUCCESS)
  158. {
  159. throw SystemException("cannot get network adapter list");
  160. }
  161. try
  162. {
  163. bool found = false;
  164. if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR)
  165. {
  166. pAdapter = pAdapterInfo;
  167. while (pAdapter && !found)
  168. {
  169. if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id))
  170. {
  171. std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
  172. found = true;
  173. }
  174. pAdapter = pAdapter->Next;
  175. }
  176. }
  177. else throw SystemException("cannot get network adapter list");
  178. if (!found) throw SystemException("no Ethernet adapter found");
  179. }
  180. catch (Exception&)
  181. {
  182. delete [] reinterpret_cast<char*>(pAdapterInfo);
  183. throw;
  184. }
  185. delete [] reinterpret_cast<char*>(pAdapterInfo);
  186. }
  187. } // namespace Poco