authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-15 19:37:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-15 19:37:55-07:00
logd87bd3d8afc883853958389fcf4c65826426769b
treee081338028a98565fc44bd77c45bf90a3e1d862e
parentc7c38e72793df53e6582b6f8b501e18314ab07e9

fixups regarding windows wide strings

* remove GetModuleHandleA from kernel32.zig. use of A functions considered harmful. * make it a compile error to expose WinMain instead of wWinMain. same thing. * start code declares wWinMainCRTStartup instead of WinMainCRTStartup when it has the choice.

4 files changed, 13 insertions(+), 41 deletions(-)

lib/std/os/windows/kernel32.zig-2
...@@ -112,8 +112,6 @@ pub extern "kernel32" fn GetFileAttributesW(lpFileName: [*]const WCHAR) callconv...@@ -112,8 +112,6 @@ pub extern "kernel32" fn GetFileAttributesW(lpFileName: [*]const WCHAR) callconv
112112
113pub extern "kernel32" fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: [*]u16, nSize: DWORD) callconv(.Stdcall) DWORD;113pub extern "kernel32" fn GetModuleFileNameW(hModule: ?HMODULE, lpFilename: [*]u16, nSize: DWORD) callconv(.Stdcall) DWORD;
114114
115pub extern "kernel32" fn GetModuleHandleA(lpModuleName: ?LPCSTR) callconv(.Stdcall) ?HMODULE;
116
117pub extern "kernel32" fn GetModuleHandleW(lpModuleName: ?[*:0]const WCHAR) callconv(.Stdcall) ?HMODULE;115pub extern "kernel32" fn GetModuleHandleW(lpModuleName: ?[*:0]const WCHAR) callconv(.Stdcall) ?HMODULE;
118116
119pub extern "kernel32" fn GetLastError() callconv(.Stdcall) Win32Error;117pub extern "kernel32" fn GetLastError() callconv(.Stdcall) Win32Error;
lib/std/start.zig+8-30
...@@ -29,11 +29,11 @@ comptime {...@@ -29,11 +29,11 @@ comptime {
29 if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and29 if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
30 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))30 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
31 {31 {
32 @export(WinStartup, .{ .name = "WinMainCRTStartup" });32 @export(WinStartup, .{ .name = "wWinMainCRTStartup" });
33 } else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and33 } else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
34 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))34 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
35 {35 {
36 @export(WinMainCRTStartup, .{ .name = "WinMainCRTStartup" });36 @compileError("WinMain not supported; declare wWinMain or main instead");
37 } else if (@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup") and37 } else if (@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup") and
38 !@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup"))38 !@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup"))
39 {39 {
...@@ -162,18 +162,6 @@ fn WinStartup() callconv(.Stdcall) noreturn {...@@ -162,18 +162,6 @@ fn WinStartup() callconv(.Stdcall) noreturn {
162 std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain(u8, callMain));162 std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain(u8, callMain));
163}163}
164164
165fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
166 @setAlignStack(16);
167 if (!builtin.single_threaded) {
168 _ = @import("start_windows_tls.zig");
169 }
170
171 std.debug.maybeEnableSegfaultHandler();
172
173 const result = initEventLoopAndCallMain(std.os.windows.INT, callWinMain);
174 std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
175}
176
177fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {165fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {
178 @setAlignStack(16);166 @setAlignStack(16);
179 if (!builtin.single_threaded) {167 if (!builtin.single_threaded) {
...@@ -182,7 +170,7 @@ fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {...@@ -182,7 +170,7 @@ fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {
182170
183 std.debug.maybeEnableSegfaultHandler();171 std.debug.maybeEnableSegfaultHandler();
184172
185 const result = initEventLoopAndCallMain(std.os.windows.INT, callWWinMain);173 const result = initEventLoopAndCallMain(std.os.windows.INT, call_wWinMain);
186 std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));174 std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
187}175}
188176
...@@ -266,7 +254,7 @@ inline fn initEventLoopAndCallMain(comptime Out: type, comptime mainFunc: fn ()...@@ -266,7 +254,7 @@ inline fn initEventLoopAndCallMain(comptime Out: type, comptime mainFunc: fn ()
266254
267 var result: u8 = undefined;255 var result: u8 = undefined;
268 var frame: @Frame(callMainAsync) = undefined;256 var frame: @Frame(callMainAsync) = undefined;
269 _ = @asyncCall(&frame, &result, callMainAsync, .{u8, mainFunc, loop});257 _ = @asyncCall(&frame, &result, callMainAsync, .{ u8, mainFunc, loop });
270 loop.run();258 loop.run();
271 return result;259 return result;
272 }260 }
...@@ -323,24 +311,14 @@ pub fn callMain() u8 {...@@ -323,24 +311,14 @@ pub fn callMain() u8 {
323 }311 }
324}312}
325313
326pub fn callWinMain() std.os.windows.INT {314pub fn call_wWinMain() std.os.windows.INT {
327 const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);315 const hInstance = @ptrCast(std.os.windows.HINSTANCE, std.os.windows.kernel32.GetModuleHandleW(null).?);
328 const lpCmdLine = std.os.windows.kernel32.GetCommandLineA();316 const hPrevInstance: ?std.os.windows.HINSTANCE = null; // MSDN: "This parameter is always NULL"
329
330 // There's no (documented) way to get the nCmdShow parameter, so we're
331 // using this fairly standard default.
332 const nCmdShow = std.os.windows.user32.SW_SHOW;
333
334 return root.WinMain(hInstance, null, lpCmdLine, nCmdShow);
335}
336
337pub fn callWWinMain() std.os.windows.INT {
338 const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
339 const lpCmdLine = std.os.windows.kernel32.GetCommandLineW();317 const lpCmdLine = std.os.windows.kernel32.GetCommandLineW();
340318
341 // There's no (documented) way to get the nCmdShow parameter, so we're319 // There's no (documented) way to get the nCmdShow parameter, so we're
342 // using this fairly standard default.320 // using this fairly standard default.
343 const nCmdShow = std.os.windows.user32.SW_SHOW;321 const nCmdShow = std.os.windows.user32.SW_SHOW;
344322
345 return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow);323 return root.wWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
346}324}
src/link/Coff.zig+4-8
...@@ -1097,17 +1097,13 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {...@@ -1097,17 +1097,13 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
1097 try argv.append("-NODEFAULTLIB");1097 try argv.append("-NODEFAULTLIB");
1098 if (!is_lib) {1098 if (!is_lib) {
1099 if (self.base.options.module) |module| {1099 if (self.base.options.module) |module| {
1100 if (module.stage1_flags.have_winmain) {1100 if (module.stage1_flags.have_winmain_crt_startup) {
1101 try argv.append("-ENTRY:WinMain");
1102 } else if (module.stage1_flags.have_wwinmain) {
1103 try argv.append("-ENTRY:wWinMain");
1104 } else if (module.stage1_flags.have_wwinmain_crt_startup) {
1105 try argv.append("-ENTRY:wWinMainCRTStartup");
1106 } else {
1107 try argv.append("-ENTRY:WinMainCRTStartup");1101 try argv.append("-ENTRY:WinMainCRTStartup");
1102 } else {
1103 try argv.append("-ENTRY:wWinMainCRTStartup");
1108 }1104 }
1109 } else {1105 } else {
1110 try argv.append("-ENTRY:WinMainCRTStartup");1106 try argv.append("-ENTRY:wWinMainCRTStartup");
1111 }1107 }
1112 }1108 }
1113 }1109 }
test/stack_traces.zig+1-1
...@@ -282,7 +282,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -282,7 +282,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
282 \\source.zig:10:8: [address] in main (test)282 \\source.zig:10:8: [address] in main (test)
283 \\ foo();283 \\ foo();
284 \\ ^284 \\ ^
285 \\start.zig:301:29: [address] in std.start.posixCallMainAndExit (test)285 \\start.zig:289:29: [address] in std.start.posixCallMainAndExit (test)
286 \\ return root.main();286 \\ return root.main();
287 \\ ^287 \\ ^
288 \\start.zig:151:5: [address] in std.start._start (test)288 \\start.zig:151:5: [address] in std.start._start (test)