From 1e1463bfdf292a76c0b2bb7b946da70ac35616bb Mon Sep 17 00:00:00 2001 From: Adriano Raiano Date: Wed, 13 Mar 2013 08:58:30 +0100 Subject: [PATCH] fixes Errors when unplug device on windows Issue #95 --- src/serialport_win.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/serialport_win.cpp b/src/serialport_win.cpp index 7835670..1cc5936 100644 --- a/src/serialport_win.cpp +++ b/src/serialport_win.cpp @@ -10,6 +10,10 @@ #define MAX_BUFFER_SIZE 1000 +// Declare type of pointer to CancelIoEx function +typedef BOOL (WINAPI *CancelIoExType)(HANDLE hFile, LPOVERLAPPED lpOverlapped); + + std::list g_closingHandles; int bufferSize; void ErrorCodeToString(const char* prefix, int errorCode, char *errorStr) { @@ -310,6 +314,17 @@ void EIO_Close(uv_work_t* req) { CloseBaton* data = static_cast(req->data); g_closingHandles.push_back(data->fd); + + HMODULE hKernel32 = LoadLibrary("kernel32.dll"); + // Look up function address + CancelIoExType pCancelIoEx = (CancelIoExType)GetProcAddress(hKernel32, "CancelIoEx"); + // Do something with it + if (pCancelIoEx) + { + // Function exists so call it + // Cancel all pending IO Requests for the current device + pCancelIoEx((HANDLE)data->fd, NULL); + } if(!CloseHandle((HANDLE)data->fd)) { ErrorCodeToString("closing connection", GetLastError(), data->errorString); return;