Skip to content

Commit ae6a989

Browse files
mluggandrewrk
authored andcommitted
std.start: initialize Windows console output CP to UTF-8 on exe startup
1 parent 97b1a9b commit ae6a989

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/std/start.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ extern "kernel32" fn ExitProcess(exit_code: u32) callconv(.C) noreturn;
210210

211211
////////////////////////////////////////////////////////////////////////////////
212212

213+
/// Contains any logic that should be run on exe startup to bully Windows
214+
/// into being a sane environment
215+
inline fn setupWindows() void {
216+
if (std.options.windows_force_utf8_codepage) {
217+
_ = std.os.windows.kernel32.SetConsoleOutputCP(65001); // use UTF-8 codepage
218+
}
219+
}
220+
213221
fn _DllMainCRTStartup(
214222
hinstDLL: std.os.windows.HINSTANCE,
215223
fdwReason: std.os.windows.DWORD,
@@ -382,6 +390,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
382390
_ = @import("start_windows_tls.zig");
383391
}
384392

393+
setupWindows();
385394
std.debug.maybeEnableSegfaultHandler();
386395

387396
std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
@@ -393,6 +402,7 @@ fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
393402
_ = @import("start_windows_tls.zig");
394403
}
395404

405+
setupWindows();
396406
std.debug.maybeEnableSegfaultHandler();
397407

398408
const result: std.os.windows.INT = initEventLoopAndCallWinMain();
@@ -495,6 +505,9 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
495505
std.os.argv = argv[0..argc];
496506
std.os.environ = envp;
497507

508+
if (builtin.os.tag == .windows) {
509+
setupWindows();
510+
}
498511
std.debug.maybeEnableSegfaultHandler();
499512

500513
return initEventLoopAndCallMain();

lib/std/std.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ pub const options = struct {
159159
options_override.crypto_always_getrandom
160160
else
161161
false;
162+
163+
pub const windows_force_utf8_codepage: bool = if (@hasDecl(options_override, "windows_force_utf8_codepage"))
164+
options_override.windows_force_utf8_codepage
165+
else
166+
true;
162167
};
163168

164169
// This forces the start.zig file to be imported, and the comptime logic inside that

0 commit comments

Comments
 (0)