diff options
author | Pete Batard <pbatard@gmail.com> | 2010-10-26 16:31:13 +0100 |
---|---|---|
committer | Pete Batard <pbatard@gmail.com> | 2010-10-26 16:31:13 +0100 |
commit | 5a2aa170f3a5ba651c76542ea6e65d4d78bcf3b6 (patch) | |
tree | f057952cff41a42889980d025be050e254905c0e /libusb/os | |
parent | 281d8009bec0a46c28b4717c2c14630172dd2593 (diff) | |
download | libusb-5a2aa170f3a5ba651c76542ea6e65d4d78bcf3b6.tar.gz |
removed SetupAPI, AdvAPI32 and OLE32 dependencies
* this ensures that libusb dependent applications only need
to link with libusb on Windows
Diffstat (limited to 'libusb/os')
-rw-r--r-- | libusb/os/windows_usb.c | 58 | ||||
-rw-r--r-- | libusb/os/windows_usb.h | 36 |
2 files changed, 60 insertions, 34 deletions
diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c index 1ed72d4..38a149b 100644 --- a/libusb/os/windows_usb.c +++ b/libusb/os/windows_usb.c @@ -41,7 +41,7 @@ #include <process.h> #include <stdio.h> #include <inttypes.h> -#include <objbase.h> // for string to GUID conv. requires libole32.a +#include <objbase.h> #include <winioctl.h> #include <libusbi.h> @@ -257,15 +257,23 @@ static char* sanitize_path(const char* path) } /* - * Cfgmgr32 API functions + * Cfgmgr32, OLE32 and SetupAPI DLL functions */ -static int Cfgmgr32_init(void) +static int init_dlls(void) { DLL_LOAD(Cfgmgr32.dll, CM_Get_Parent, TRUE); DLL_LOAD(Cfgmgr32.dll, CM_Get_Child, TRUE); DLL_LOAD(Cfgmgr32.dll, CM_Get_Sibling, TRUE); DLL_LOAD(Cfgmgr32.dll, CM_Get_Device_IDA, TRUE); - + // Prefixed to avoid conflict with header files + DLL_LOAD_PREFIXED(OLE32.dll, p, CLSIDFromString, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetClassDevsA, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiEnumDeviceInfo, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiEnumDeviceInterfaces, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetDeviceInterfaceDetailA, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiDestroyDeviceInfoList, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiOpenDevRegKey, TRUE); + DLL_LOAD_PREFIXED(SetupAPI.dll, p, SetupDiGetDeviceRegistryPropertyA, TRUE); return LIBUSB_SUCCESS; } @@ -286,19 +294,19 @@ bool get_devinfo_data(struct libusb_context *ctx, HDEVINFO *dev_info, SP_DEVINFO_DATA *dev_info_data, unsigned _index) { if (_index <= 0) { - *dev_info = SetupDiGetClassDevsA(NULL, "USB", NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); + *dev_info = pSetupDiGetClassDevsA(NULL, "USB", NULL, DIGCF_PRESENT|DIGCF_ALLCLASSES); if (*dev_info == INVALID_HANDLE_VALUE) { return false; } } dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA); - if (!SetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { + if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { if (GetLastError() != ERROR_NO_MORE_ITEMS) { usbi_err(ctx, "Could not obtain device info data for index %u: %s", _index, windows_error_str(0)); } - SetupDiDestroyDeviceInfoList(*dev_info); + pSetupDiDestroyDeviceInfoList(*dev_info); *dev_info = INVALID_HANDLE_VALUE; return false; } @@ -326,35 +334,35 @@ SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_context * DWORD size; if (_index <= 0) { - *dev_info = SetupDiGetClassDevs(guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); + *dev_info = pSetupDiGetClassDevsA(guid, NULL, NULL, DIGCF_PRESENT|DIGCF_DEVICEINTERFACE); } if (dev_info_data != NULL) { dev_info_data->cbSize = sizeof(SP_DEVINFO_DATA); - if (!SetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { + if (!pSetupDiEnumDeviceInfo(*dev_info, _index, dev_info_data)) { if (GetLastError() != ERROR_NO_MORE_ITEMS) { usbi_err(ctx, "Could not obtain device info data for index %u: %s", _index, windows_error_str(0)); } - SetupDiDestroyDeviceInfoList(*dev_info); + pSetupDiDestroyDeviceInfoList(*dev_info); *dev_info = INVALID_HANDLE_VALUE; return NULL; } } dev_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); - if (!SetupDiEnumDeviceInterfaces(*dev_info, NULL, guid, _index, &dev_interface_data)) { + if (!pSetupDiEnumDeviceInterfaces(*dev_info, NULL, guid, _index, &dev_interface_data)) { if (GetLastError() != ERROR_NO_MORE_ITEMS) { usbi_err(ctx, "Could not obtain interface data for index %u: %s", _index, windows_error_str(0)); } - SetupDiDestroyDeviceInfoList(*dev_info); + pSetupDiDestroyDeviceInfoList(*dev_info); *dev_info = INVALID_HANDLE_VALUE; return NULL; } // Read interface data (dummy + actual) to access the device path - if (!SetupDiGetDeviceInterfaceDetail(*dev_info, &dev_interface_data, NULL, 0, &size, NULL)) { + if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, NULL, 0, &size, NULL)) { // The dummy call should fail with ERROR_INSUFFICIENT_BUFFER if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { usbi_err(ctx, "could not access interface data (dummy) for index %u: %s", @@ -371,8 +379,8 @@ SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_context * goto err_exit; } - dev_interface_details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); - if (!SetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, + dev_interface_details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A); + if (!pSetupDiGetDeviceInterfaceDetailA(*dev_info, &dev_interface_data, dev_interface_details, size, &size, NULL)) { usbi_err(ctx, "could not access interface data (actual) for index %u: %s", _index, windows_error_str(0)); @@ -381,7 +389,7 @@ SP_DEVICE_INTERFACE_DETAIL_DATA_A *get_interface_details(struct libusb_context * return dev_interface_details; err_exit: - SetupDiDestroyDeviceInfoList(*dev_info); + pSetupDiDestroyDeviceInfoList(*dev_info); *dev_info = INVALID_HANDLE_VALUE; return NULL; } @@ -819,9 +827,9 @@ static int windows_init(struct libusb_context *ctx) // Initialize pollable file descriptors init_polling(); - // Load missing CFGMGR32.DLL imports - if (Cfgmgr32_init() != LIBUSB_SUCCESS) { - usbi_err(ctx, "could not resolve Cfgmgr32.dll functions"); + // Load DLL imports + if (init_dlls() != LIBUSB_SUCCESS) { + usbi_err(ctx, "could not resolve DLL functions"); return LIBUSB_ERROR_NOT_FOUND; } @@ -1127,7 +1135,7 @@ static uint8_t get_api_type(struct libusb_context *ctx, // Check the service & filter names to know the API we should use for (k=0; k<3; k++) { - if (SetupDiGetDeviceRegistryPropertyA(*dev_info, dev_info_data, lookup[k].reg_prop, + if (pSetupDiGetDeviceRegistryPropertyA(*dev_info, dev_info_data, lookup[k].reg_prop, ®_type, (BYTE*)lookup[k].list, MAX_KEY_LENGTH, &size)) { // Turn the REG_SZ SPDRP_SERVICE into REG_MULTI_SZ if (lookup[k].reg_prop == SPDRP_SERVICE) { @@ -1344,7 +1352,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered // The SPDRP_ADDRESS for USB devices is the device port number on the hub port_nr = 0; if ((pass >= HUB_PASS) && (pass <= GEN_PASS)) { - if ( (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, SPDRP_ADDRESS, + if ( (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_ADDRESS, ®_type, (BYTE*)&port_nr, 4, &size)) || (size != 4) ) { usbi_warn(ctx, "could not retrieve port number for device %s, skipping: %s", @@ -1362,13 +1370,13 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered case GEN_PASS: // We use the GEN pass to detect driverless devices... size = sizeof(strbuf); - if (!SetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_DRIVER, + if (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_DRIVER, ®_type, (BYTE*)strbuf, size, &size)) { usbi_info(ctx, "The following device has no driver: '%s'", dev_id_path); usbi_info(ctx, "libusb will not be able to access it."); } // ...and to add the additional device interface GUIDs - key = SetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); + key = pSetupDiOpenDevRegKey(dev_info, &dev_info_data, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ); if (key != INVALID_HANDLE_VALUE) { size = sizeof(guid_string_w); s = RegQueryValueExW(key, L"DeviceInterfaceGUIDs", NULL, ®_type, @@ -1381,7 +1389,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered LOOP_BREAK(LIBUSB_ERROR_OVERFLOW); } if_guid = calloc(1, sizeof(GUID)); - CLSIDFromString(guid_string_w, if_guid); + pCLSIDFromString(guid_string_w, if_guid); guid[nb_guids++] = if_guid; usbi_dbg("extra GUID: %s", guid_to_string(if_guid)); } @@ -1392,7 +1400,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered break; default: // DEV_PASS or EXT_PASS // Get the API type (after checking that the driver installation is OK) - if ( (!SetupDiGetDeviceRegistryProperty(dev_info, &dev_info_data, SPDRP_INSTALL_STATE, + if ( (!pSetupDiGetDeviceRegistryPropertyA(dev_info, &dev_info_data, SPDRP_INSTALL_STATE, ®_type, (BYTE*)&install_state, 4, &size)) || (size != 4) ){ usbi_warn(ctx, "could not detect installation state of driver for '%s': %s", diff --git a/libusb/os/windows_usb.h b/libusb/os/windows_usb.h index 80e3dec..133ef73 100644 --- a/libusb/os/windows_usb.h +++ b/libusb/os/windows_usb.h @@ -327,11 +327,11 @@ struct driver_lookup { /* * API macros - from libusb-win32 1.x */ -#define DLL_DECLARE(api, ret, name, args) \ +#define DLL_DECLARE_PREFIXED(api, ret, prefix, name, args) \ typedef ret (api * __dll_##name##_t)args; \ - static __dll_##name##_t name = NULL + static __dll_##name##_t prefix##name = NULL -#define DLL_LOAD(dll, name, ret_on_failure) \ +#define DLL_LOAD_PREFIXED(dll, prefix, name, ret_on_failure) \ do { \ HMODULE h = GetModuleHandleA(#dll); \ if (!h) \ @@ -340,16 +340,34 @@ struct driver_lookup { if (ret_on_failure) { return LIBUSB_ERROR_NOT_FOUND; }\ else { break; } \ } \ - name = (__dll_##name##_t)GetProcAddress(h, #name); \ - if (name) break; \ - name = (__dll_##name##_t)GetProcAddress(h, #name "A"); \ - if (name) break; \ - name = (__dll_##name##_t)GetProcAddress(h, #name "W"); \ - if (name) break; \ + prefix##name = (__dll_##name##_t)GetProcAddress(h, #name);\ + if (prefix##name) break; \ + prefix##name = (__dll_##name##_t)GetProcAddress(h, #name "A");\ + if (prefix##name) break; \ + prefix##name = (__dll_##name##_t)GetProcAddress(h, #name "W");\ + if (prefix##name) break; \ if(ret_on_failure) \ return LIBUSB_ERROR_NOT_FOUND; \ } while(0) +#define DLL_DECLARE(api, ret, name, args) DLL_DECLARE_PREFIXED(api, ret, , name, args) +#define DLL_LOAD(dll, name, ret_on_failure) DLL_LOAD_PREFIXED(dll, , name, ret_on_failure) + +/* OLE32 dependency */ +DLL_DECLARE_PREFIXED(WINAPI, HRESULT, p, CLSIDFromString, (LPCOLESTR, LPCLSID)); + +/* SetupAPI dependencies */ +DLL_DECLARE_PREFIXED(WINAPI, HDEVINFO, p, SetupDiGetClassDevsA, (const GUID*, PCSTR, HWND, DWORD)); +DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiEnumDeviceInfo, (HDEVINFO, DWORD, PSP_DEVINFO_DATA)); +DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiEnumDeviceInterfaces, (HDEVINFO, PSP_DEVINFO_DATA, + const GUID*, DWORD, PSP_DEVICE_INTERFACE_DATA)); +DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceInterfaceDetailA, (HDEVINFO, PSP_DEVICE_INTERFACE_DATA, + PSP_DEVICE_INTERFACE_DETAIL_DATA_A, DWORD, PDWORD, PSP_DEVINFO_DATA)); +DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiDestroyDeviceInfoList, (HDEVINFO)); +DLL_DECLARE_PREFIXED(WINAPI, HKEY, p, SetupDiOpenDevRegKey, (HDEVINFO, PSP_DEVINFO_DATA, DWORD, DWORD, DWORD, REGSAM)); +DLL_DECLARE_PREFIXED(WINAPI, BOOL, p, SetupDiGetDeviceRegistryPropertyA, (HDEVINFO, + PSP_DEVINFO_DATA, DWORD, PDWORD, PBYTE, DWORD, PDWORD)); + /* * Windows DDK API definitions. Most of it copied from MinGW's includes |