X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjni%2Flibusb%2Fmsvc%2Fmissing.c;fp=app%2Fsrc%2Fmain%2Fjni%2Flibusb%2Fmsvc%2Fmissing.c;h=85d9d6f3449585bc9eaf4888c5b02c24531b8a01;hb=831cc09829bc6e18d8d0d8bb78063e89ea565ce9;hp=0000000000000000000000000000000000000000;hpb=061580c83656bf358b01a6b78fd22ae9bd497728;p=rtmpclient.git diff --git a/app/src/main/jni/libusb/msvc/missing.c b/app/src/main/jni/libusb/msvc/missing.c new file mode 100644 index 0000000..85d9d6f --- /dev/null +++ b/app/src/main/jni/libusb/msvc/missing.c @@ -0,0 +1,80 @@ +/* + * Source file for missing WinCE functionality + * Copyright © 2012 RealVNC Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "missing.h" + +#include +#include + +#include + +// The registry path to store environment variables +#define ENVIRONMENT_REG_PATH _T("Software\\libusb\\environment") + +/* Workaround getenv not being available on WinCE. + * Instead look in HKLM\Software\libusb\environment */ +char *getenv(const char *name) +{ + static char value[MAX_PATH]; + TCHAR wValue[MAX_PATH]; + WCHAR wName[MAX_PATH]; + DWORD dwType, dwData; + HKEY hkey; + LONG rc; + + if (!name) + return NULL; + + if (MultiByteToWideChar(CP_UTF8, 0, name, -1, wName, MAX_PATH) <= 0) { + usbi_dbg("Failed to convert environment variable name to wide string"); + return NULL; + } + wName[MAX_PATH - 1] = 0; // Be sure it's NUL terminated + + rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ENVIRONMENT_REG_PATH, 0, KEY_QUERY_VALUE, &hkey); + if (rc != ERROR_SUCCESS) { + usbi_dbg("Failed to open registry key for getenv with error %d", rc); + return NULL; + } + + // Attempt to read the key + dwData = sizeof(wValue); + rc = RegQueryValueEx(hkey, wName, NULL, &dwType, + (LPBYTE)&wValue, &dwData); + RegCloseKey(hkey); + if (rc != ERROR_SUCCESS) { + usbi_dbg("Failed to read registry key value for getenv with error %d", rc); + return NULL; + } + if (dwType != REG_SZ) { + usbi_dbg("Registry value was of type %d instead of REG_SZ", dwType); + return NULL; + } + + // Success in reading the key, convert from WCHAR to char + if (WideCharToMultiByte(CP_UTF8, 0, + wValue, dwData / sizeof(*wValue), + value, MAX_PATH, + NULL, NULL) <= 0) { + usbi_dbg("Failed to convert environment variable value to narrow string"); + return NULL; + } + value[MAX_PATH - 1] = 0; // Be sure it's NUL terminated + return value; +}