Environment_WIN32U.cpp
Go to the documentation of this file.
00001 //
00002 // Environment_WIN32U.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/Environment_WIN32U.cpp#4 $
00005 //
00006 // Library: Foundation
00007 // Package: Core
00008 // Module:  Environment
00009 //
00010 // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
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         // Make an initial call to GetAdaptersInfo to get
00170         // the necessary size into len
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 } // namespace Poco


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19