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.
This commit is contained in:
Andrew Kelley 2020-10-15 19:37:55 -07:00
parent c7c38e7279
commit d87bd3d8af
4 changed files with 13 additions and 41 deletions

View file

@ -112,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;

View file

@ -29,11 +29,11 @@ comptime {
if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
!@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
{
@export(WinStartup, .{ .name = "WinMainCRTStartup" });
@export(WinStartup, .{ .name = "wWinMainCRTStartup" });
} else if (@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
!@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
{
@export(WinMainCRTStartup, .{ .name = "WinMainCRTStartup" });
@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"))
{
@ -162,18 +162,6 @@ fn WinStartup() callconv(.Stdcall) noreturn {
std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain(u8, callMain));
}
fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
@setAlignStack(16);
if (!builtin.single_threaded) {
_ = @import("start_windows_tls.zig");
}
std.debug.maybeEnableSegfaultHandler();
const result = initEventLoopAndCallMain(std.os.windows.INT, callWinMain);
std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
}
fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {
@setAlignStack(16);
if (!builtin.single_threaded) {
@ -182,7 +170,7 @@ fn wWinMainCRTStartup() callconv(.Stdcall) noreturn {
std.debug.maybeEnableSegfaultHandler();
const result = initEventLoopAndCallMain(std.os.windows.INT, callWWinMain);
const result = initEventLoopAndCallMain(std.os.windows.INT, call_wWinMain);
std.os.windows.kernel32.ExitProcess(@bitCast(std.os.windows.UINT, result));
}
@ -266,7 +254,7 @@ inline fn initEventLoopAndCallMain(comptime Out: type, comptime mainFunc: fn ()
var result: u8 = undefined;
var frame: @Frame(callMainAsync) = undefined;
_ = @asyncCall(&frame, &result, callMainAsync, .{u8, mainFunc, loop});
_ = @asyncCall(&frame, &result, callMainAsync, .{ u8, mainFunc, loop });
loop.run();
return result;
}
@ -323,24 +311,14 @@ pub fn callMain() u8 {
}
}
pub fn callWinMain() std.os.windows.INT {
const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
const lpCmdLine = std.os.windows.kernel32.GetCommandLineA();
// 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.WinMain(hInstance, null, lpCmdLine, nCmdShow);
}
pub fn callWWinMain() std.os.windows.INT {
const hInstance = std.os.windows.kernel32.GetModuleHandleA(null);
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, null, lpCmdLine, nCmdShow);
return root.wWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}

View file

@ -1097,17 +1097,13 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
try argv.append("-NODEFAULTLIB");
if (!is_lib) {
if (self.base.options.module) |module| {
if (module.stage1_flags.have_winmain) {
try argv.append("-ENTRY:WinMain");
} else if (module.stage1_flags.have_wwinmain) {
try argv.append("-ENTRY:wWinMain");
} else if (module.stage1_flags.have_wwinmain_crt_startup) {
try argv.append("-ENTRY:wWinMainCRTStartup");
} else {
if (module.stage1_flags.have_winmain_crt_startup) {
try argv.append("-ENTRY:WinMainCRTStartup");
} else {
try argv.append("-ENTRY:wWinMainCRTStartup");
}
} else {
try argv.append("-ENTRY:WinMainCRTStartup");
try argv.append("-ENTRY:wWinMainCRTStartup");
}
}
}

View file

@ -282,7 +282,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
\\source.zig:10:8: [address] in main (test)
\\ foo();
\\ ^
\\start.zig:301:29: [address] in std.start.posixCallMainAndExit (test)
\\start.zig:289:29: [address] in std.start.posixCallMainAndExit (test)
\\ return root.main();
\\ ^
\\start.zig:151:5: [address] in std.start._start (test)