Reputation: 302
I have to make a DLL time tester program in c++ that can load DLL and try their functions, testing if the give a correct results and checking the time that it takes to run them. So this means that I will receive during the execution of my program a DLL or different ones, load them and call their functions without knowing how many params the functions have (before compiling my program).
So I understand that I can't declare pointers to functions need by GetProcAddress
unless I declare dynamicly (I don't know if I can do this in c++).
Neither include the header of the DLL dynamically.
The only solution I can find is that my program generate a new c or c++ file with all the things necessary to call the DLL functions, compile, launch it...
Can you give me opinions, ideas, maybe there is a way to do this in C++ and I'm skipping it.
I'm using C++, with codeblock 13.12 and MinWG 4.8.
Upvotes: 1
Views: 145
Reputation: 13073
There are two basic solutions to this problem.
1) fill out all the possible functions on the fly, then when you are calling a function, you can choose the correct template and fill in the details.
2) generate the code on the fly.
1) Is not as difficult as it sounds, as there are a large number of functionally identical functions. It is possible for two functions to meet all the 0 parameter functions (stdcall + cdecl). 2 functions for one parameter call and so on. The correct data (e.g. pointer and size) gets aliased into pointer1, pointer2 and then passed to a generic function void * Func1_2params( void*,void*); - this may meet the calling convention of void func1( char, char);, int strtok( char *, char *);, ... as the slot (reg/stack) for each parameter element is "pointer sized"
2) There is a library ffi (foreign form interface) - which binds script languages to C/C++ which could be used to build the code on the fly to create the correct interface, this would be not quite straight forward, but would possible to get this to work. Finally some script languages have direct calls (java, luajit). It would be relatively easy to embed luajit in your code, and use a small amount of lua to build a callsite from "lua view" of a function to native, and then call a DLL.
Upvotes: 2