Skip to content

Commit 2f6df2c

Browse files
authored
added example
1 parent a27f67d commit 2f6df2c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ This project is licensed under the `GNU AGPL-3.0`. No later version is allowed.
1111

1212
Read the file `LICENSE` for more information.
1313

14+
## Example
15+
Hooking the function `static int myFunction(int mynumber, String name)` from a class `Dummy`:
16+
```c++
17+
jvalue hkMyFunction(JNIEnv *jni, jmethodID callableMethod, jvalue *args, size_t nargs, void *arg)
18+
{
19+
// Print parameters
20+
jvalue mynumber = args[0];
21+
jvalue name = args[1];
22+
std::cout << "[*] mynumber value: " << mynumber.i << std::endl;
23+
std::cout << "[*] name object: " << name.l << std::endl;
24+
25+
// Call the original function with 'mynumber' set to '1337'
26+
mynumber.i = 1337;
27+
jni->CallStaticIntMethod(dummyClass, callableMethod, mynumber, (jstring)&name);
28+
29+
// Get original function parameters
30+
return jvalue { .i = 42 }; // Modify the return value to '42'
31+
}
32+
33+
void start(JavaVM *jvm, JNIEnv *jni)
34+
{
35+
jclass dummyClass = jni->FindClass("dummy/Dummy");
36+
jmethodID myFunctionID = jni->GetStaticMethodID(dummyClass, "myFunction", "(ILjava/lang/String;)I");
37+
38+
JNIHook_Init(jvm);
39+
JNIHook_Attach(myFunctionID, hkMyFunction, NULL);
40+
}
41+
```
42+
1443
## Building
1544
To build this, you can either compile all the files in `src` into your project, or
1645
use CMake to build a static library, which can be compiled into your project.

0 commit comments

Comments
 (0)