3
hexc
5y

So I was wondering if any of you know if any good ways to inject additional functionality into a function in CPP. My use case is injecting a counter into an OpenGL draw function to see how many times per frame it's called. I know I can do this using assembly Inca more hacky manner as you might do for cheats in games(code caves), but I'm more interested in adding is for debugging/statistics for the game engine I'm working on. Basically im looking for a portable stable way of doing it that when I compile as a debug build, the code gets added to various functions, and when I compile under release, it doesn't.

Example:
glDraw();

Would call
glDraw() {
drawCount++; //some debug stuff
glDraw(); //call the real one internally
}

I should mention with code caves you can do this by saving the original address of the function, patching the vTable to point to your new function that has the same parameters etc, then all calls to that function are redirected to yours instead and then you simply call the original function with the address of the function you originally saved. That said, I'm not sure how to access vTable, etc the "normal" way...

Comments
  • 0
    i'm not familiar with cpp but wouldn't it just be something like OpenGL.glDraw(); or object_instance.glDraw();
  • 0
    I don't think there's a way to inject functionality into functions whose source you don't control with C++, though you could achieve that behaviour in other ways. Those would be:

    1. Write a function overload that takes a dummy argument and does what you need.
    2. Write a function named differently that does what you need.
    3. Write a function inside a namespace you control with that name and that does what you need and when you want to call glDraw(), you call ns::glDraw(), you'd want to call the real glDraw() with ::glDraw() in your namespaced one though
Add Comment