aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/src/Environment_WINCE.cpp
blob: afa59b68d35783888dab7ebfd4cf182c8efed16c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// Environment_WINCE.cpp
//
// Library: Foundation
// Package: Core
// Module:  Environment
//
// Copyright (c) 2009-2010, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Environment_WINCE.h"
#include "Poco/Exception.h"
#include "Poco/UnicodeConverter.h"
#include "Poco/String.h"
#include "Poco/Path.h"
#include "Poco/NumberFormatter.h"
#include <sstream>
#include <cstring>
#include <windows.h>
#include <iphlpapi.h>


namespace Poco {


const std::string EnvironmentImpl::TEMP("TEMP");
const std::string EnvironmentImpl::TMP("TMP");
const std::string EnvironmentImpl::HOMEPATH("HOMEPATH");
const std::string EnvironmentImpl::COMPUTERNAME("COMPUTERNAME");
const std::string EnvironmentImpl::OS("OS");
const std::string EnvironmentImpl::NUMBER_OF_PROCESSORS("NUMBER_OF_PROCESSORS");
const std::string EnvironmentImpl::PROCESSOR_ARCHITECTURE("PROCESSOR_ARCHITECTURE");


std::string EnvironmentImpl::getImpl(const std::string& name)
{
	std::string value;
	if (!envVar(name, &value)) throw NotFoundException(name);
	return value;
}


bool EnvironmentImpl::hasImpl(const std::string& name)
{
	return envVar(name, 0);
}


void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
{
	throw NotImplementedException("Cannot set environment variables on Windows CE");
}


std::string EnvironmentImpl::osNameImpl()
{
	return "Windows CE";
}


std::string EnvironmentImpl::osDisplayNameImpl()
{
	return osNameImpl();
}


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
	case PROCESSOR_ARCHITECTURE_SHX:
		return "SHX";
	case PROCESSOR_ARCHITECTURE_ARM:
		return "ARM";
	default:
		return "Unknown";
	}
}


std::string EnvironmentImpl::nodeNameImpl()
{
    HKEY hKey;
	DWORD dwDisposition;
    if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"\\Ident", 0, 0, 0, 0, 0, &hKey, &dwDisposition) != ERROR_SUCCESS) 
		throw SystemException("Cannot get node name", "registry key not found");

	std::string value;
    DWORD dwType;
    BYTE bData[1026];
    DWORD dwData = sizeof(bData);
	if (RegQueryValueExW(hKey, L"Name", 0, &dwType, bData, &dwData) == ERROR_SUCCESS)
	{
		switch (dwType) 
		{
		case REG_SZ:
			UnicodeConverter::toUTF8(reinterpret_cast<wchar_t*>(bData), value);
			break;

		default:
			RegCloseKey(hKey);
			throw SystemException("Cannot get node name", "registry value has wrong type");
		}
	}
	else
	{
	    RegCloseKey(hKey);
		throw SystemException("Cannot get node name", "registry value not found");
	}
    RegCloseKey(hKey);
	return value;
}


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);
}


unsigned EnvironmentImpl::processorCountImpl()
{
	SYSTEM_INFO si;
	GetSystemInfo(&si);
	return si.dwNumberOfProcessors;
}


bool EnvironmentImpl::envVar(const std::string& name, std::string* value)
{
	if (icompare(name, TEMP) == 0)
	{
		if (value) *value = Path::temp();
	}
	else if (icompare(name, TMP) == 0)
	{
		if (value) *value = Path::temp();
	}
	else if (icompare(name, HOMEPATH) == 0)
	{
		if (value) *value = Path::home();
	}
	else if (icompare(name, COMPUTERNAME) == 0)
	{
		if (value) *value = nodeNameImpl();
	}
	else if (icompare(name, OS) == 0)
	{
		if (value) *value = osNameImpl();
	}
	else if (icompare(name, NUMBER_OF_PROCESSORS) == 0)
	{
		if (value) *value = NumberFormatter::format(processorCountImpl());
	}
	else if (icompare(name, PROCESSOR_ARCHITECTURE) == 0)
	{
		if (value) *value = osArchitectureImpl();
	}
	else return false;
	return true;
}


} // namespace Poco