From 5c4acf88a8695e52b1d9860b4d596c5ec53d83f6 Mon Sep 17 00:00:00 2001 From: Megamouse Date: Sun, 29 Sep 2024 11:09:13 +0200 Subject: [PATCH] Windows: return bytes_written if WriteFile returns synchronously (#697) According to documentation, the system reserves the right to run WriteFile synchronously, even if FILE_FLAG_OVERLAPPED is set, so `bytes_written` needs to be taken from `WriteFile` directly in that case. --- windows/hid.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/windows/hid.c b/windows/hid.c index 35c2de04..bc676223 100644 --- a/windows/hid.c +++ b/windows/hid.c @@ -1086,7 +1086,7 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char * length = dev->output_report_length; } - res = WriteFile(dev->device_handle, buf, (DWORD) length, NULL, &dev->write_ol); + res = WriteFile(dev->device_handle, buf, (DWORD) length, &bytes_written, &dev->write_ol); if (!res) { if (GetLastError() != ERROR_IO_PENDING) { @@ -1095,6 +1095,9 @@ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char * goto end_of_function; } overlapped = TRUE; + } else { + /* WriteFile() succeeded synchronously. */ + function_result = bytes_written; } if (overlapped) {