source: XMLIO_V2/external/src/POCO/Foundation/Environment_WIN32U.hpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

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