How can you call a game function inside a function hook?

For example, I hooked the recv function of winsock. Then, I examined a packet. This packet has the value of my current hp. I check the hp. If it's lower than the constant hp threshold I will call a game function that uses an item to heal my hp. Is it possible to call that game function without crashing the application?

I tried and watch the execution in the debugger it's calling the game function of use item. However, it crashes the application. I think it's probably because of registers? The stack is fine btw.

I'm wondering why I when calling the send function of winsock inside the recv function does not crash. But when I call a game function other than send it crashes. Is it because i'm in a different module? or You can't call a function in a different module? But I followed the execution in the debugger and when the call to use item is executed it really goes to that function. Help me please.


The problem is that the game's functions are usually expected to be called only in certain circumstances. If you're calling a function at a time when it's not prepared for it, the game's state may be wrong and you may get crashes or other unexpected behavior. For example, in normal code flow the caller prepares some objects before calling the function, but you don't do that.

It's difficult to tell the exact reason with such limited info, but I suggest you to trace the function's execution in normal game flow versus when you call it from the hook and see where they diverge; maybe you can figure out how to fix it to behave the same way.

  • I managed to make it work :) I'm lucky that the function I'm calling requires less preparations. Thank you. – Noob Programmer Aug 30 '17 at 23:39

In addition to Igor's answer I would like to suggest an alternative approach:

If the function you're trying to call indeed expects some additional preparations, specific conditions for global game state variables, etc. It might be easier for you to just reverse engineer the function you're trying to call and implement the modification and state changes you desire yourself. This might be as easy as figuring out your character structure's memory location and modifying the health parameter with your own code.

One other point to consider is that it might happen to be that the state and condition requirements set by the function you're trying to call might be more complex than just passing along the right parameters. It could expect global variables to be initialized which might only happen in later stages of the game, for example.

Lastly, you might have a bug related to the way you process the received message and have hidden assumptions about the state of the game while these messages arrive. Writing careful code that validates the expected conditions might both help you diagnose the issue at hand as well as remediate it by avoiding the call in faulty circumstances.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.