UE5导入第三方动态链接库dll

Build.cs文件里面加上"Projects"模块,要不然报错

// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"

class FOpenFacePlusModule : public IModuleInterface
{
public:

	/** IModuleInterface implementation */
	virtual void StartupModule() override;
	virtual void ShutdownModule() override;

	/** Handle to the test dll we will load */
	void* OpenFaceHandle;
};

加载模块这里添加个指针,存dll的句柄

// Copyright Epic Games, Inc. All Rights Reserved.

#include "OpenFacePlus.h"
//dll
#include "Core.h"
#include "Modules/ModuleManager.h"
#include "Interfaces/IPluginManager.h"

#define LOCTEXT_NAMESPACE "FOpenFacePlusModule"

void FOpenFacePlusModule::StartupModule()
{
	//加载dll
	FString BaseDir = IPluginManager::Get().FindPlugin("OpenFacePlus")->GetBaseDir();
	FString LibraryPath;
#if PLATFORM_WINDOWS
	LibraryPath = FPaths::Combine(*BaseDir, TEXT("Binaries/ThirdParty/OpenFaceDll.dll"));
#endif // PLATFORM_WINDOWS
	FPlatformProcess::PushDllDirectory(*(FPaths::ProjectDir() / TEXT("Plugins/OpenFacePlus/Binaries/ThirdParty/")));
	OpenFaceHandle = !LibraryPath.IsEmpty() ? FPlatformProcess::GetDllHandle(*LibraryPath) : nullptr;
	if (OpenFaceHandle)
	{
	}
	else
	{
		FMessageDialog::Open(EAppMsgType::Ok, LOCTEXT("ThirdPartyLibraryError", "Failed to load example OpenFaceDll library"));
	}
}

void FOpenFacePlusModule::ShutdownModule()
{
	//卸载dll
	FPlatformProcess::FreeDllHandle(OpenFaceHandle);
	OpenFaceHandle = nullptr;
}

#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FOpenFacePlusModule, OpenFacePlus)

然后这就加载好了,下面是调用

#include "Misc/MessageDialog.h"
#include "Modules/ModuleManager.h"
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"

创建函数指针,或者用头文件更好

typedef int (*runOpenFace)(int argc, char** argv);
FModuleManager& fmode = FModuleManager::Get();
	FOpenFacePlusModule* mode = (FOpenFacePlusModule*)fmode.GetModule("OpenFacePlus");

	FString procName = "runOpenFace";
	runOpenFace ssss = (runOpenFace)FPlatformProcess::GetDllExport(mode->OpenFaceHandle, *procName);
int a = ssss();
	UE_LOG(LogTemp, Warning, TEXT("OpenFaceDll return :%d"), a);

这样就从模块那边获取到dll句柄了,然后就调用就好了

这里说一下静态的库

需要lib,头文件如果很多的话容易报错的,尤其是opencv,dilb之类的库,和ue有点冲突,静态链接很容易被坑到

这里补充一点:

dll的Debug模式与Release差别很多

下图是我计算象棋算法的耗时

UE5导入第三方动态链接库dll_第1张图片

可以看到相差十秒的时间,所以感觉太慢的话有可能是这个原因导致的

 

你可能感兴趣的:(ue5,游戏引擎,虚幻)