ptr0x ptr0x - 1 year ago 76
C++ Question

How to automatically call all functions of a class/scope

I'm developing a patcher dll in c++ and I need a way to automatically call all funcions of a given code scope or a class or whatever works.

I need this beacause I'm organizing the code by putting each patch in a different function, which have to be called at some point. So my code ends up with a lot of calls to void functions that creates the patches. I want to call these functions automatically, so I don't have to maintain this "useless" code.

I tried to create some mechanism based in declaring these functions as virtual functions of a class and then just iterate the vtable calling the functions, the problem is that I could not find some sort of pattern of the vtable's end, so I couldn't know where to stop iterating correctly.

Does anyone have any idea of what I could do?

Thanks in advance.

Answer Source

This is definitely an instance of the XY problem, but fortunately you explained your motivation for wanting to do this. Instead of trying to do introspection on normal code after compiling, automate the generation of the boilerplate sequence of calls.

One option would be to use some kind of a macro that declares a function and adds it to a list of functions to be called.

Another way would be auto-generating a file with some kind of macro-preprocessor or text-processing tool. #include it at the location you want the sequence of calls.