From a450016eb1816a8f0989626dde4b0c1865cba33d Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Fri, 1 May 2026 19:24:59 -0700 Subject: [PATCH] Add tests for DllMain to windows_entry_points standalone test --- .../standalone/windows_entry_points/build.zig | 71 +++++++++++++++++++ .../standalone/windows_entry_points/dllmain.c | 9 +++ .../windows_entry_points/dllmain.zig | 18 +++++ .../windows_entry_points/loaddll.zig | 13 ++++ 4 files changed, 111 insertions(+) create mode 100644 test/standalone/windows_entry_points/dllmain.c create mode 100644 test/standalone/windows_entry_points/dllmain.zig create mode 100644 test/standalone/windows_entry_points/loaddll.zig diff --git a/test/standalone/windows_entry_points/build.zig b/test/standalone/windows_entry_points/build.zig index 236ebe95c632112c9cefc8afd912c247d103dc52..d63de59c2b970b7bfe04bcd95578eaae9fc6b88d 100644 --- a/test/standalone/windows_entry_points/build.zig +++ b/test/standalone/windows_entry_points/build.zig @@ -139,4 +139,75 @@ pub fn build(b: *std.Build) void { _ = exe.getEmittedBin(); test_step.dependOn(&exe.step); } + + const load_dll_exe = b.addExecutable(.{ .name = "load_dll", .root_module = b.createModule(.{ + .root_source_file = b.path("loaddll.zig"), + .target = target, + .optimize = .Debug, + }) }); + + { + const dll = b.addLibrary(.{ + .name = "zig_dllmain", + .linkage = .dynamic, + .root_module = b.createModule(.{ + .root_source_file = b.path("dllmain.zig"), + .target = target, + .optimize = .Debug, + }), + }); + + const run = b.addRunArtifact(load_dll_exe); + run.addArtifactArg(dll); + run.expectStdErrEqual("hello from DllMain"); + run.expectStdOutEqual(""); + run.expectExitCode(0); + run.skip_foreign_checks = true; + + test_step.dependOn(&run.step); + } + + { + const dll = b.addLibrary(.{ + .name = "zig_dllmain_link_libc", + .linkage = .dynamic, + .root_module = b.createModule(.{ + .root_source_file = b.path("dllmain.zig"), + .target = target, + .optimize = .Debug, + .link_libc = true, + }), + }); + + const run = b.addRunArtifact(load_dll_exe); + run.addArtifactArg(dll); + run.expectStdErrEqual("hello from DllMain"); + run.expectStdOutEqual(""); + run.expectExitCode(0); + run.skip_foreign_checks = true; + + test_step.dependOn(&run.step); + } + + { + const dll = b.addLibrary(.{ + .name = "c_dllmain", + .linkage = .dynamic, + .root_module = b.createModule(.{ + .target = target, + .optimize = .Debug, + .link_libc = true, + }), + }); + dll.root_module.addCSourceFile(.{ .file = b.path("dllmain.c") }); + + const run = b.addRunArtifact(load_dll_exe); + run.addArtifactArg(dll); + run.expectStdErrEqual("hello from DllMain"); + run.expectStdOutEqual(""); + run.expectExitCode(0); + run.skip_foreign_checks = true; + + test_step.dependOn(&run.step); + } } diff --git a/test/standalone/windows_entry_points/dllmain.c b/test/standalone/windows_entry_points/dllmain.c new file mode 100644 index 0000000000000000000000000000000000000000..266ce5876bd2da206f8cf1c0fe6e72d8bd6635d3 --- /dev/null +++ b/test/standalone/windows_entry_points/dllmain.c @@ -0,0 +1,9 @@ +#include +#include + +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { + if (fdwReason == DLL_PROCESS_ATTACH) { + fprintf(stderr, "hello from DllMain"); + } + return TRUE; +} diff --git a/test/standalone/windows_entry_points/dllmain.zig b/test/standalone/windows_entry_points/dllmain.zig new file mode 100644 index 0000000000000000000000000000000000000000..f4df3a5afbad2fc17693a963650fc88f63a027c3 --- /dev/null +++ b/test/standalone/windows_entry_points/dllmain.zig @@ -0,0 +1,18 @@ +const std = @import("std"); +const windows = std.os.windows; + +const DLL_PROCESS_ATTACH = 1; + +pub fn DllMain( + hinstDLL: windows.HINSTANCE, + fdwReason: windows.DWORD, + lpReserved: windows.LPVOID, +) windows.BOOL { + _ = hinstDLL; + _ = lpReserved; + switch (fdwReason) { + DLL_PROCESS_ATTACH => std.debug.print("hello from DllMain", .{}), + else => {}, + } + return .TRUE; +} diff --git a/test/standalone/windows_entry_points/loaddll.zig b/test/standalone/windows_entry_points/loaddll.zig new file mode 100644 index 0000000000000000000000000000000000000000..116fe36a645bf34fa199f2f8c50701ddbd3152b7 --- /dev/null +++ b/test/standalone/windows_entry_points/loaddll.zig @@ -0,0 +1,13 @@ +const std = @import("std"); +const windows = std.os.windows; + +extern "kernel32" fn LoadLibraryW(windows.LPCWSTR) callconv(.winapi) ?windows.HMODULE; + +pub fn main(init: std.process.Init) !void { + const arena = init.arena.allocator(); + const args = try init.minimal.args.toSlice(arena); + if (args.len < 2) return error.NoDllPathSpecified; + const dll_path = args[1]; + const dll_path_w = try std.unicode.wtf8ToWtf16LeAllocZ(arena, dll_path); + _ = LoadLibraryW(dll_path_w) orelse return error.FailedToLoadDll; +} -- 2.54.0