@@ -20,18 +20,21 @@ namespace matplot::detail {
20
20
21
21
// Create a pipe for the child process's input
22
22
if (!CreatePipe (&hChildStdinRd, &hChildStdinWr, &saAttr, 0 )) {
23
+ errno = GetLastError (); // emulate POSIX behavior
23
24
return nullptr ;
24
25
}
25
26
26
27
// Ensure the write handle to the pipe is not inherited by child
27
28
// processes
28
29
if (!SetHandleInformation (hChildStdinWr, HANDLE_FLAG_INHERIT, 0 )) {
30
+ errno = GetLastError (); // emulate POSIX behavior
29
31
CloseHandle (hChildStdinRd);
30
32
return nullptr ;
31
33
}
32
34
33
35
// Create a pipe for the child process's output
34
36
if (!CreatePipe (&hStdin, &hStdout, &saAttr, 0 )) {
37
+ errno = GetLastError (); // emulate POSIX behavior
35
38
CloseHandle (hChildStdinRd);
36
39
CloseHandle (hChildStdinWr);
37
40
return nullptr ;
@@ -40,6 +43,7 @@ namespace matplot::detail {
40
43
// Ensure the read handle to the output pipe is not inherited by child
41
44
// processes
42
45
if (!SetHandleInformation (hStdout, HANDLE_FLAG_INHERIT, 0 )) {
46
+ errno = GetLastError (); // emulate POSIX behavior
43
47
CloseHandle (hChildStdinRd);
44
48
CloseHandle (hChildStdinWr);
45
49
CloseHandle (hStdin);
@@ -57,6 +61,7 @@ namespace matplot::detail {
57
61
// Create the child process, while hiding the window
58
62
if (!CreateProcess (NULL , const_cast <char *>(command), NULL , NULL , TRUE ,
59
63
CREATE_NO_WINDOW, NULL , NULL , &si, &pi)) {
64
+ errno = GetLastError (); // emulate POSIX behavior
60
65
CloseHandle (hChildStdinRd);
61
66
CloseHandle (hChildStdinWr);
62
67
CloseHandle (hStdin);
@@ -73,6 +78,7 @@ namespace matplot::detail {
73
78
FILE *file = _fdopen (_open_osfhandle ((intptr_t )hChildStdinWr, 0 ), mode);
74
79
75
80
if (file == nullptr ) {
81
+ errno = GetLastError (); // emulate POSIX behavior
76
82
CloseHandle (hChildStdinWr);
77
83
CloseHandle (hStdin);
78
84
CloseHandle (pi.hProcess );
@@ -87,16 +93,19 @@ namespace matplot::detail {
87
93
int hiddenPclose (FILE *file) {
88
94
HANDLE hFile = (HANDLE)_get_osfhandle (_fileno (file));
89
95
fclose (file);
96
+ // Wait for the process to finish
97
+ if (auto r = WaitForSingleObject (hFile, INFINITE); r != WAIT_OBJECT_0) {
98
+ errno = ECHILD; // emulate POSIX behavior
99
+ return -1 ;
100
+ }
101
+ // Retrieve the exit code
90
102
DWORD exitCode;
91
-
92
- // Wait for the process to finish and get its exit code
93
- if (WaitForSingleObject (hFile, INFINITE) == WAIT_OBJECT_0 &&
94
- GetExitCodeProcess (hFile, &exitCode)) {
95
- CloseHandle (hFile);
96
- return exitCode;
97
- } else {
98
- return -1 ; // Failed to get the exit code
103
+ if (auto r = GetExitCodeProcess (hFile, &exitCode); r == 0 ) {
104
+ errno = ECHILD; // emulate POSIX behavior
105
+ return -1 ;
99
106
}
107
+ CloseHandle (hFile);
108
+ return exitCode;
100
109
}
101
110
} // namespace matplot::detail
102
111
#endif // _WIN32
0 commit comments