有很多方法可以做到这一点,但我认为最简单的选择之一是在链接时将应用程序链接到 DLL,然后使用定义文件定义要从 DLL 导出的符号。
CAVEAT:定义文件方法最适合未装饰的符号名称。如果您想导出装饰符号,那么最好是NOT USE定义文件方法。
这是一个关于如何完成此操作的简单示例。
Step 1:定义函数在export.h file.
int WINAPI IsolatedFunction(const char *title, const char *test);
Step 2:定义函数在导出.cpp file.
#include <windows.h>
int WINAPI IsolatedFunction(const char *title, const char *test)
{
MessageBox(0, title, test, MB_OK);
return 1;
}
Step 3:将函数定义为导出导出.def定义文件。
EXPORTS IsolatedFunction @1
Step 4:创建一个DLL项目并添加导出.cpp and 导出.def文件到这个项目。构建这个项目将创建一个导出.dll and an 导出库 file.
以下两个步骤在链接时链接到 DLL。如果您不想在链接时定义入口点,请忽略接下来的两个步骤并使用加载库 and 获取进程地址在运行时加载函数入口点。
Step 5:创建一个Test应用程序项目通过添加以下内容来使用该dll导出库文件到项目中。复制导出.dll文件复制到与Test控制台可执行文件。
Step 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;
}