From fb87e7c19336cfad9d86aa7bcc68516b256a85a5 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Thu, 7 Sep 2017 16:16:34 -0700 Subject: [PATCH] ReadFile and WriteFile really want to report how many bytes were read/written. --- src/connection_win.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/connection_win.cpp b/src/connection_win.cpp index 3159c7e..fa80570 100644 --- a/src/connection_win.cpp +++ b/src/connection_win.cpp @@ -5,6 +5,7 @@ #define NOSERVICE #define NOIME #include +#include int GetProcessId() { @@ -75,22 +76,44 @@ bool BaseConnection::Write(const void* data, size_t length) return true; } auto self = reinterpret_cast(this); + assert(self); + if (!self) { + return false; + } if (self->pipe == INVALID_HANDLE_VALUE) { return false; } - return ::WriteFile(self->pipe, data, (DWORD)length, nullptr, nullptr) == TRUE; + assert(data); + if (!data) { + return false; + } + const DWORD bytesLength = (DWORD)length; + DWORD bytesWritten = 0; + return ::WriteFile(self->pipe, data, bytesLength, &bytesWritten, nullptr) == TRUE && + bytesWritten == bytesLength; } bool BaseConnection::Read(void* data, size_t length) { + assert(data); + if (!data) { + return false; + } auto self = reinterpret_cast(this); + assert(self); + if (!self) { + return false; + } if (self->pipe == INVALID_HANDLE_VALUE) { return false; } DWORD bytesAvailable = 0; if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) { if (bytesAvailable >= length) { - if (::ReadFile(self->pipe, data, (DWORD)length, nullptr, nullptr) == TRUE) { + DWORD bytesToRead = (DWORD)length; + DWORD bytesRead = 0; + if (::ReadFile(self->pipe, data, bytesToRead, &bytesRead, nullptr) == TRUE) { + assert(bytesToRead == bytesRead); return true; } else { @@ -102,4 +125,4 @@ bool BaseConnection::Read(void* data, size_t length) Close(); } return false; -} +} \ No newline at end of file