使用 C++ (Win32) 导入 DLL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1922580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import a DLL with C++ (Win32)
提问by Kristina Brooks
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. I already know the argument list for the function but I don't know how to call it.
我希望能够在这个 DLL 中调用一个函数。我已经知道函数的参数列表,但我不知道如何调用它。
Is there a way of declaring an imported function in C++ like in C# ?
有没有办法像在 C# 中一样在 C++ 中声明一个导入的函数?
采纳答案by luke
The c# syntax for declaring an imported function is not available in c++. Here are some other SO questions on how to use DLLs:
用于声明导入函数的 C# 语法在 C++ 中不可用。以下是有关如何使用 DLL 的其他一些 SO 问题:
回答by Russell Newquist
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. If you're not, they can be very difficult to produce - unlessyou already know the argument list for the function, which you say you do. Instructions can be found here, amongst other places.
否则,您需要与导入库链接,并且需要为编译器提供函数原型。通常,DLL 开发人员会提供导入库和带有原型的头文件。如果你不是,它们可能很难产生——除非你已经知道函数的参数列表,你说你知道。可以在此处以及其他地方找到说明。
回答by Serge Rogatch
At runtime you can call LoadLibrary()
and then call GetProcAddress()
to access the function from a DLL. You will need to cast this address to a prototype you define with typedef
. See the example at GetProcAddress
documentation page.
在运行时,您可以调用LoadLibrary()
然后调用GetProcAddress()
以从 DLL 访问该函数。您需要将此地址转换为您定义的原型typedef
。请参阅GetProcAddress
文档页面上的示例。