@@ -41,6 +41,17 @@ public func SetHandleInformation(hObject : HANDLE, dwMask : DWORD, dwFlags : DWO
4141@ extern
4242func SetEnvironmentVariableA (name : *char, value : *char) : int;
4343
44+ @ dllimport @ extern @ stdcall
45+ func CreateFileA (
46+ lpFileName : LPCSTR,
47+ dwDesiredAccess : DWORD,
48+ dwShareMode : DWORD,
49+ lpSecurityAttributes : *mut SECURITY_ATTRIBUTES,
50+ dwCreationDisposition : DWORD,
51+ dwFlagsAndAttributes : DWORD,
52+ hTemplateFile : HANDLE
53+ ) : HANDLE;
54+
4455// ---------------------------------------------------------------------------
4556// Constants
4657// ---------------------------------------------------------------------------
@@ -51,11 +62,22 @@ comptime const WAIT_OBJECT_0 : DWORD = 0 as DWORD;
5162comptime const WAIT_TIMEOUT : DWORD = 258 as DWORD;
5263comptime const INFINITE : DWORD = 0xFFFFFFFF as DWORD;
5364comptime const HANDLE_FLAG_INHERIT : DWORD = 0x00000001 as DWORD;
65+ comptime const FILE_WRITE_DATA : DWORD = 0x00000002 as DWORD;
66+ comptime const OPEN_EXISTING : DWORD = 3 as DWORD;
67+ comptime const INVALID_HANDLE_VALUE : HANDLE = (0xFFFFFFFF as usize) as HANDLE;
5468
5569// ---------------------------------------------------------------------------
5670// Internal helpers
5771// ---------------------------------------------------------------------------
5872
73+ /// Open NUL: (Windows /dev/null equivalent) so un-captured output doesn't
74+ /// leak to the parent's console.
75+ func win_open_nul () : HANDLE {
76+ var h = CreateFileA ("NUL" , FILE_WRITE_DATA, 0 , null, OPEN_EXISTING, 0 , null)
77+ if (h == INVALID_HANDLE_VALUE) { return GetStdHandle (STD_OUTPUT_HANDLE) }
78+ return h
79+ }
80+
5981/// Read all data from a handle until EOF.
6082func win_read_all (h : HANDLE, data : *mut vector<u8>) : bool {
6183 unsafe var buf : [4096 ]u8;
@@ -221,14 +243,14 @@ public func win_execute(cfg : *ProcessConfig, out : *mut ProcessResult) : bool {
221243 if (cfg.capture_stdout || cfg.merge_stdout_stderr) {
222244 si.hStdOutput = stdout_write
223245 } else {
224- si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE )
246+ si.hStdOutput = win_open_nul ( )
225247 }
226248 if (cfg.merge_stdout_stderr) {
227249 si.hStdError = stdout_write
228250 } else if (cfg.capture_stderr) {
229251 si.hStdError = stderr_write
230252 } else {
231- si.hStdError = GetStdHandle (STD_ERROR_HANDLE )
253+ si.hStdError = win_open_nul ( )
232254 }
233255 if (has_stdin_data) {
234256 si.hStdInput = stdin_read
@@ -416,14 +438,14 @@ public func win_spawn(cfg : *ProcessConfig, child : *mut ChildProcess) : bool {
416438 if (cfg.capture_stdout || cfg.merge_stdout_stderr) {
417439 si.hStdOutput = stdout_write
418440 } else {
419- si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE )
441+ si.hStdOutput = win_open_nul ( )
420442 }
421443 if (cfg.merge_stdout_stderr) {
422444 si.hStdError = stdout_write
423445 } else if (cfg.capture_stderr) {
424446 si.hStdError = stderr_write
425447 } else {
426- si.hStdError = GetStdHandle (STD_ERROR_HANDLE )
448+ si.hStdError = win_open_nul ( )
427449 }
428450 si.hStdInput = stdin_read
429451
0 commit comments