Thursday, May 02, 2024

How to call MessageBoxW using Zig in Windows

How to call MessageBoxW using Zig in Windows

OS: Windows 10 22H2
Zig: 0.12
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);
}
view raw messageboxw.zig hosted with ❤ by GitHub
Compile messageboxw.zig:
PS C:\prj> zig build-exe -lc .\msgboxw.zig

No comments:

How to configure nfs server on QNX 7.1

How to configure nfs server on QNX 7.1 Target: QNX 7.1 running on a VM (VMware Workstation or VirtualBox). It is assummed that the targets ...