Calling game functions with C#
I'm writing a little bot for myself, and because i'm good in c# but not that great in c++ i wrote myself a little bootstrap dll that basically hosts the clr in the game process and then proceeds to load my C# dll which contains the bot logic. The C# dll also loads EasyHook, which enables me to easily hook functions.
After a bit of derping around in x64dbg i found a function in the games that takes a string pointer as argument and writes that string into chat or does all kinds of command interpretation (/ability, / laugh, ...) so it's pretty powerful. I'd like to call that function from my C# dll.
What I did:
* Find the main game loop
* Hook the first function in the loop, so I can have a "OnUpdate" event in C# thats called every frame and runs in the game thread
* Create a delegate that describes the target function
* Get the delegate from the function pointer:
* Call the function in my OnUpdate once: func("Kappa");
Results: It works
the string is sent into chat as intended
Problem: After a few seconds, the game crashes with an exception of type STATUS_INVALID_PARAMETER. x64dbg shows a huge call stack that doesn't make much sense to me:
i.?imgur?.?com/?pmRlHrT.png (am i seriously not allowed to post images?)
I thought that maybe easyhook didn't clean stuff up, but thats not the case (checked manually with debugger). If I just hook the function and do trivial stuff, for example count frames and print them to the screen everything is fine. Only if I call a game function everything goes wrong.
I'd like to hear any ideas you have regarding this, especially:
* Do you think this is because I'm calling from C#?
* Do you think this is because I'm calling the function from a wrong place, which causes global stuff to be modified?
After a bit of derping around in x64dbg i found a function in the games that takes a string pointer as argument and writes that string into chat or does all kinds of command interpretation (/ability, / laugh, ...) so it's pretty powerful. I'd like to call that function from my C# dll.
What I did:
* Find the main game loop
* Hook the first function in the loop, so I can have a "OnUpdate" event in C# thats called every frame and runs in the game thread
* Create a delegate that describes the target function
* Get the delegate from the function pointer:
Code:
var func = Marshal.GetDelegateForFunctionPointer<ChatFunctionDlg>(new IntPtr(GetModuleBaseAddress("Game.dll").ToInt64() + 0x1234ABC));
Results: It works
the string is sent into chat as intendedProblem: After a few seconds, the game crashes with an exception of type STATUS_INVALID_PARAMETER. x64dbg shows a huge call stack that doesn't make much sense to me:
i.?imgur?.?com/?pmRlHrT.png (am i seriously not allowed to post images?)
I thought that maybe easyhook didn't clean stuff up, but thats not the case (checked manually with debugger). If I just hook the function and do trivial stuff, for example count frames and print them to the screen everything is fine. Only if I call a game function everything goes wrong.
I'd like to hear any ideas you have regarding this, especially:
* Do you think this is because I'm calling from C#?
* Do you think this is because I'm calling the function from a wrong place, which causes global stuff to be modified?
