首页 > c++ > 动态调用DLL C ++

动态调用DLL C ++ (Call DLLs dynamically C++)

2015-08-06 c++dll

问题

我必须在c ++中创建一个DLL时间测试程序,它可以加载DLL并尝试它们的功能,测试是否给出了正确的结果并检查运行它们所需的时间。 所以这意味着我将在程序执行期间收到一个DLL或不同的DLL,加载它们并调用它们的函数,而不知道函数有多少个参数(在编译程序之前)。

所以我明白我不能声明指向函数需要的指针,GetProcAddress除非我动态地声明(我不知道我是否可以在c ++中这样做)。 既不动态包含DLL的标头。

我能找到的唯一解决方案是我的程序生成一个新的c或c ++文件,其中包含调用DLL函数,编译,启动它所需的所有内容......

你能给我意见,想法吗?也许有一种方法可以用C ++做到这一点,我正在跳过它。

我正在使用C ++,代码块13.12和MinWG 4.8。

解决方法

这个问题有两个基本的解决方案。

1)即时填写所有可能的功能,然后在调用功能时,您可以选择正确的模板并填写详细信息。

2)动态生成代码。

1)并不像听起来那么困难,因为有大量功能相同的功能。两个函数可以满足所有0参数函数(stdcall + cdecl)。2个函数用于一个参数调用,依此类推。正确的数据(例如指针和大小)被别名变为pointer1,pointer2,然后传递给泛型函数void * Func1_2params(void *,void *); - 这可能符合void func1(char,char);, int strtok(char *,char *);,...的调用约定,因为每个参数元素的槽(reg / stack)是“指针大小”

2)有一个库ffi(外来表单接口) - 它将脚本语言绑定到C / C ++,它可以用来动态构建代码以创建正确的接口,这可能不是很直接,但可以让这个工作。最后一些脚本语言有直接调用(java,luajit)。将luajit嵌入代码中相对容易,并使用少量的lua从函数的“lua view”构建一个调用点到native,然后调用DLL。

问题

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.

解决方法

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.

相似信息