diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-10-15 19:39:44 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-10-15 19:39:44 -0700 |
| commit | 996a2284dde16b030a63cb9ba42f586c7a1b66cd (patch) | |
| tree | e081338028a98565fc44bd77c45bf90a3e1d862e /lib/std | |
| parent | 51a3d0603c116d99c0a93dd451a69c79dd0cbca2 (diff) | |
| parent | d87bd3d8afc883853958389fcf4c65826426769b (diff) | |
| download | zig-996a2284dde16b030a63cb9ba42f586c7a1b66cd.tar.gz zig-996a2284dde16b030a63cb9ba42f586c7a1b66cd.zip | |
Merge branch 'AnthonyYoManz-5002-fix-entrypoint-with-winmain'
closes #5002
closes #5613
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/os/windows/kernel32.zig | 3 | ||||
| -rw-r--r-- | lib/std/start.zig | 50 |
2 files changed, 42 insertions, 11 deletions
diff --git a/lib/std/os/windows/kernel32.zig b/lib/std/os/windows/kernel32.zig index 05d160485d..5d0b5bb227 100644 --- a/lib/std/os/windows/kernel32.zig +++ b/lib/std/os/windows/kernel32.zig @@ -84,6 +84,7 @@ pub extern "kernel32" fn FormatMessageW(dwFlags: DWORD, lpSource: ?LPVOID, dwMes pub extern "kernel32" fn FreeEnvironmentStringsW(penv: [*:0]u16) callconv(.Stdcall) BOOL; pub extern "kernel32" fn GetCommandLineA() callconv(.Stdcall) LPSTR; +pub extern "kernel32" fn GetCommandLineW() callconv(.Stdcall) LPWSTR; pub extern "kernel32" fn GetConsoleMode(in_hConsoleHandle: HANDLE, out_lpMode: *DWORD) callconv(.Stdcall) BOOL; @@ -111,8 +112,6 @@ pub extern "kernel32" fn GetFileAttributesW(lpFileName: [*]const WCHAR) callconv pub extern "kernel32" fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: [*]u16, nSize: DWORD) callconv(.Stdcall) DWORD; -pub extern "kernel32" fn GetModuleHandleA(lpModuleName: ?LPCSTR) callconv(.Stdcall) ?HMODULE; - pub extern "kernel32" fn GetModuleHandleW(lpModuleName: ?[*:0]const WCHAR) callconv(.Stdcall) ?HMODULE; pub extern "kernel32" fn GetLastError() callconv(.Stdcall) Win32Error; diff --git a/lib/std/start.zig b/lib/std/start.zig index 71940b12ca..cb838fef34 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -29,7 +29,15 @@ comptime { if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup")) { - @export(WinMainCRTStartup, .{ .name = "WinMainCRTStartup" }); + @export(WinStartup, .{ .name = "wWinMainCRTStartup" }); + } else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and + !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup")) + { + @compileError("WinMain not supported; declare wWinMain or main instead"); + } else if (@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup") and + !@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup")) + { + @export(wWinMainCRTStartup, .{ .name = "wWinMainCRTStartup" }); } } else if (builtin.os.tag == .uefi) { if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" }); @@ -143,7 +151,18 @@ fn _start() callconv(.Naked) noreturn { @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{}); } -fn WinMainCRTStartup() callconv(.Stdcall) noreturn { +fn WinStartup() callconv(.Stdcall) noreturn { + @setAlignStack(16); + if (!builtin.single_threaded) { + _ = @import("start_windows_tls.zig"); + } + + std.debug.maybeEnableSegfaultHandler(); + + std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain(u8, callMain)); +} + +fn wWinMainCRTStartup() callconv(.Stdcall) noreturn { @setAlignStack(16); if (!builtin.single_threaded) { _ = @import("start_windows_tls.zig"); @@ -151,7 +170,8 @@ fn WinMainCRTStartup() callconv(.Stdcall) noreturn { std.debug.maybeEnableSegfaultHandler(); - std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain()); + const result = initEventLoopAndCallMain(std.os.windows.INT, call_wWinMain); + std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result)); } // TODO https://github.com/ziglang/zig/issues/265 @@ -205,7 +225,7 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 { std.debug.maybeEnableSegfaultHandler(); - return initEventLoopAndCallMain(); + return initEventLoopAndCallMain(u8, callMain); } fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) callconv(.C) i32 { @@ -220,7 +240,7 @@ const bad_main_ret = "expected return type of main to be 'void', '!void', 'noret // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. -inline fn initEventLoopAndCallMain() u8 { +inline fn initEventLoopAndCallMain(comptime Out: type, comptime mainFunc: fn () Out) Out { if (std.event.Loop.instance) |loop| { if (!@hasDecl(root, "event_loop")) { loop.init() catch |err| { @@ -234,7 +254,7 @@ inline fn initEventLoopAndCallMain() u8 { var result: u8 = undefined; var frame: @Frame(callMainAsync) = undefined; - _ = @asyncCall(&frame, &result, callMainAsync, .{loop}); + _ = @asyncCall(&frame, &result, callMainAsync, .{ u8, mainFunc, loop }); loop.run(); return result; } @@ -242,13 +262,13 @@ inline fn initEventLoopAndCallMain() u8 { // This is marked inline because for some reason LLVM in release mode fails to inline it, // and we want fewer call frames in stack traces. - return @call(.{ .modifier = .always_inline }, callMain, .{}); + return @call(.{ .modifier = .always_inline }, mainFunc, .{}); } -fn callMainAsync(loop: *std.event.Loop) callconv(.Async) u8 { +fn callMainAsync(comptime Out: type, comptime mainProc: fn () Out, loop: *std.event.Loop) callconv(.Async) Out { // This prevents the event loop from terminating at least until main() has returned. loop.beginOneEvent(); defer loop.finishOneEvent(); - return callMain(); + return mainProc(); } // This is not marked inline because it is called with @asyncCall when @@ -290,3 +310,15 @@ pub fn callMain() u8 { else => @compileError(bad_main_ret), } } + +pub fn call_wWinMain() std.os.windows.INT { + const hInstance = @ptrCast(std.os.windows.HINSTANCE, std.os.windows.kernel32.GetModuleHandleW(null).?); + const hPrevInstance: ?std.os.windows.HINSTANCE = null; // MSDN: "This parameter is always NULL" + const lpCmdLine = std.os.windows.kernel32.GetCommandLineW(); + + // There's no (documented) way to get the nCmdShow parameter, so we're + // using this fairly standard default. + const nCmdShow = std.os.windows.user32.SW_SHOW; + + return root.wWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); +} |
