authorgravatar for anthonyarian96@gmail.comAnthony Arian <anthonyarian96@gmail.com> 2020-06-15 14:24:25+01:00
committergravatar for anthonyarian96@gmail.comDixiE <anthonyarian96@gmail.com> 2020-06-15 22:46:16+01:00
log5e48ed4a8d700c213a8cc71e7e8f0c57be52c70d
tree0199b0163a0e00118f8dcc76cb6a7e865706c739
parente7207bc267d90b5be009fcd937b86cde9bf9ae77

Implement WinMain Callers that Pass Valid Params


4 files changed, 65 insertions(+), 14 deletions(-)

lib/std/os/windows/bits.zig+2-2
......@@ -30,9 +30,9 @@ pub const HCRYPTPROV = ULONG_PTR;
3030pub const HBRUSH = *@Type(.Opaque);
3131pub const HCURSOR = *@Type(.Opaque);
3232pub const HICON = *@Type(.Opaque);
33pub const HINSTANCE = *@Type(.Opaque);
33pub const HINSTANCE = HANDLE;
3434pub const HMENU = *@Type(.Opaque);
35pub const HMODULE = *@Type(.Opaque);
35pub const HMODULE = HANDLE;
3636pub const HWND = *@Type(.Opaque);
3737pub const HDC = *@Type(.Opaque);
3838pub const HGLRC = *@Type(.Opaque);
lib/std/os/windows/kernel32.zig+1
......@@ -79,6 +79,7 @@ pub extern "kernel32" fn FormatMessageW(dwFlags: DWORD, lpSource: ?LPVOID, dwMes
7979pub extern "kernel32" fn FreeEnvironmentStringsW(penv: [*:0]u16) callconv(.Stdcall) BOOL;
8080
8181pub extern "kernel32" fn GetCommandLineA() callconv(.Stdcall) LPSTR;
82pub extern "kernel32" fn GetCommandLineW() callconv(.Stdcall) LPWSTR;
8283
8384pub extern "kernel32" fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: *DWORD) callconv(.Stdcall) BOOL;
8485
lib/std/start.zig+61-7
......@@ -23,8 +23,16 @@ comptime {
2323 } else if (builtin.os.tag == .windows) {
2424 if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
2525 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
26 {
27 @export(WinStartup, .{ .name = "WinMainCRTStartup" });
28 } else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
29 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
2630 {
2731 @export(WinMainCRTStartup, .{ .name = "WinMainCRTStartup" });
32 } else if (@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup") and
33 !@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup"))
34 {
35 @export(wWinMainCRTStartup, .{ .name = "wWinMainCRTStartup" });
2836 }
2937 } else if (builtin.os.tag == .uefi) {
3038 if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });
......@@ -123,6 +131,17 @@ fn _start() callconv(.Naked) noreturn {
123131 @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
124132}
125133
134fn WinStartup() callconv(.Stdcall) noreturn {
135 @setAlignStack(16);
136 if (!builtin.single_threaded) {
137 _ = @import("start_windows_tls.zig");
138 }
139
140 std.debug.maybeEnableSegfaultHandler();
141
142 std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain(u8, callMain));
143}
144
126145fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
127146 @setAlignStack(16);
128147 if (!builtin.single_threaded) {
......@@ -131,7 +150,20 @@ fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
131150
132151 std.debug.maybeEnableSegfaultHandler();
133152
134 std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
153 const result = initEventLoopAndCallMain(std.os.windows.INT, callWinMain);
154 std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
155}
156
157fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {
158 @setAlignStack(16);
159 if (!builtin.single_threaded) {
160 _ = @import("start_windows_tls.zig");
161 }
162
163 std.debug.maybeEnableSegfaultHandler();
164
165 const result = initEventLoopAndCallMain(std.os.windows.INT, callWWinMain);
166 std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
135167}
136168
137169// TODO https://github.com/ziglang/zig/issues/265
......@@ -185,7 +217,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
185217
186218 std.debug.maybeEnableSegfaultHandler();
187219
188 return initEventLoopAndCallMain();
220 return initEventLoopAndCallMain(u8, callMain);
189221}
190222
191223fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C) i32 {
......@@ -200,7 +232,7 @@ const bad_main_ret = "expected return type of main to be 'void', '!void', 'noret
200232
201233// This is marked inline because for some reason LLVM in release mode fails to inline it,
202234// and we want fewer call frames in stack traces.
203inline fn initEventLoopAndCallMain() u8 {
235inline fn initEventLoopAndCallMain(comptime Out: type, comptime mainFunc: fn () Out) Out {
204236 if (std.event.Loop.instance) |loop| {
205237 if (!@hasDecl(root, "event_loop")) {
206238 loop.init() catch |err| {
......@@ -214,7 +246,7 @@ inline fn initEventLoopAndCallMain() u8 {
214246
215247 var result: u8 = undefined;
216248 var frame: @Frame(callMainAsync) = undefined;
217 _ = @asyncCall(&frame, &result, callMainAsync, loop);
249 _ = @asyncCall(&frame, &result, callMainAsync, u8, mainFunc, loop);
218250 loop.run();
219251 return result;
220252 }
......@@ -222,13 +254,13 @@ inline fn initEventLoopAndCallMain() u8 {
222254
223255 // This is marked inline because for some reason LLVM in release mode fails to inline it,
224256 // and we want fewer call frames in stack traces.
225 return @call(.{ .modifier = .always_inline }, callMain, .{});
257 return @call(.{ .modifier = .always_inline }, mainFunc, .{});
226258}
227fn callMainAsync(loop: *std.event.Loop) callconv(.Async) u8 {
259fn callMainAsync(comptime Out: type, comptime mainProc: fn () Out, loop: *std.event.Loop) callconv(.Async) Out {
228260 // This prevents the event loop from terminating at least until main() has returned.
229261 loop.beginOneEvent();
230262 defer loop.finishOneEvent();
231 return callMain();
263 return mainProc();
232264}
233265
234266// This is not marked inline because it is called with @asyncCall when
......@@ -270,3 +302,25 @@ pub fn callMain() u8 {
270302 else => @compileError(bad_main_ret),
271303 }
272304}
305
306pub fn callWinMain() std.os.windows.INT {
307 const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
308 const lpCmdLine = std.os.windows.kernel32.GetCommandLineA();
309
310 // There's no (documented) way to get the nCmdShow parameter, so we're
311 // using this fairly standard default.
312 const nCmdShow = std.os.windows.user32.SW_SHOW;
313
314 return root.WinMain(hInstance, null, lpCmdLine, nCmdShow);
315}
316
317pub fn callWWinMain() std.os.windows.INT {
318 const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
319 const lpCmdLine = std.os.windows.kernel32.GetCommandLineW();
320
321 // There's no (documented) way to get the nCmdShow parameter, so we're
322 // using this fairly standard default.
323 const nCmdShow = std.os.windows.user32.SW_SHOW;
324
325 return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow);
326}
src/link.cpp+1-5
......@@ -2459,11 +2459,7 @@ static void add_win_link_args(LinkJob *lj, bool is_library, bool *have_windows_d
24592459 } else {
24602460 lj->args.append("-NODEFAULTLIB");
24612461 if (!is_library) {
2462 if (lj->codegen->have_winmain) {
2463 lj->args.append("-ENTRY:WinMain");
2464 } else if (lj->codegen->have_wwinmain) {
2465 lj->args.append("-ENTRY:wWinMain");
2466 } else if (lj->codegen->have_wwinmain_crt_startup) {
2462 if (lj->codegen->have_wwinmain_crt_startup) {
24672463 lj->args.append("-ENTRY:wWinMainCRTStartup");
24682464 } else {
24692465 lj->args.append("-ENTRY:WinMainCRTStartup");