[80] | 1 | // |
---|
| 2 | // Environment_WIN32.cpp |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/Foundation/src/Environment_WIN32.cpp#6 $ |
---|
| 5 | // |
---|
| 6 | // Library: Foundation |
---|
| 7 | // Package: Core |
---|
| 8 | // Module: Environment |
---|
| 9 | // |
---|
| 10 | // Copyright (c) 2004-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 | |
---|
| 36 | |
---|
| 37 | #include <Poco/Environment_WIN32.h> |
---|
| 38 | #include <Poco/Exception.h> |
---|
| 39 | #include <sstream> |
---|
| 40 | #include <cstring> |
---|
| 41 | #include <Poco/UnWindows.h> |
---|
| 42 | #include <iphlpapi.h> |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | namespace Poco { |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | std::string EnvironmentImpl::getImpl(const std::string& name) |
---|
| 49 | { |
---|
| 50 | DWORD len = GetEnvironmentVariableA(name.c_str(), 0, 0); |
---|
| 51 | if (len == 0) throw NotFoundException(name); |
---|
| 52 | char* buffer = new char[len]; |
---|
| 53 | GetEnvironmentVariableA(name.c_str(), buffer, len); |
---|
| 54 | std::string result(buffer); |
---|
| 55 | delete [] buffer; |
---|
| 56 | return result; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | bool EnvironmentImpl::hasImpl(const std::string& name) |
---|
| 61 | { |
---|
| 62 | DWORD len = GetEnvironmentVariableA(name.c_str(), 0, 0); |
---|
| 63 | return len > 0; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | void EnvironmentImpl::setImpl(const std::string& name, const std::string& value) |
---|
| 68 | { |
---|
| 69 | if (SetEnvironmentVariableA(name.c_str(), value.c_str()) == 0) |
---|
| 70 | { |
---|
| 71 | std::string msg = "cannot set environment variable: "; |
---|
| 72 | msg.append(name); |
---|
| 73 | throw SystemException(msg); |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | std::string EnvironmentImpl::osNameImpl() |
---|
| 79 | { |
---|
| 80 | OSVERSIONINFO vi; |
---|
| 81 | vi.dwOSVersionInfoSize = sizeof(vi); |
---|
| 82 | if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information"); |
---|
| 83 | switch (vi.dwPlatformId) |
---|
| 84 | { |
---|
| 85 | case VER_PLATFORM_WIN32s: |
---|
| 86 | return "Windows 3.x"; |
---|
| 87 | case VER_PLATFORM_WIN32_WINDOWS: |
---|
| 88 | return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98"; |
---|
| 89 | case VER_PLATFORM_WIN32_NT: |
---|
| 90 | return "Windows NT"; |
---|
| 91 | default: |
---|
| 92 | return "Unknown"; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | std::string EnvironmentImpl::osVersionImpl() |
---|
| 98 | { |
---|
| 99 | OSVERSIONINFO vi; |
---|
| 100 | vi.dwOSVersionInfoSize = sizeof(vi); |
---|
| 101 | if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information"); |
---|
| 102 | std::ostringstream str; |
---|
| 103 | str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF); |
---|
| 104 | if (vi.szCSDVersion[0]) str << ": " << vi.szCSDVersion; |
---|
| 105 | str << ")"; |
---|
| 106 | return str.str(); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | std::string EnvironmentImpl::osArchitectureImpl() |
---|
| 111 | { |
---|
| 112 | SYSTEM_INFO si; |
---|
| 113 | GetSystemInfo(&si); |
---|
| 114 | switch (si.wProcessorArchitecture) |
---|
| 115 | { |
---|
| 116 | case PROCESSOR_ARCHITECTURE_INTEL: |
---|
| 117 | return "IA32"; |
---|
| 118 | case PROCESSOR_ARCHITECTURE_MIPS: |
---|
| 119 | return "MIPS"; |
---|
| 120 | case PROCESSOR_ARCHITECTURE_ALPHA: |
---|
| 121 | return "ALPHA"; |
---|
| 122 | case PROCESSOR_ARCHITECTURE_PPC: |
---|
| 123 | return "PPC"; |
---|
| 124 | case PROCESSOR_ARCHITECTURE_IA64: |
---|
| 125 | return "IA64"; |
---|
| 126 | #ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64 |
---|
| 127 | case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64: |
---|
| 128 | return "IA64/32"; |
---|
| 129 | #endif |
---|
| 130 | #ifdef PROCESSOR_ARCHITECTURE_AMD64 |
---|
| 131 | case PROCESSOR_ARCHITECTURE_AMD64: |
---|
| 132 | return "AMD64"; |
---|
| 133 | #endif |
---|
| 134 | default: |
---|
| 135 | return "Unknown"; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | std::string EnvironmentImpl::nodeNameImpl() |
---|
| 141 | { |
---|
| 142 | char name[MAX_COMPUTERNAME_LENGTH + 1]; |
---|
| 143 | DWORD size = sizeof(name); |
---|
| 144 | if (GetComputerNameA(name, &size) == 0) throw SystemException("Cannot get computer name"); |
---|
| 145 | return std::string(name); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | void EnvironmentImpl::nodeIdImpl(NodeId& id) |
---|
| 150 | { |
---|
| 151 | PIP_ADAPTER_INFO pAdapterInfo; |
---|
| 152 | PIP_ADAPTER_INFO pAdapter = 0; |
---|
| 153 | ULONG len = sizeof(IP_ADAPTER_INFO); |
---|
| 154 | pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]); |
---|
| 155 | // Make an initial call to GetAdaptersInfo to get |
---|
| 156 | // the necessary size into len |
---|
| 157 | DWORD rc = GetAdaptersInfo(pAdapterInfo, &len); |
---|
| 158 | if (rc == ERROR_BUFFER_OVERFLOW) |
---|
| 159 | { |
---|
| 160 | delete [] reinterpret_cast<char*>(pAdapterInfo); |
---|
| 161 | pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]); |
---|
| 162 | } |
---|
| 163 | else if (rc != ERROR_SUCCESS) |
---|
| 164 | { |
---|
| 165 | throw SystemException("cannot get network adapter list"); |
---|
| 166 | } |
---|
| 167 | try |
---|
| 168 | { |
---|
| 169 | bool found = false; |
---|
| 170 | if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR) |
---|
| 171 | { |
---|
| 172 | pAdapter = pAdapterInfo; |
---|
| 173 | while (pAdapter && !found) |
---|
| 174 | { |
---|
| 175 | if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id)) |
---|
| 176 | { |
---|
| 177 | std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength); |
---|
| 178 | found = true; |
---|
| 179 | } |
---|
| 180 | pAdapter = pAdapter->Next; |
---|
| 181 | } |
---|
| 182 | } |
---|
| 183 | else throw SystemException("cannot get network adapter list"); |
---|
| 184 | if (!found) throw SystemException("no Ethernet adapter found"); |
---|
| 185 | } |
---|
| 186 | catch (Exception&) |
---|
| 187 | { |
---|
| 188 | delete [] reinterpret_cast<char*>(pAdapterInfo); |
---|
| 189 | throw; |
---|
| 190 | } |
---|
| 191 | delete [] reinterpret_cast<char*>(pAdapterInfo); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | unsigned EnvironmentImpl::processorCountImpl() |
---|
| 196 | { |
---|
| 197 | SYSTEM_INFO si; |
---|
| 198 | GetSystemInfo(&si); |
---|
| 199 | return si.dwNumberOfProcessors; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | } // namespace Poco |
---|