From e3cd447a40c81c65ce3e40c10f680e64f6fa1895 Mon Sep 17 00:00:00 2001 From: amullins Date: Thu, 8 Jan 2015 16:29:21 -0600 Subject: [PATCH] Get and use CancelIoEx if available --- windows/hid.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/windows/hid.c b/windows/hid.c index 427bcd1c..0595aa21 100755 --- a/windows/hid.c +++ b/windows/hid.c @@ -124,6 +124,8 @@ extern "C" { static BOOLEAN initialized = FALSE; #endif /* HIDAPI_USE_DDK */ + static HMODULE kernel_lib_handle = NULL; + struct hid_device_ { HANDLE device_handle; BOOL blocking; @@ -257,6 +259,11 @@ int HID_API_EXPORT hid_exit(void) if (lib_handle) FreeLibrary(lib_handle); lib_handle = NULL; + + if (kernel_lib_handle) + FreeLibrary(kernel_lib_handle); + kernel_lib_handle = NULL; + initialized = FALSE; #endif return 0; @@ -801,11 +808,31 @@ int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned #endif } +typedef BOOL (WINAPI *CancolIoEx_)(HANDLE, LPOVERLAPPED); + void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev) { + static CancolIoEx_ CancelIoEx = NULL; + static BOOL didTryToLoadKernelLib = FALSE; + if (!dev) return; - CancelIo(dev->device_handle); + + if (!kernel_lib_handle && !didTryToLoadKernelLib) { + kernel_lib_handle = LoadLibraryA("kernel32.dll"); + didTryToLoadKernelLib = TRUE; + if (kernel_lib_handle) { + CancelIoEx = GetProcAddress(kernel_lib_handle, "CancelIoEx"); + } + } + + if (CancelIoEx) { + CancelIoEx(dev->device_handle, &dev->ol); + } + else { + CancelIo(dev->device_handle); + } + free_hid_device(dev); }