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_WIN32.h"
00038 #include "Poco/Exception.h"
00039 #include <sstream>
00040 #include <cstring>
00041 #include "Poco/UnWindows.h"
00042 #include <iphlpapi.h>
00043
00044
00045 namespace Poco {
00046
00047
00048 std::string EnvironmentImpl::getImpl(const std::string& name)
00049 {
00050 DWORD len = GetEnvironmentVariableA(name.c_str(), 0, 0);
00051 if (len == 0) throw NotFoundException(name);
00052 char* buffer = new char[len];
00053 GetEnvironmentVariableA(name.c_str(), buffer, len);
00054 std::string result(buffer);
00055 delete [] buffer;
00056 return result;
00057 }
00058
00059
00060 bool EnvironmentImpl::hasImpl(const std::string& name)
00061 {
00062 DWORD len = GetEnvironmentVariableA(name.c_str(), 0, 0);
00063 return len > 0;
00064 }
00065
00066
00067 void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
00068 {
00069 if (SetEnvironmentVariableA(name.c_str(), value.c_str()) == 0)
00070 {
00071 std::string msg = "cannot set environment variable: ";
00072 msg.append(name);
00073 throw SystemException(msg);
00074 }
00075 }
00076
00077
00078 std::string EnvironmentImpl::osNameImpl()
00079 {
00080 OSVERSIONINFO vi;
00081 vi.dwOSVersionInfoSize = sizeof(vi);
00082 if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
00083 switch (vi.dwPlatformId)
00084 {
00085 case VER_PLATFORM_WIN32s:
00086 return "Windows 3.x";
00087 case VER_PLATFORM_WIN32_WINDOWS:
00088 return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98";
00089 case VER_PLATFORM_WIN32_NT:
00090 return "Windows NT";
00091 default:
00092 return "Unknown";
00093 }
00094 }
00095
00096
00097 std::string EnvironmentImpl::osVersionImpl()
00098 {
00099 OSVERSIONINFO vi;
00100 vi.dwOSVersionInfoSize = sizeof(vi);
00101 if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
00102 std::ostringstream str;
00103 str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF);
00104 if (vi.szCSDVersion[0]) str << ": " << vi.szCSDVersion;
00105 str << ")";
00106 return str.str();
00107 }
00108
00109
00110 std::string EnvironmentImpl::osArchitectureImpl()
00111 {
00112 SYSTEM_INFO si;
00113 GetSystemInfo(&si);
00114 switch (si.wProcessorArchitecture)
00115 {
00116 case PROCESSOR_ARCHITECTURE_INTEL:
00117 return "IA32";
00118 case PROCESSOR_ARCHITECTURE_MIPS:
00119 return "MIPS";
00120 case PROCESSOR_ARCHITECTURE_ALPHA:
00121 return "ALPHA";
00122 case PROCESSOR_ARCHITECTURE_PPC:
00123 return "PPC";
00124 case PROCESSOR_ARCHITECTURE_IA64:
00125 return "IA64";
00126 #ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
00127 case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
00128 return "IA64/32";
00129 #endif
00130 #ifdef PROCESSOR_ARCHITECTURE_AMD64
00131 case PROCESSOR_ARCHITECTURE_AMD64:
00132 return "AMD64";
00133 #endif
00134 default:
00135 return "Unknown";
00136 }
00137 }
00138
00139
00140 std::string EnvironmentImpl::nodeNameImpl()
00141 {
00142 char name[MAX_COMPUTERNAME_LENGTH + 1];
00143 DWORD size = sizeof(name);
00144 if (GetComputerNameA(name, &size) == 0) throw SystemException("Cannot get computer name");
00145 return std::string(name);
00146 }
00147
00148
00149 void EnvironmentImpl::nodeIdImpl(NodeId& id)
00150 {
00151 PIP_ADAPTER_INFO pAdapterInfo;
00152 PIP_ADAPTER_INFO pAdapter = 0;
00153 ULONG len = sizeof(IP_ADAPTER_INFO);
00154 pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
00155
00156
00157 DWORD rc = GetAdaptersInfo(pAdapterInfo, &len);
00158 if (rc == ERROR_BUFFER_OVERFLOW)
00159 {
00160 delete [] reinterpret_cast<char*>(pAdapterInfo);
00161 pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
00162 }
00163 else if (rc != ERROR_SUCCESS)
00164 {
00165 throw SystemException("cannot get network adapter list");
00166 }
00167 try
00168 {
00169 bool found = false;
00170 if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR)
00171 {
00172 pAdapter = pAdapterInfo;
00173 while (pAdapter && !found)
00174 {
00175 if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id))
00176 {
00177 std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
00178 found = true;
00179 }
00180 pAdapter = pAdapter->Next;
00181 }
00182 }
00183 else throw SystemException("cannot get network adapter list");
00184 if (!found) throw SystemException("no Ethernet adapter found");
00185 }
00186 catch (Exception&)
00187 {
00188 delete [] reinterpret_cast<char*>(pAdapterInfo);
00189 throw;
00190 }
00191 delete [] reinterpret_cast<char*>(pAdapterInfo);
00192 }
00193
00194
00195 }