(OLD) Process and function hooking library for Windows
Striven
2026-01-21 38b4bb0a701e79f77352a2b9431b4afdb74783e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const win32 = @import("win32").everything;
 
const Error = error{WinApiError};
 
const Module = @This();
 
pub fn win32Fallible(ret: win32.BOOL) !void {
    if (ret == win32.FALSE) return Error.WinApiError;
}
 
pub fn win32Nullable(ret: anytype) !@TypeOf(ret.?) {
    if (ret == null) return Error.WinApiError;
    return ret.?;
}
 
pub const Process = struct {
    handle: win32.HANDLE,
 
    pub fn current() Process {
        return .{ .handle = win32.GetCurrentProcess().? };
    }
};
 
pub const Info = struct {
    image_size: usize,
 
    pub fn fromWin32ModuleInfo(module_info: win32.MODULEINFO) Info {
        return .{
            .image_size = module_info.SizeOfImage,
        };
    }
};
 
process: Process,
handle: win32.HINSTANCE,
 
pub fn fromCurrentProcess(name: [:0]const u8) !Module {
    return .{
        .process = .current(),
        .handle = try win32Nullable(win32.GetModuleHandleA(name)),
    };
}
 
pub fn info(self: Module) !Info {
    var module_info: win32.MODULEINFO = undefined;
    try win32Fallible(win32.K32GetModuleInformation(
        self.process.handle,
        self.handle,
        &module_info,
        @sizeOf(win32.MODULEINFO),
    ));
 
    return .fromWin32ModuleInfo(module_info);
}
 
pub fn slice(self: Module) ![]const u8 {
    const inf = try self.info();
    const start: [*]u8 = @ptrCast(self.handle);
 
    return start[0..inf.image_size];
}
 
pub fn getProcAddress(self: Module, proc_name: [:0]const u8) ?*const anyopaque {
    return @ptrCast(win32.GetProcAddress(self.handle, proc_name));
}