WinCE: Add internal build script for WinCE binaries
[libusbx.git] / msvc / missing.c
blob85d9d6f3449585bc9eaf4888c5b02c24531b8a01
1 /*
2 * Source file for missing WinCE functionality
3 * Copyright © 2012 RealVNC Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "missing.h"
22 #include <config.h>
23 #include <libusbi.h>
25 #include <windows.h>
27 // The registry path to store environment variables
28 #define ENVIRONMENT_REG_PATH _T("Software\\libusb\\environment")
30 /* Workaround getenv not being available on WinCE.
31 * Instead look in HKLM\Software\libusb\environment */
32 char *getenv(const char *name)
34 static char value[MAX_PATH];
35 TCHAR wValue[MAX_PATH];
36 WCHAR wName[MAX_PATH];
37 DWORD dwType, dwData;
38 HKEY hkey;
39 LONG rc;
41 if (!name)
42 return NULL;
44 if (MultiByteToWideChar(CP_UTF8, 0, name, -1, wName, MAX_PATH) <= 0) {
45 usbi_dbg("Failed to convert environment variable name to wide string");
46 return NULL;
48 wName[MAX_PATH - 1] = 0; // Be sure it's NUL terminated
50 rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ENVIRONMENT_REG_PATH, 0, KEY_QUERY_VALUE, &hkey);
51 if (rc != ERROR_SUCCESS) {
52 usbi_dbg("Failed to open registry key for getenv with error %d", rc);
53 return NULL;
56 // Attempt to read the key
57 dwData = sizeof(wValue);
58 rc = RegQueryValueEx(hkey, wName, NULL, &dwType,
59 (LPBYTE)&wValue, &dwData);
60 RegCloseKey(hkey);
61 if (rc != ERROR_SUCCESS) {
62 usbi_dbg("Failed to read registry key value for getenv with error %d", rc);
63 return NULL;
65 if (dwType != REG_SZ) {
66 usbi_dbg("Registry value was of type %d instead of REG_SZ", dwType);
67 return NULL;
70 // Success in reading the key, convert from WCHAR to char
71 if (WideCharToMultiByte(CP_UTF8, 0,
72 wValue, dwData / sizeof(*wValue),
73 value, MAX_PATH,
74 NULL, NULL) <= 0) {
75 usbi_dbg("Failed to convert environment variable value to narrow string");
76 return NULL;
78 value[MAX_PATH - 1] = 0; // Be sure it's NUL terminated
79 return value;