@@ -138,9 +138,9 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
138138 // Use process ID as a unique ID for this process at this time.
139139 base_name.append (std::to_string (GetCurrentProcessId ()));
140140 const std::string in_name = base_name + " \\ IN" ;
141- child_std_IN_Rd = CreateNamedPipe (
141+ child_std_IN_Wr = CreateNamedPipe (
142142 in_name.c_str (),
143- PIPE_ACCESS_INBOUND , // Reading for us
143+ PIPE_ACCESS_OUTBOUND , // Writing for us
144144 PIPE_TYPE_BYTE | PIPE_NOWAIT, // Bytes and non-blocking
145145 PIPE_UNLIMITED_INSTANCES, // Probably doesn't matter
146146 BUFSIZE,
@@ -156,9 +156,9 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
156156 throw system_exceptiont (" Input pipe creation failed for child_std_IN_Rd" );
157157 }
158158 // Connect to the other side of the pipe
159- child_std_IN_Wr = CreateFileA (
159+ child_std_IN_Rd = CreateFile (
160160 in_name.c_str (),
161- GENERIC_WRITE , // Write side
161+ GENERIC_READ , // Read side
162162 FILE_SHARE_READ | FILE_SHARE_WRITE, // Shared read/write
163163 &sec_attr, // Need this for inherit
164164 OPEN_EXISTING, // Opening other end
@@ -168,7 +168,8 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
168168 {
169169 throw system_exceptiont (" Input pipe creation failed for child_std_IN_Wr" );
170170 }
171- if (!SetHandleInformation (child_std_IN_Rd, HANDLE_FLAG_INHERIT, 0 ))
171+ if (!SetHandleInformation (
172+ child_std_IN_Rd, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
172173 {
173174 throw system_exceptiont (
174175 " Input pipe creation failed on SetHandleInformation" );
@@ -187,7 +188,7 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
187188 {
188189 throw system_exceptiont (" Output pipe creation failed for child_std_OUT_Rd" );
189190 }
190- child_std_OUT_Wr = CreateFileA (
191+ child_std_OUT_Wr = CreateFile (
191192 out_name.c_str (),
192193 GENERIC_WRITE, // Write side
193194 FILE_SHARE_READ | FILE_SHARE_WRITE, // Shared read/write
@@ -233,6 +234,8 @@ piped_processt::piped_processt(const std::vector<std::string> &commandvec)
233234 // recognize that the child process has ended (but maybe we don't care).
234235 CloseHandle (child_std_OUT_Wr);
235236 CloseHandle (child_std_IN_Rd);
237+ if (!success)
238+ throw system_exceptiont (" Process creation failed." );
236239# else
237240
238241 if (pipe (pipe_input) == -1 )
@@ -335,18 +338,25 @@ piped_processt::~piped_processt()
335338# endif
336339}
337340
341+ NODISCARD
338342piped_processt::send_responset piped_processt::send (const std::string &message)
339343{
340344 if (process_state != statet::RUNNING)
341345 {
342346 return send_responset::ERRORED;
343347 }
344348#ifdef _WIN32
345- if (!WriteFile (child_std_IN_Wr, message.c_str (), message.size (), NULL , NULL ))
349+ const auto message_size = narrow<DWORD>(message.size ());
350+ DWORD bytes_written = 0 ;
351+ if (!WriteFile (
352+ child_std_IN_Wr, message.c_str (), message_size, &bytes_written, NULL ))
346353 {
347354 // Error handling with GetLastError ?
348355 return send_responset::FAILED;
349356 }
357+ INVARIANT (
358+ message_size == bytes_written,
359+ " Number of bytes written to sub process must match message size." );
350360#else
351361 // send message to solver process
352362 int send_status = fputs (message.c_str (), command_stream);
@@ -415,15 +425,22 @@ bool piped_processt::can_receive(optionalt<std::size_t> wait_time)
415425 const int timeout = wait_time ? narrow<int >(*wait_time) : -1 ;
416426#ifdef _WIN32
417427 int waited_time = 0 ;
418- // The next four need to be initialised for compiler warnings
419- DWORD buffer = 0 ;
420- LPDWORD nbytes = 0 ;
421- LPDWORD rbytes = 0 ;
422- LPDWORD rmbytes = 0 ;
428+ DWORD total_bytes_available = 0 ;
423429 while (timeout < 0 || waited_time >= timeout)
424430 {
425- PeekNamedPipe (child_std_OUT_Rd, &buffer, 1 , nbytes, rbytes, rmbytes);
426- if (buffer != 0 )
431+ const LPVOID lpBuffer = nullptr ;
432+ const DWORD nBufferSize = 0 ;
433+ const LPDWORD lpBytesRead = nullptr ;
434+ const LPDWORD lpTotalBytesAvail = &total_bytes_available;
435+ const LPDWORD lpBytesLeftThisMessage = nullptr ;
436+ PeekNamedPipe (
437+ child_std_OUT_Rd,
438+ lpBuffer,
439+ nBufferSize,
440+ lpBytesRead,
441+ lpTotalBytesAvail,
442+ lpBytesLeftThisMessage);
443+ if (total_bytes_available > 0 )
427444 {
428445 return true ;
429446 }
0 commit comments