}
#endif
+/*
+ * Dynamically loads a DLL from the Windows system directory. Unlike the
+ * LoadLibraryA() function, this function will not search through any
+ * directories to try and find the library.
+ */
+HMODULE load_system_library(struct libusb_context *ctx, const char *name)
+{
+ char library_path[MAX_PATH];
+ char *filename_start;
+ UINT length;
+
+ length = GetSystemDirectoryA(library_path, sizeof(library_path));
+ if ((length == 0) || (length >= (UINT)sizeof(library_path))) {
+ usbi_err(ctx, "program assertion failed - could not get system directory");
+ return NULL;
+ }
+
+ filename_start = library_path + length;
+ // Append '\' + name + ".dll" + NUL
+ length += 1 + strlen(name) + 4 + 1;
+ if (length >= (UINT)sizeof(library_path)) {
+ usbi_err(ctx, "program assertion failed - library path buffer overflow");
+ return NULL;
+ }
+
+ sprintf(filename_start, "\\%s.dll", name);
+ return LoadLibraryA(library_path);
+}
+
/* Hash table functions - modified From glibc 2.3.2:
[Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
[Knuth] The Art of Computer Programming, part 3 (6.4) */
* API macros - leveraged from libusb-win32 1.x
*/
#define DLL_STRINGIFY(s) #s
-#define DLL_LOAD_LIBRARY(name) LoadLibraryA(DLL_STRINGIFY(name))
/*
* Macros for handling DLL themselves
*/
#define DLL_HANDLE_NAME(name) __dll_##name##_handle
-#define DLL_DECLARE_HANDLE(name) \
+#define DLL_DECLARE_HANDLE(name) \
static HMODULE DLL_HANDLE_NAME(name)
-#define DLL_GET_HANDLE(name) \
- do { \
- DLL_HANDLE_NAME(name) = DLL_LOAD_LIBRARY(name); \
- if (!DLL_HANDLE_NAME(name)) \
- return false; \
+#define DLL_GET_HANDLE(ctx, name) \
+ do { \
+ DLL_HANDLE_NAME(name) = load_system_library(ctx, \
+ DLL_STRINGIFY(name)); \
+ if (!DLL_HANDLE_NAME(name)) \
+ return false; \
} while (0)
-#define DLL_FREE_HANDLE(name) \
- do { \
- if (DLL_HANDLE_NAME(name)) { \
- FreeLibrary(DLL_HANDLE_NAME(name)); \
- DLL_HANDLE_NAME(name) = NULL; \
- } \
+#define DLL_FREE_HANDLE(name) \
+ do { \
+ if (DLL_HANDLE_NAME(name)) { \
+ FreeLibrary(DLL_HANDLE_NAME(name)); \
+ DLL_HANDLE_NAME(name) = NULL; \
+ } \
} while (0)
/*
extern const struct windows_backend usbdk_backend;
extern const struct windows_backend winusb_backend;
+HMODULE load_system_library(struct libusb_context *ctx, const char *name);
unsigned long htab_hash(const char *str);
enum libusb_transfer_status usbd_status_to_libusb_transfer_status(USBD_STATUS status);
void windows_force_sync_completion(struct usbi_transfer *itransfer, ULONG size);
static int load_usbdk_helper_dll(struct libusb_context *ctx)
{
- usbdk_helper.module = LoadLibraryA("UsbDkHelper");
+ usbdk_helper.module = load_system_library(ctx, "UsbDkHelper");
if (usbdk_helper.module == NULL) {
usbi_err(ctx, "Failed to load UsbDkHelper.dll: %s", windows_error_str(0));
return LIBUSB_ERROR_NOT_FOUND;
SC_HANDLE serviceHandle;
HMODULE h;
- h = LoadLibraryA("Advapi32");
+ h = load_system_library(ctx, "Advapi32");
if (h == NULL) {
usbi_warn(ctx, "failed to open Advapi32\n");
return LIBUSB_ERROR_OTHER;
/*
* Cfgmgr32, AdvAPI32, OLE32 and SetupAPI DLL functions
*/
-static bool init_dlls(void)
+static bool init_dlls(struct libusb_context *ctx)
{
- DLL_GET_HANDLE(Cfgmgr32);
+ DLL_GET_HANDLE(ctx, Cfgmgr32);
DLL_LOAD_FUNC(Cfgmgr32, CM_Get_Parent, true);
DLL_LOAD_FUNC(Cfgmgr32, CM_Get_Child, true);
// Prefixed to avoid conflict with header files
- DLL_GET_HANDLE(AdvAPI32);
+ DLL_GET_HANDLE(ctx, AdvAPI32);
DLL_LOAD_FUNC_PREFIXED(AdvAPI32, p, RegQueryValueExW, true);
DLL_LOAD_FUNC_PREFIXED(AdvAPI32, p, RegCloseKey, true);
- DLL_GET_HANDLE(OLE32);
+ DLL_GET_HANDLE(ctx, OLE32);
DLL_LOAD_FUNC_PREFIXED(OLE32, p, IIDFromString, true);
- DLL_GET_HANDLE(SetupAPI);
+ DLL_GET_HANDLE(ctx, SetupAPI);
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiGetClassDevsA, true);
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiEnumDeviceInfo, true);
DLL_LOAD_FUNC_PREFIXED(SetupAPI, p, SetupDiEnumDeviceInterfaces, true);
int i;
// Load DLL imports
- if (!init_dlls()) {
+ if (!init_dlls(ctx)) {
usbi_err(ctx, "could not resolve DLL functions");
return LIBUSB_ERROR_OTHER;
}
{
HMODULE hWinUSB, hlibusbK;
- hWinUSB = LoadLibraryA("WinUSB");
+ hWinUSB = load_system_library(ctx, "WinUSB");
if (hWinUSB != NULL) {
WinUSB_Set(hWinUSB, AbortPipe, true);
WinUSB_Set(hWinUSB, ControlTransfer, true);
usbi_info(ctx, "WinUSB DLL is not available");
}
- hlibusbK = LoadLibraryA("libusbK");
+ hlibusbK = load_system_library(ctx, "libusbK");
if (hlibusbK != NULL) {
LibK_GetVersion_t pLibK_GetVersion;
LibK_GetProcAddress_t pLibK_GetProcAddress;
*/
static bool hid_init(struct libusb_context *ctx)
{
- UNUSED(ctx);
-
- DLL_GET_HANDLE(hid);
+ DLL_GET_HANDLE(ctx, hid);
DLL_LOAD_FUNC(hid, HidD_GetAttributes, true);
DLL_LOAD_FUNC(hid, HidD_GetHidGuid, true);
-#define LIBUSB_NANO 11568
+#define LIBUSB_NANO 11569