Environment_WIN32.cpp
Go to the documentation of this file.
00001 //
00002 // Environment_WIN32.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/Environment_WIN32.cpp#5 $
00005 //
00006 // Library: Foundation
00007 // Package: Core
00008 // Module:  Environment
00009 //
00010 // Copyright (c) 2004-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_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         // Make an initial call to GetAdaptersInfo to get
00156         // the necessary size into len
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 } // namespace Poco


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