Environment_WIN32U.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // Environment_WIN32U.cpp
  3. //
  4. // $Id: //poco/Main/Foundation/src/Environment_WIN32U.cpp#9 $
  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. namespace Poco {
  43. std::string EnvironmentImpl::getImpl(const std::string& name)
  44. {
  45. std::wstring uname;
  46. UnicodeConverter::toUTF16(name, uname);
  47. DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
  48. if (len == 0) throw NotFoundException(name);
  49. Buffer<wchar_t> buffer(len);
  50. GetEnvironmentVariableW(uname.c_str(), buffer.begin(), len);
  51. std::string result;
  52. UnicodeConverter::toUTF8(buffer.begin(), len - 1, result);
  53. return result;
  54. }
  55. bool EnvironmentImpl::hasImpl(const std::string& name)
  56. {
  57. std::wstring uname;
  58. UnicodeConverter::toUTF16(name, uname);
  59. DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
  60. return len > 0;
  61. }
  62. void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
  63. {
  64. std::wstring uname;
  65. std::wstring uvalue;
  66. UnicodeConverter::toUTF16(name, uname);
  67. UnicodeConverter::toUTF16(value, uvalue);
  68. if (SetEnvironmentVariableW(uname.c_str(), uvalue.c_str()) == 0)
  69. {
  70. std::string msg = "cannot set environment variable: ";
  71. msg.append(name);
  72. throw SystemException(msg);
  73. }
  74. }
  75. std::string EnvironmentImpl::osNameImpl()
  76. {
  77. OSVERSIONINFO vi;
  78. vi.dwOSVersionInfoSize = sizeof(vi);
  79. if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
  80. switch (vi.dwPlatformId)
  81. {
  82. case VER_PLATFORM_WIN32s:
  83. return "Windows 3.x";
  84. case VER_PLATFORM_WIN32_WINDOWS:
  85. return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98";
  86. case VER_PLATFORM_WIN32_NT:
  87. return "Windows NT";
  88. default:
  89. return "Unknown";
  90. }
  91. }
  92. std::string EnvironmentImpl::osVersionImpl()
  93. {
  94. OSVERSIONINFOW vi;
  95. vi.dwOSVersionInfoSize = sizeof(vi);
  96. if (GetVersionExW(&vi) == 0) throw SystemException("Cannot get OS version information");
  97. std::ostringstream str;
  98. str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF);
  99. std::string version;
  100. UnicodeConverter::toUTF8(vi.szCSDVersion, version);
  101. if (!version.empty()) str << ": " << version;
  102. str << ")";
  103. return str.str();
  104. }
  105. std::string EnvironmentImpl::osArchitectureImpl()
  106. {
  107. SYSTEM_INFO si;
  108. GetSystemInfo(&si);
  109. switch (si.wProcessorArchitecture)
  110. {
  111. case PROCESSOR_ARCHITECTURE_INTEL:
  112. return "IA32";
  113. case PROCESSOR_ARCHITECTURE_MIPS:
  114. return "MIPS";
  115. case PROCESSOR_ARCHITECTURE_ALPHA:
  116. return "ALPHA";
  117. case PROCESSOR_ARCHITECTURE_PPC:
  118. return "PPC";
  119. case PROCESSOR_ARCHITECTURE_IA64:
  120. return "IA64";
  121. #ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
  122. case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
  123. return "IA64/32";
  124. #endif
  125. #ifdef PROCESSOR_ARCHITECTURE_AMD64
  126. case PROCESSOR_ARCHITECTURE_AMD64:
  127. return "AMD64";
  128. #endif
  129. default:
  130. return "Unknown";
  131. }
  132. }
  133. std::string EnvironmentImpl::nodeNameImpl()
  134. {
  135. wchar_t name[MAX_COMPUTERNAME_LENGTH + 1];
  136. DWORD size = MAX_COMPUTERNAME_LENGTH + 1;
  137. if (GetComputerNameW(name, &size) == 0) throw SystemException("Cannot get computer name");
  138. std::string result;
  139. UnicodeConverter::toUTF8(name, result);
  140. return result;
  141. }
  142. } // namespace Poco