Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "Poco/Environment_WIN32U.h"
00038 #include "Poco/Exception.h"
00039 #include "Poco/UnicodeConverter.h"
00040 #include "Poco/Buffer.h"
00041 #include <sstream>
00042 #include <cstring>
00043 #include "Poco/UnWindows.h"
00044 #include <iphlpapi.h>
00045
00046
00047 namespace Poco {
00048
00049
00050 std::string EnvironmentImpl::getImpl(const std::string& name)
00051 {
00052 std::wstring uname;
00053 UnicodeConverter::toUTF16(name, uname);
00054 DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
00055 if (len == 0) throw NotFoundException(name);
00056 Buffer<wchar_t> buffer(len);
00057 GetEnvironmentVariableW(uname.c_str(), buffer.begin(), len);
00058 std::string result;
00059 UnicodeConverter::toUTF8(buffer.begin(), len - 1, result);
00060 return result;
00061 }
00062
00063
00064 bool EnvironmentImpl::hasImpl(const std::string& name)
00065 {
00066 std::wstring uname;
00067 UnicodeConverter::toUTF16(name, uname);
00068 DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
00069 return len > 0;
00070 }
00071
00072
00073 void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
00074 {
00075 std::wstring uname;
00076 std::wstring uvalue;
00077 UnicodeConverter::toUTF16(name, uname);
00078 UnicodeConverter::toUTF16(value, uvalue);
00079 if (SetEnvironmentVariableW(uname.c_str(), uvalue.c_str()) == 0)
00080 {
00081 std::string msg = "cannot set environment variable: ";
00082 msg.append(name);
00083 throw SystemException(msg);
00084 }
00085 }
00086
00087
00088 std::string EnvironmentImpl::osNameImpl()
00089 {
00090 OSVERSIONINFO vi;
00091 vi.dwOSVersionInfoSize = sizeof(vi);
00092 if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
00093 switch (vi.dwPlatformId)
00094 {
00095 case VER_PLATFORM_WIN32s:
00096 return "Windows 3.x";
00097 case VER_PLATFORM_WIN32_WINDOWS:
00098 return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98";
00099 case VER_PLATFORM_WIN32_NT:
00100 return "Windows NT";
00101 default:
00102 return "Unknown";
00103 }
00104 }
00105
00106
00107 std::string EnvironmentImpl::osVersionImpl()
00108 {
00109 OSVERSIONINFOW vi;
00110 vi.dwOSVersionInfoSize = sizeof(vi);
00111 if (GetVersionExW(&vi) == 0) throw SystemException("Cannot get OS version information");
00112 std::ostringstream str;
00113 str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF);
00114 std::string version;
00115 UnicodeConverter::toUTF8(vi.szCSDVersion, version);
00116 if (!version.empty()) str << ": " << version;
00117 str << ")";
00118 return str.str();
00119 }
00120
00121
00122 std::string EnvironmentImpl::osArchitectureImpl()
00123 {
00124 SYSTEM_INFO si;
00125 GetSystemInfo(&si);
00126 switch (si.wProcessorArchitecture)
00127 {
00128 case PROCESSOR_ARCHITECTURE_INTEL:
00129 return "IA32";
00130 case PROCESSOR_ARCHITECTURE_MIPS:
00131 return "MIPS";
00132 case PROCESSOR_ARCHITECTURE_ALPHA:
00133 return "ALPHA";
00134 case PROCESSOR_ARCHITECTURE_PPC:
00135 return "PPC";
00136 case PROCESSOR_ARCHITECTURE_IA64:
00137 return "IA64";
00138 #ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
00139 case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
00140 return "IA64/32";
00141 #endif
00142 #ifdef PROCESSOR_ARCHITECTURE_AMD64
00143 case PROCESSOR_ARCHITECTURE_AMD64:
00144 return "AMD64";
00145 #endif
00146 default:
00147 return "Unknown";
00148 }
00149 }
00150
00151
00152 std::string EnvironmentImpl::nodeNameImpl()
00153 {
00154 wchar_t name[MAX_COMPUTERNAME_LENGTH + 1];
00155 DWORD size = MAX_COMPUTERNAME_LENGTH + 1;
00156 if (GetComputerNameW(name, &size) == 0) throw SystemException("Cannot get computer name");
00157 std::string result;
00158 UnicodeConverter::toUTF8(name, result);
00159 return result;
00160 }
00161
00162
00163 void EnvironmentImpl::nodeIdImpl(NodeId& id)
00164 {
00165 PIP_ADAPTER_INFO pAdapterInfo;
00166 PIP_ADAPTER_INFO pAdapter = 0;
00167 ULONG len = sizeof(IP_ADAPTER_INFO);
00168 pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
00169
00170
00171 DWORD rc = GetAdaptersInfo(pAdapterInfo, &len);
00172 if (rc == ERROR_BUFFER_OVERFLOW)
00173 {
00174 delete [] reinterpret_cast<char*>(pAdapterInfo);
00175 pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
00176 }
00177 else if (rc != ERROR_SUCCESS)
00178 {
00179 throw SystemException("cannot get network adapter list");
00180 }
00181 try
00182 {
00183 bool found = false;
00184 if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR)
00185 {
00186 pAdapter = pAdapterInfo;
00187 while (pAdapter && !found)
00188 {
00189 if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id))
00190 {
00191 std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
00192 found = true;
00193 }
00194 pAdapter = pAdapter->Next;
00195 }
00196 }
00197 else throw SystemException("cannot get network adapter list");
00198 if (!found) throw SystemException("no Ethernet adapter found");
00199 }
00200 catch (Exception&)
00201 {
00202 delete [] reinterpret_cast<char*>(pAdapterInfo);
00203 throw;
00204 }
00205 delete [] reinterpret_cast<char*>(pAdapterInfo);
00206 }
00207
00208
00209 }