20
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 file to define the symbols to be exported from the DLL.
有很多方法可以做到这一点,但我认为最简单的方法之一是在链接时将应用程序链接到DLL,然后使用定义文件定义要从DLL导出的符号。
CAVEAT: The definition file approach works bests for undecorated symbol names. If you want to export decorated symbols then it is probably better to NOT USE the definition file approach.
注意:定义文件方法对于未修饰的符号名称效果最好。如果您想要导出装饰符号,那么最好不要使用定义文件方法。
Here is an simple example on how this is done.
这里有一个简单的例子说明如何实现这一点。
Step 1: Define the function in the export.h file.
步骤1:在导出中定义函数。h文件。
int WINAPI IsolatedFunction(const char *title, const char *test);
Step 2: Define the function in the export.cpp file.
步骤2:在导出中定义函数。cpp文件。
#include
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.def defintion file.
步骤3:将函数定义为出口.def定义文件中的导出。
EXPORTS IsolatedFunction @1
Step 4: Create a DLL project and add the export.cpp and export.def files to this project. Building this project will create an export.dll and an export.lib file.
步骤4:创建一个DLL项目并添加导出。cpp和export.def文件到这个项目。构建这个项目将创建一个导出。dll和出口。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 LoadLibrary and GetProcAddress to load the function entry point at runtime.
以下两个步骤在链接时链接到DLL。如果您不想在链接时定义入口点,请忽略接下来的两个步骤,并使用LoadLibrary和GetProcAddress在运行时加载函数入口点。
Step 5: Create a Test application project to use the dll by adding the export.lib file to the project. Copy the export.dll file to ths same location as the Test console executable.
步骤5:创建一个测试应用程序项目,通过添加导出来使用dll。项目的库文件。复制导出。dll文件到与测试控制台可执行文件相同的位置。
Step 6: Call the IsolatedFunction function from within the Test application as shown below.
步骤6:从测试应用程序中调用隔离函数,如下所示。
#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;
}