简体   繁体   English

用C ++导入DLL(Win32)

[英]Import a DLL with C++ (Win32)

How do I import a DLL (minifmod.dll) in C++ ? 如何在C ++中导入DLL(minifmod.dll)?

I want to be able to call a function inside this DLL. 我希望能够在此DLL中调用一个函数。 I already know the argument list for the function but I don't know how to call it. 我已经知道该函数的参数列表,但不知道如何调用它。

Is there a way of declaring an imported function in C++ like in C# ? 有没有办法像C#中那样在C ++中声明导入的函数?

If the DLL includes a COM type library, you can use the #import statement as such: 如果DLL包含COM类型库,则可以这样使用#import语句:

#import dllname.dll

Otherwise, you'll need to link with an import library, and you'll need to provide a function prototype for your compiler. 否则,您将需要与导入库链接,并且需要为编译器提供函数原型。 Typically the import library and a header file with the prototypes are provided by the DLL developer. 通常,DLL开发人员会提供导入库和带有原型的头文件。 If you're not, they can be very difficult to produce - unless you already know the argument list for the function, which you say you do. 如果不是这样,则很难生成它们- 除非您已经知道该函数的参数列表(即您说的那样)。 Instructions can be found here , amongst other places. 指示可以在这里找到,以及其他地方。

At runtime you can call LoadLibrary() and then call GetProcAddress() to access the function from a DLL. 在运行时,您可以调用LoadLibrary() ,然后调用GetProcAddress()来从DLL中访问函数。 You will need to cast this address to a prototype you define with typedef . 您将需要将此地址转换为使用typedef定义的原型。 See the example at GetProcAddress documentation page. 请参阅GetProcAddress文档页面上的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM