How to call MessageBoxW using Zig in Windows
OS: Windows 10 22H2Zig: 0.12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
PS C:\prj> zig build-exe -lc .\msgboxw.zig
No comments:
Post a Comment