Repository navigation
Expand file tree
/
Copy pathhello_hook.cpp
More file actions
48 lines (41 loc) · 1.67 KB
/
Copy pathhello_hook.cpp
File metadata and controls
48 lines (41 loc) · 1.67 KB
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
// ─── Splice example: hello-hook ───────────────────────────────────────────
//
// The 5-minute "does it work" example. Hooks a plain free function, proves
// the callback fires and that calling `orig` still reaches the real body.
//
// Build (from a configured tree):
// cmake --build --preset=windows-x64-dev --target splice_example_hello_hook
// ./out/build/windows-x64-dev/examples/splice_example_hello_hook
//
// Expected output:
// add(2, 3) before hook = 5
// [hook] add(2, 3) intercepted
// add(2, 3) after hook = 105
// ───────────────────────────────────────────────────────────────────────────
#include <splice/splice.h>
#include <cstdio>
// A noinline target so the compiler leaves a real prologue to patch.
#if defined(_MSC_VER)
__declspec(noinline)
#else
__attribute__((noinline))
#endif
int add(int a, int b) {
return a + b;
}
int main() {
std::printf("add(2, 3) before hook = %d\n", add(2, 3));
// Install a hook: log the call, then return the original result + 100.
SPLICE_HOOK_ADDR(&add)
.onInvoke([](auto orig, int a, int b) {
std::printf("[hook] add(%d, %d) intercepted\n", a, b);
return orig(a, b) + 100;
});
splice::install_all();
if (!splice_is_hooked(reinterpret_cast<void*>(&add))) {
std::printf("(no live patcher on this platform — built as a stub)\n");
return 0;
}
std::printf("add(2, 3) after hook = %d\n", add(2, 3));
return 0;
}