Help! Issue when hook string

Hello, I just started learning about hooking. Currently, I am trying to modify the string at PlayerName, but the game always crashes.

Here is my hook code:
Code:
monoString* (*old_PlayerName)(void* instance);
monoString* PlayerName(void* instance) {
    if(instance!=nullptr) {
        return CreateString ("test"); 
    }
}

    HOOK_LIB("libil2cpp.so", "0x13d0adc", PlayerName, old_PlayerName);
Dump:
Code:
// Dll : Assembly-CSharp.dll
// Namespace:
public class PlayerName : MonoBehaviour
{
    // Fields
    public TextMeshProUGUI textMesh; // 0x20

    // Properties

    // Methods
    // RVA: 0x13d0adc VA: 0x7ba90e6adc
    private Void Update() { }
    // RVA: 0x13d0b54 VA: 0x7ba90e6b54
    public Void .ctor() { }
}
 
The problem is that Update() is a void, but your hook is returning a monoString* or simply a string, which causes a return type mismatch and crashes the game.

What you need to hook is a method like this:
C++:
public string PlayerName()

Then you hook with:
C++:
monoString* (*old_PlayerName)(void* instance);
monoString* PlayerName(void* instance) {
    if (instance != NULL) {
        return CreateString("yourname");
    }
    return old_PlayerName(instance); // Always call this incase the instance is null.
}

By hooking a proper method that returns a string, you match the expected return type and prevent the game from crashing.
 
Back
Top Bottom