From 1655a666d5695ac974e107233a90a75eebe5fc2f Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Tue, 13 Jan 2026 02:36:21 -0800 Subject: [PATCH] windows_resources standalone test: Load a resource and check its data Just a potential way to catch regressions and to ensure the resources actually make it into the binary correctly. --- test/standalone/windows_resources/build.zig | 7 ++- test/standalone/windows_resources/main.zig | 48 ++++++++++++++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/test/standalone/windows_resources/build.zig b/test/standalone/windows_resources/build.zig index d3afadb60d5d4a6e16cc3f98f04a45c75c350307..3b140c90f79e650cc9fb4b944ac1452ac9996c97 100644 --- a/test/standalone/windows_resources/build.zig +++ b/test/standalone/windows_resources/build.zig @@ -46,7 +46,10 @@ fn add( .gnu => .gnu, }; - _ = exe.getEmittedBin(); + const exe_run_step = b.addRunArtifact(exe); + exe_run_step.skip_foreign_checks = true; + exe_run_step.expectStdErrEqual(""); + exe_run_step.expectStdOutEqual(""); - test_step.dependOn(&exe.step); + test_step.dependOn(&exe_run_step.step); } diff --git a/test/standalone/windows_resources/main.zig b/test/standalone/windows_resources/main.zig index f92e18124bb86f9b42fa47ec0a8291c7b0d9ad35..9af794103652656d6dd25c2139cfb2b7a45d6a0d 100644 --- a/test/standalone/windows_resources/main.zig +++ b/test/standalone/windows_resources/main.zig @@ -1,5 +1,51 @@ const std = @import("std"); +const builtin = @import("builtin"); +const w = std.os.windows; pub fn main() !void { - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + if (builtin.os.tag == .windows) { + const name = std.unicode.wtf8ToWtf16LeStringLiteral("FOO"); + const RT_RCDATA = MAKEINTRESOURCEW(10); + const handle = FindResourceW(null, name, RT_RCDATA) orelse { + std.debug.print("unable to find resource: {t}\n", .{w.GetLastError()}); + return error.FailedToLoadResource; + }; + const res = LoadResource(null, handle) orelse { + std.debug.print("unable to load resource: {t}\n", .{w.GetLastError()}); + return error.FailedToLoadResource; + }; + const data_ptr = LockResource(res) orelse { + std.debug.print("unable to lock resource: {t}\n", .{w.GetLastError()}); + return error.FailedToLoadResource; + }; + const size = SizeofResource(null, handle); + const data = @as([*]const u8, @ptrCast(data_ptr))[0..size]; + try std.testing.expectEqualSlices(u8, "foo", data); + } } + +const HRSRC = *opaque {}; +const HGLOBAL = *opaque {}; +fn MAKEINTRESOURCEW(id: u16) [*:0]align(1) const w.WCHAR { + return @ptrFromInt(id); +} + +extern "kernel32" fn FindResourceW( + hModule: ?w.HMODULE, + lpName: [*:0]align(1) const w.WCHAR, + lpType: [*:0]align(1) const w.WCHAR, +) callconv(.winapi) ?HRSRC; + +extern "kernel32" fn LoadResource( + hModule: ?w.HMODULE, + hResInfo: HRSRC, +) callconv(.winapi) ?HGLOBAL; + +extern "kernel32" fn LockResource( + hResData: HGLOBAL, +) callconv(.winapi) ?w.LPVOID; + +extern "kernel32" fn SizeofResource( + hModule: ?w.HMODULE, + hResInfo: HRSRC, +) callconv(.winapi) w.DWORD; -- 2.54.0