必要知识1:
动态链接库-隐式调用:需要dll、lib、h
动态链接库-显式调用:只需要dll
必要知识2:
注意UE4是64位,别搞个32位的dll那就丢人了
必要知识3:
VS自带工具:dumpbin可以查看dll里面的函数名
因为c++的dll导出函数名和你的声明不一样
如(HelloWorld)变成(?HelloWorld@DllClass@@QEAAXXZ)
必要知识4:
如果导出dll类,那需要一个额外函数用来new并且返回指针
外面无法实例化类,比如createCalss()
如果你用的是更复杂的隐式调用,有头文件h的话也许可以在外面创建类实例
必要知识5:
UE4的显式导入dll的函数与c++不同,如下
FPlatformProcess::PushDllDirectory(*(FPaths::ProjectDir() / TEXT("Plugins/Chess/ThirdParty/Lib/")));
//或者全路径FPaths::ProjectDir()/TEXT("ThirdParty/CaptureScreen.dll")
ChessHandle = FPlatformProcess::GetDllHandle(TEXT("Dll2.dll"));
UE_LOG(LogTemp, Warning, TEXT("-----------------------------------------"));
FString path = *(FPaths::ProjectDir() / TEXT("Plugins/Chess/ThirdParty/Lib/"));
UE_LOG(LogTemp, Warning, TEXT("%s"), *path);
if (!ChessHandle) {
UE_LOG(LogTemp, Warning, TEXT("Failed to load PDF library."));
}
if (ChessHandle) {
UE_LOG(LogTemp, Warning, TEXT("^_^"));
}
FString procName = "?HelloWorld@DllClass@@QEAAXXZ"; // 创建象棋类
ChessClass chessclass;
chessclass = (ChessClass)FPlatformProcess::GetDllExport(ChessHandle, *procName);
if (!chessclass) {
UE_LOG(LogTemp, Warning, TEXT("test---------------False"));
}
if (chessclass) {
UE_LOG(LogTemp, Warning, TEXT("test---------------True"));
}
void* a = chessclass();
UE_LOG(LogTemp, Warning, TEXT("%d"),a);
#include <iostream>
#include <windows.h>
int main()
{
typedef int (*_pAdd)(int a, int b);
//HMODULE module = LoadLibraryA("tanchuang.dll");
HINSTANCE module = LoadLibrary("Dll2.dll");
int nParam1 = 9;
int nParam2 = 3;
_pAdd pAdd = (_pAdd)GetProcAddress(module, "?HelloWorld@DllClass@@QAEXXZ");
int nAdd = pAdd(nParam1, nParam2);
std::cout << nAdd << ":" << std::endl; FreeLibrary(module);
system("pause");
return 0;
}
两段代码,上面是UE4导入,下面是普通C++导入
必要知识6:
想要理解上面的代码,需要懂得函数指针,可以自行百度
必要知识7:
你的dll的DllMain函数HMODULE等于HINSTANCE
不同编译器可能不太一样我的是vs2019
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
感兴趣可以百度,这里只提供方向