Skip to content

Instantly share code, notes, and snippets.

@technoscavenger
Created May 3, 2024 02:54
Show Gist options
  • Save technoscavenger/6e2cbdfb3c3637e0354130b86ea9b6ae to your computer and use it in GitHub Desktop.
Save technoscavenger/6e2cbdfb3c3637e0354130b86ea9b6ae to your computer and use it in GitHub Desktop.
MessageBoxW in Zig
const std = @import("std");
const c = @cImport({
@cInclude("windows.h");
});
pub fn main() !void {
const message = "Hello, World!";
const title = "Zig MessageBox";
// Buffer for UTF-16 strings
var message_w: [100]u16 = undefined;
var title_w: [100]u16 = undefined;
// Convert UTF-8 string to UTF-16LE
const message_len = std.unicode.utf8ToUtf16Le(&message_w, message) catch {
std.log.err("Error converting message string", .{});
return;
};
const title_len = std.unicode.utf8ToUtf16Le(&title_w, title) catch {
std.log.err("Error converting title string", .{});
return;
};
// Null-terminate the UTF-16 strings
message_w[message_len] = 0;
title_w[title_len] = 0;
// Calling MessageBoxW
_ = c.MessageBoxW(null, message_w[0..message_len :0], title_w[0..title_len :0], c.MB_OK);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment