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