Skip to content

set error mode to remove error popups #2445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/hello_world/hello_windows.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use @import("std").os.windows;
usingnamespace @import("std").os.windows;

extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) c_int;

Expand Down
15 changes: 15 additions & 0 deletions std/os/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ pub const shell32 = @import("windows/shell32.zig");

pub usingnamespace @import("windows/bits.zig");

pub const subsystem = blk: {
if (builtin.os == .windows or builtin.os == .uefi) {
if (@hasDecl(builtin, "subsystem")) break :blk builtin.subsystem;
const root = @import("root");
if (@hasDecl(root, "main") or @hasDecl(root, "wmain") or @hasDecl(root, "mainCRTStartup") or @hasDecl(root, "wmainCRTStartup")) {
break :blk builtin.SubSystem.Console;
} else if (@hasDecl(root, "WinMain") or @hasDecl(root, "wWinMain") or @hasDecl(root, "WinMainCRTStartup") or @hasDecl(root, "wWinMainCRTStartup")) {
break :blk builtin.SubSystem.Windows;
} else {
break :blk builtin.SubSystem.Console;
}
}
break :blk null;
};

pub const CreateFileError = error{
SharingViolation,
PathAlreadyExists,
Expand Down
3 changes: 3 additions & 0 deletions std/os/windows/bits.zig
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,6 @@ pub const FORMAT_MESSAGE_FROM_STRING = 0x00000400;
pub const FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;
pub const FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
pub const FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF;

pub const SEM_FAILCRITICALERRORS = 0x0001;
pub const SEM_NOGPFAULTERRORBOX = 0x0002;
14 changes: 14 additions & 0 deletions std/os/windows/kernel32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub extern "kernel32" stdcallcc fn GetConsoleScreenBufferInfo(hConsoleOutput: HA
pub extern "kernel32" stdcallcc fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: ?[*]WCHAR) DWORD;

pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;

pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;

pub extern "kernel32" stdcallcc fn GetEnvironmentStringsW() ?[*]u16;
Expand Down Expand Up @@ -101,13 +102,19 @@ pub extern "kernel32" stdcallcc fn GetProcessHeap() ?HANDLE;
pub extern "kernel32" stdcallcc fn GetQueuedCompletionStatus(CompletionPort: HANDLE, lpNumberOfBytesTransferred: LPDWORD, lpCompletionKey: *ULONG_PTR, lpOverlapped: *?*OVERLAPPED, dwMilliseconds: DWORD) BOOL;

pub extern "kernel32" stdcallcc fn GetSystemInfo(lpSystemInfo: *SYSTEM_INFO) void;

pub extern "kernel32" stdcallcc fn GetSystemTimeAsFileTime(*FILETIME) void;

pub extern "kernel32" stdcallcc fn HeapCreate(flOptions: DWORD, dwInitialSize: SIZE_T, dwMaximumSize: SIZE_T) ?HANDLE;

pub extern "kernel32" stdcallcc fn HeapDestroy(hHeap: HANDLE) BOOL;

pub extern "kernel32" stdcallcc fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: *c_void, dwBytes: SIZE_T) ?*c_void;

pub extern "kernel32" stdcallcc fn HeapSize(hHeap: HANDLE, dwFlags: DWORD, lpMem: *const c_void) SIZE_T;

pub extern "kernel32" stdcallcc fn HeapCompact(hHeap: HANDLE, dwFlags: DWORD) SIZE_T;

pub extern "kernel32" stdcallcc fn HeapSummary(hHeap: HANDLE, dwFlags: DWORD, lpSummary: LPHEAP_SUMMARY) BOOL;

pub extern "kernel32" stdcallcc fn GetStdHandle(in_nStdHandle: DWORD) ?HANDLE;
Expand Down Expand Up @@ -163,6 +170,10 @@ pub extern "kernel32" stdcallcc fn SetFilePointerEx(
in_dwMoveMethod: DWORD,
) BOOL;

pub extern "kernel32" stdcallcc fn SetThreadErrorMode(dwNewMode: DWORD, lpOldMode: LPDWORD) BOOL;

pub extern "kernel32" stdcallcc fn GetThreadErrorMode() DWORD;

pub extern "kernel32" stdcallcc fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) BOOL;

pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD) void;
Expand Down Expand Up @@ -192,8 +203,11 @@ pub extern "kernel32" stdcallcc fn GetProcAddress(hModule: HMODULE, lpProcName:
pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) BOOL;

pub extern "kernel32" stdcallcc fn InitializeCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;

pub extern "kernel32" stdcallcc fn EnterCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;

pub extern "kernel32" stdcallcc fn LeaveCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;

pub extern "kernel32" stdcallcc fn DeleteCriticalSection(lpCriticalSection: *CRITICAL_SECTION) void;

pub extern "kernel32" stdcallcc fn InitOnceExecuteOnce(InitOnce: *INIT_ONCE, InitFn: INIT_ONCE_FN, Parameter: ?*c_void, Context: ?*c_void) BOOL;
5 changes: 5 additions & 0 deletions std/special/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ extern fn WinMainCRTStartup() noreturn {
if (!builtin.single_threaded) {
_ = @import("start_windows_tls.zig");
}

if (std.os.windows.getSubSystem().? != .Windows) {
var err_mode = std.os.windows.kernel32.GetThreadErrorMode();
_ = std.os.windows.kernel32.SetThreadErrorMode(err_mode | std.os.windows.kernel32.SEM_FAILCRITICALERRORS | std.os.windows.kernel32.SEM_NOGPFAULTERRORBOX, &err_mode);
}
std.os.windows.kernel32.ExitProcess(callMain());
}

Expand Down