Skip to content

Instantly share code, notes, and snippets.

@dimdin
Last active November 20, 2024 20:10
Show Gist options
  • Save dimdin/77e435a02d88dfe721634088a1cca1ff to your computer and use it in GitHub Desktop.
Save dimdin/77e435a02d88dfe721634088a1cca1ff to your computer and use it in GitHub Desktop.
zig translate-c windows.h
const std = @import("std");
const win = std.os.windows;
// int MessageBoxA(
// [in, optional] HWND hWnd,
// [in, optional] LPCSTR lpText,
// [in, optional] LPCSTR lpCaption,
// [in] UINT uType
// );
extern "user32" fn MessageBoxA(hWnd: ?win.HANDLE, lpText: ?win.LPCSTR, lpCaption: ?win.LPCSTR, uType: win.UINT) c_int;
// int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);
pub export fn wWinMain(hInstance: ?win.HINSTANCE, hPrevInstance: ?win.HINSTANCE, lpCmdLine: ?win.LPWSTR, nShowCmd: win.INT) callconv(win.WINAPI) c_int {
_ = hInstance; // autofix
_ = hPrevInstance; // autofix
_ = lpCmdLine; // autofix
_ = nShowCmd; // autofix
const MB_OK = 0;
const MB_ICONINFORMATION = 0x40;
_ = MessageBoxA(null, "Hello, World!", "Zig!", MB_OK | MB_ICONINFORMATION);
return 0;
}
# The directory that contains `stdlib.h`.
include_dir=~/xwin/sdk/include/ucrt
# On Windows it's the directory that includes `vcruntime.h`.
sys_include_dir=~/xwin/crt/include
crt_dir=~/xwin/sdk/lib/ucrt/x64
# The directory that contains `vcruntime.lib`.
msvc_lib_dir=~/xwin/crt/lib/x64
# The directory that contains `kernel32.lib`.
kernel32_lib_dir=~/xwin/sdk/lib/um/x64
gcc_dir=
# install https://rustup.rs/
# and https://github.com/Jake-Shadle/xwin
cargo install xwin
# download microsoft sdk in ~/xwin
xwin --accept-license --arch x86_64,aarch64 --variant desktop --sdk-version "10.0.20348" --cache-dir ~/.xwin-cache \
splat --include-debug-libs --include-debug-symbols --preserve-ms-arch-notation --output ~/xwin
# 10.0.20348 is the latest Windows 10 SDK, after that it is Windows 11 SDK.
zig build-exe win.zig -target x86_64-windows-msvc --libc xwin-libc.txt --subsystem windows
zig build-exe main.zig -lc -target x86_64-windows-msvc --libc xwin-libc.txt --subsystem console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment