Skip to content

Commit 0dcbca2

Browse files
committed
start: Make wWinMain nCmdShow setting match Windows better
1 parent 91e1176 commit 0dcbca2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/std/start.zig

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,29 @@ pub fn callMain() u8 {
603603
}
604604

605605
pub fn call_wWinMain() std.os.windows.INT {
606+
const peb = std.os.windows.peb();
606607
const MAIN_HINSTANCE = @typeInfo(@TypeOf(root.wWinMain)).Fn.params[0].type.?;
607608
const hInstance = @as(MAIN_HINSTANCE, @ptrCast(std.os.windows.kernel32.GetModuleHandleW(null).?));
608609
const lpCmdLine = std.os.windows.kernel32.GetCommandLineW();
609610

610-
// There's no (documented) way to get the nCmdShow parameter, so we're
611-
// using this fairly standard default.
612-
const nCmdShow = 5;
611+
// This makes Zig mostly match the nCmdShow behavior of a C program with a WinMain symbol:
612+
// - With STARTF_USESHOWWINDOW set in STARTUPINFO.dwFlags of the CreateProcess call:
613+
// - Compiled with subsystem:console -> nCmdShow is always SW_SHOWDEFAULT
614+
// - Compiled with subsystem:windows -> nCmdShow is STARTUPINFO.wShowWindow from
615+
// the parent CreateProcess call
616+
// - With STARTF_USESHOWWINDOW unset:
617+
// - nCmdShow is always SW_SHOWDEFAULT
618+
//
619+
// TODO: Figure out how to check the subsystem and use it to match the above
620+
// behavior, or intentionally avoid matching the above behavior with
621+
// regards to the subsystem.
622+
const SW_SHOWDEFAULT = 10;
623+
const STARTF_USESHOWWINDOW = 1;
624+
var nCmdShow: std.os.windows.ULONG = SW_SHOWDEFAULT;
625+
if (peb.ProcessParameters.dwFlags & STARTF_USESHOWWINDOW != 0) {
626+
nCmdShow = peb.ProcessParameters.dwShowWindow;
627+
}
613628

614629
// second parameter hPrevInstance, MSDN: "This parameter is always NULL"
615-
return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow);
630+
return root.wWinMain(hInstance, null, lpCmdLine, @intCast(nCmdShow));
616631
}

0 commit comments

Comments
 (0)