From 8b46e1c088167eb86b1712765896e2f17d70d148 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Mon, 30 Dec 2013 21:32:28 +0000 Subject: [PATCH] Windows: Add SetupAPI error handling * http://msdn.microsoft.com/en-us/library/windows/hardware/ff545011.aspx states that SetupAPI errors must be be converted before passing it to FormatMessage(). * Use our own implementation of HRESULT_FROM_SETUPAPI to avoid defining a new function call. * Issue and original fix suggested by Matthias Bolte * Closes #166 --- libusb/os/windows_usb.c | 14 ++++++++++++++ libusb/version_nano.h | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c index aa4c0f4..5abadc1 100644 --- a/libusb/os/windows_usb.c +++ b/libusb/os/windows_usb.c @@ -158,6 +158,20 @@ static char err_string[ERR_BUFFER_SIZE]; safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%u] ", error_code); + // Translate codes returned by SetupAPI. The ones we are dealing with are either + // in 0x0000xxxx or 0xE000xxxx and can be distinguished from standard error codes. + // See http://msdn.microsoft.com/en-us/library/windows/hardware/ff545011.aspx + switch (error_code & 0xE0000000) { + case 0: + error_code = HRESULT_FROM_WIN32(error_code); // Still leaves ERROR_SUCCESS unmodified + break; + case 0xE0000000: + error_code = 0x80000000 | (FACILITY_SETUPAPI << 16) | (error_code & 0x0000FFFF); + break; + default: + break; + } + size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[safe_strlen(err_string)], ERR_BUFFER_SIZE - (DWORD)safe_strlen(err_string), NULL); diff --git a/libusb/version_nano.h b/libusb/version_nano.h index c7f218d..9b72a0c 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10856 +#define LIBUSB_NANO 10857 -- 2.7.4