/labo/2009.08.17/poco-1.3.5-mingw/Foundation/src/Environment_WIN32U.cpp
C++ | 209 lines | 151 code | 22 blank | 36 comment | 29 complexity | a1908d9f8db24b2fa026157fdfb07106 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception
- //
- // Environment_WIN32U.cpp
- //
- // $Id: //poco/1.3/Foundation/src/Environment_WIN32U.cpp#4 $
- //
- // Library: Foundation
- // Package: Core
- // Module: Environment
- //
- // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // Permission is hereby granted, free of charge, to any person or organization
- // obtaining a copy of the software and accompanying documentation covered by
- // this license (the "Software") to use, reproduce, display, distribute,
- // execute, and transmit the Software, and to prepare derivative works of the
- // Software, and to permit third-parties to whom the Software is furnished to
- // do so, all subject to the following:
- //
- // The copyright notices in the Software and this entire statement, including
- // the above license grant, this restriction and the following disclaimer,
- // must be included in all copies of the Software, in whole or in part, and
- // all derivative works of the Software, unless such copies or derivative
- // works are solely in the form of machine-executable object code generated by
- // a source language processor.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
- // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
- // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
- // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- // DEALINGS IN THE SOFTWARE.
- //
-
-
- #include "Poco/Environment_WIN32U.h"
- #include "Poco/Exception.h"
- #include "Poco/UnicodeConverter.h"
- #include "Poco/Buffer.h"
- #include <sstream>
- #include <cstring>
- #include "Poco/UnWindows.h"
- #include <iphlpapi.h>
-
-
- namespace Poco {
-
-
- std::string EnvironmentImpl::getImpl(const std::string& name)
- {
- std::wstring uname;
- UnicodeConverter::toUTF16(name, uname);
- DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
- if (len == 0) throw NotFoundException(name);
- Buffer<wchar_t> buffer(len);
- GetEnvironmentVariableW(uname.c_str(), buffer.begin(), len);
- std::string result;
- UnicodeConverter::toUTF8(buffer.begin(), len - 1, result);
- return result;
- }
-
-
- bool EnvironmentImpl::hasImpl(const std::string& name)
- {
- std::wstring uname;
- UnicodeConverter::toUTF16(name, uname);
- DWORD len = GetEnvironmentVariableW(uname.c_str(), 0, 0);
- return len > 0;
- }
-
-
- void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
- {
- std::wstring uname;
- std::wstring uvalue;
- UnicodeConverter::toUTF16(name, uname);
- UnicodeConverter::toUTF16(value, uvalue);
- if (SetEnvironmentVariableW(uname.c_str(), uvalue.c_str()) == 0)
- {
- std::string msg = "cannot set environment variable: ";
- msg.append(name);
- throw SystemException(msg);
- }
- }
-
-
- std::string EnvironmentImpl::osNameImpl()
- {
- OSVERSIONINFO vi;
- vi.dwOSVersionInfoSize = sizeof(vi);
- if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information");
- switch (vi.dwPlatformId)
- {
- case VER_PLATFORM_WIN32s:
- return "Windows 3.x";
- case VER_PLATFORM_WIN32_WINDOWS:
- return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98";
- case VER_PLATFORM_WIN32_NT:
- return "Windows NT";
- default:
- return "Unknown";
- }
- }
-
-
- std::string EnvironmentImpl::osVersionImpl()
- {
- OSVERSIONINFOW vi;
- vi.dwOSVersionInfoSize = sizeof(vi);
- if (GetVersionExW(&vi) == 0) throw SystemException("Cannot get OS version information");
- std::ostringstream str;
- str << vi.dwMajorVersion << "." << vi.dwMinorVersion << " (Build " << (vi.dwBuildNumber & 0xFFFF);
- std::string version;
- UnicodeConverter::toUTF8(vi.szCSDVersion, version);
- if (!version.empty()) str << ": " << version;
- str << ")";
- return str.str();
- }
-
-
- std::string EnvironmentImpl::osArchitectureImpl()
- {
- SYSTEM_INFO si;
- GetSystemInfo(&si);
- switch (si.wProcessorArchitecture)
- {
- case PROCESSOR_ARCHITECTURE_INTEL:
- return "IA32";
- case PROCESSOR_ARCHITECTURE_MIPS:
- return "MIPS";
- case PROCESSOR_ARCHITECTURE_ALPHA:
- return "ALPHA";
- case PROCESSOR_ARCHITECTURE_PPC:
- return "PPC";
- case PROCESSOR_ARCHITECTURE_IA64:
- return "IA64";
- #ifdef PROCESSOR_ARCHITECTURE_IA32_ON_WIN64
- case PROCESSOR_ARCHITECTURE_IA32_ON_WIN64:
- return "IA64/32";
- #endif
- #ifdef PROCESSOR_ARCHITECTURE_AMD64
- case PROCESSOR_ARCHITECTURE_AMD64:
- return "AMD64";
- #endif
- default:
- return "Unknown";
- }
- }
-
-
- std::string EnvironmentImpl::nodeNameImpl()
- {
- wchar_t name[MAX_COMPUTERNAME_LENGTH + 1];
- DWORD size = MAX_COMPUTERNAME_LENGTH + 1;
- if (GetComputerNameW(name, &size) == 0) throw SystemException("Cannot get computer name");
- std::string result;
- UnicodeConverter::toUTF8(name, result);
- return result;
- }
-
-
- void EnvironmentImpl::nodeIdImpl(NodeId& id)
- {
- PIP_ADAPTER_INFO pAdapterInfo;
- PIP_ADAPTER_INFO pAdapter = 0;
- ULONG len = sizeof(IP_ADAPTER_INFO);
- pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
- // Make an initial call to GetAdaptersInfo to get
- // the necessary size into len
- DWORD rc = GetAdaptersInfo(pAdapterInfo, &len);
- if (rc == ERROR_BUFFER_OVERFLOW)
- {
- delete [] reinterpret_cast<char*>(pAdapterInfo);
- pAdapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(new char[len]);
- }
- else if (rc != ERROR_SUCCESS)
- {
- throw SystemException("cannot get network adapter list");
- }
- try
- {
- bool found = false;
- if (GetAdaptersInfo(pAdapterInfo, &len) == NO_ERROR)
- {
- pAdapter = pAdapterInfo;
- while (pAdapter && !found)
- {
- if (pAdapter->Type == MIB_IF_TYPE_ETHERNET && pAdapter->AddressLength == sizeof(id))
- {
- std::memcpy(&id, pAdapter->Address, pAdapter->AddressLength);
- found = true;
- }
- pAdapter = pAdapter->Next;
- }
- }
- else throw SystemException("cannot get network adapter list");
- if (!found) throw SystemException("no Ethernet adapter found");
- }
- catch (Exception&)
- {
- delete [] reinterpret_cast<char*>(pAdapterInfo);
- throw;
- }
- delete [] reinterpret_cast<char*>(pAdapterInfo);
- }
-
-
- } // namespace Poco