这是一个带有 DEF 文件的示例解决方案。
DLL 项目:
CppLib.h:
#ifdef CPPLIB_EXPORTS
#define CPPLIB_API __declspec(dllexport)
#else
#define CPPLIB_API __declspec(dllimport)
#endif
CPPLIB_API double MyFunction1(double);
CppLib.cpp:
CPPLIB_API double MyFunction1(double dd)
{
return dd;
}
CppLib.def:
LIBRARY
EXPORTS
MySuperFunction=MyFunction1 @1
构建 DLL。
如果我们在 CppLib.DLL 上运行 dumpbin,我们会得到:
...
ordinal hint RVA name
2 0 0001101E ?MyFunction1@@YANN@Z = @ILT+25(?MyFunction1@@YANN@Z)
1 1 0001101E MySuperFunction = @ILT+25(?MyFunction1@@YANN@Z)
...
使用 CppLib.dll 的控制台应用程序:
#include "CppLib.h"
#include <Windows.h>
#include <iostream>
int main()
{
typedef double(*MY_SUPER_FUNC)(double);
HMODULE hm = LoadLibraryW(L"CppLib.dll");
MY_SUPER_FUNC fn1 = (MY_SUPER_FUNC)GetProcAddress(hm, "MySuperFunction"); // find by name
MY_SUPER_FUNC fn2 = (MY_SUPER_FUNC)GetProcAddress(hm, MAKEINTRESOURCEA(1)); // find by ordinal
std::cout << fn1(34.5) << std::endl; // prints 34.5
std::cout << fn2(12.3) << std::endl; // prints 12.3
return 0;
}