从 C++ 调用 DLL 中的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/539358/
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
Calling functions in a DLL from C++
提问by QueueHammer
I have a solution in VS 2008 with 2 projects in it. One is a DLL written in C++ and the other is a simple C++ console application created from a blank project. I would like know how to call the functions in the DLL from the application.
我在 VS 2008 中有一个解决方案,其中包含 2 个项目。一个是用 C++ 编写的 DLL,另一个是从空白项目创建的简单 C++ 控制台应用程序。我想知道如何从应用程序调用 DLL 中的函数。
Assume I am starting with a blank C++ project and that I want to call a function called int IsolatedFunction(int someParam)
假设我从一个空白的 C++ 项目开始,我想调用一个名为 int IsolatedFunction(int someParam)
How do I call it?
我怎么称呼它?
采纳答案by jussij
There are many ways to do this but I think one of the easiest options is to link the application to the DLL at link time and then use a definition fileto define the symbols to be exported from the DLL.
有很多方法可以做到这一点,但我认为最简单的选择之一是在链接时将应用程序链接到 DLL,然后使用定义文件来定义要从 DLL 导出的符号。
CAVEAT:The definition file approach works bests for undecoratedsymbol names. If you want to export decorated symbols then it is probably better to NOT USEthe definition file approach.
警告:定义文件方法最适合未修饰的符号名称。如果您想导出装饰符号,那么最好不要使用定义文件方法。
Here is an simple example on how this is done.
这是一个关于如何完成的简单示例。
Step 1:Define the function in the export.hfile.
第一步:在export.h文件中定义函数。
int WINAPI IsolatedFunction(const char *title, const char *test);
Step 2:Define the function in the export.cppfile.
第二步:在export.cpp文件中定义函数。
#include <windows.h>
int WINAPI IsolatedFunction(const char *title, const char *test)
{
MessageBox(0, title, test, MB_OK);
return 1;
}
Step 3:Define the function as an export in the export.defdefintion file.
第 3 步:在export.def定义文件中将函数定义为导出。
EXPORTS IsolatedFunction @1
Step 4:Create a DLL project and add the export.cppand export.deffiles to this project. Building this project will create an export.dlland an export.libfile.
第 4 步:创建一个 DLL 项目并将export.cpp和export.def文件添加到该项目中。构建此项目将创建一个export.dll和一个export.lib文件。
The following two steps link to the DLL at link time. If you don't want to define the entry points at link time, ignore the next two steps and use the LoadLibraryand GetProcAddressto load the function entry point at runtime.
以下两个步骤在链接时链接到 DLL。如果不想在链接时定义入口点,请忽略接下来的两个步骤,并在运行时使用LoadLibrary和GetProcAddress加载函数入口点。
Step 5:Create a Testapplication project to use the dll by adding the export.libfile to the project. Copy the export.dllfile to ths same location as the Testconsole executable.
步骤 5:通过将export.lib文件添加到项目中,创建一个测试应用程序项目以使用 dll 。将export.dll文件复制到与测试控制台可执行文件相同的位置。
Step 6:Call the IsolatedFunctionfunction from within the Test application as shown below.
步骤 6:从 Test 应用程序中调用IndependentFunction函数,如下所示。
#include "stdafx.h"
// get the function prototype of the imported function
#include "../export/export.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// call the imported function found in the dll
int result = IsolatedFunction("hello", "world");
return 0;
}
回答by Ismael
Can also export functions from dll and import from the exe, it is more tricky at first but in the end is much easier than calling LoadLibrary/GetProcAddress. See MSDN.
也可以从dll中导出函数,从exe中导入,刚开始比较麻烦,但最终比调用LoadLibrary/GetProcAddress容易多了。请参阅MSDN。
When creating the project with the VS wizard there's a check box in the dll that let you export functions.
使用 VS 向导创建项目时,dll 中有一个复选框,可让您导出函数。
Then, in the exe application you only have to #include a header from the dll with the proper definitions, and add the dll project as a dependency to the exe application.
然后,在 exe 应用程序中,您只需要 #include 来自具有正确定义的 dll 的标头,并将 dll 项目作为依赖项添加到 exe 应用程序。
Check this other question if you want to investigate this point further Exporting functions from a DLL with dllexport.
如果您想进一步研究这一点,请检查另一个问题。使用 dllexport 从 DLL 导出函数。
回答by Franci Penov
You can either go the LoadLibrary/GetProcAddress route (as Harper mentioned in his answer, here's link to the run-time dynamic linking MSDN sample again) or you can link your console application to the .lib produced from the DLL project and include the hea.h file with the declaration of your function (as described in the load-time dynamic linking MSDN sample)
您可以选择 LoadLibrary/GetProcAddress 路线(正如 Harper 在他的回答中提到的,这里再次链接到运行时动态链接 MSDN 示例),或者您可以将您的控制台应用程序链接到从 DLL 项目生成的 .lib 并包含 hea .h 文件以及您的函数声明(如加载时动态链接 MSDN 示例中所述)
In both cases, you need to make sure your DLL exports the function you want to call properly. The easiest way to do it is by using __declspec(dllexport) on the function declaration (as shown in the creating a simple dynamic-link library MSDN sample), though you can do it also through the corresponding .def file in your DLL project.
在这两种情况下,您都需要确保您的 DLL 导出您想要正确调用的函数。最简单的方法是在函数声明中使用 __declspec(dllexport)(如创建一个简单的动态链接库 MSDN 示例所示),不过您也可以通过 DLL 项目中的相应 .def 文件来实现。
For more information on the topic of DLLs, you should browse through the MSDN About Dynamic-Link Librariestopic.
有关 DLL 主题的更多信息,您应该浏览MSDN 关于动态链接库主题。
回答by Kathir Softwareandfinance
The following are the 5 steps required:
以下是所需的5个步骤:
- declare the function pointer
- Load the library
- Get the procedure address
- assign it to function pointer
- call the function using function pointer
- 声明函数指针
- 加载库
- 获取程序地址
- 将其分配给函数指针
- 使用函数指针调用函数
You can find the step by step VC++ IDE screen shot at http://www.softwareandfinance.com/Visual_CPP/DLLDynamicBinding.html
您可以在http://www.softwareandfinance.com/Visual_CPP/DLLDynamicBinding.html找到逐步的 VC++ IDE 屏幕截图
Here is the code snippet:
这是代码片段:
int main()
{
/***
__declspec(dllimport) bool GetWelcomeMessage(char *buf, int len); // used for static binding
***/
typedef bool (*GW)(char *buf, int len);
HMODULE hModule = LoadLibrary(TEXT("TestServer.DLL"));
GW GetWelcomeMessage = (GW) GetProcAddress(hModule, "GetWelcomeMessage");
char buf[128];
if(GetWelcomeMessage(buf, 128) == true)
std::cout << buf;
return 0;
}
回答by Tim Matthews
When the DLL was created an import lib is usually automatically created and you should use that linked in to your program along with header files to call it but if not then you can manually call windows functions like LoadLibraryand GetProcAddressto get it working.
创建 DLL 时,通常会自动创建一个导入库,您应该使用链接到您的程序以及头文件来调用它,但如果没有,您可以手动调用 Windows 函数(如LoadLibrary和GetProcAddress)以使其工作。