ue4蓝图调用c++dll动态库
最近公司要用ue4做一些项目,对于我是写java的来说,属于半路出家,研究好久的ue4蓝图调用c++dll库的功能,记录一下。
创建c++dll库
首先我们需要一个c++的dll文件,没有就自己创建。下面是我创建的详细过程。
- 使用软件:vs2019
- 首先创建项目,选择c++动态链接类库。然后点击下一步直接创建就行,很简单。
- 项目创建完以后,新建类,我创建的名字是CreateAndLinkDLL。这时候会有两个文件,一个.cpp,另一个是.h。
4.在.cpp文件里面写入代码。
#pragma once
#include “pch.h”
#include “CreateAndLinkDLL.h”
//Exported method that iterates a given int value.
int getIntPlusPlus(int a,int b)
{
return a+b;
}
- 在.h文件写入一下代码。
#pragma once
#define DLL_EXPORT __declspec(dllexport) //shortens __declspec(dllexport) to DLL_EXPORT
#ifdef __cplusplus //if C++ is used convert it to C to prevent C++'s name mangling of method names
extern “C”
{
#endif
int DLL_EXPORT getIntPlusPlus(int a,int b);
#ifdef __cplusplus
}
#endif
-
这样基本就可以了,然后生成项目,在项目目录里找到\x64\Release文件夹,找到dll文件
-
然后新建ue项目,我是直接创建第三人称的项目测试的。在ue4项目创建Plugins/MyTutoriaDlls文件,这里文件名应该是可以自取的,但是我是看别人用这个,我也跟着用,最后把dll文件拷贝到这个文件夹中
-
进入ue4项目后,创建c++类,选择图中这个,可以用蓝图调用
创建以后会自动打开vs2019,里面会生成两个文件,.cpp和.h文件
在.h文件,写下一下代码,注意文件名称#pragma once#include “CoreMinimal.h”#include “Kismet/BlueprintFunctionLibrary.h”#include “MyBlueprintFunctionLibrary.generated.h”/**
*/
UCLASS()
class TEST1_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
UFUNCTION(BlueprintCallable, Category = “My DLL Library”)
static bool importDLL(FString folder, FString name);
UFUNCTION(BlueprintCallable, Category = "My DLL Library")
static bool importMethodGetIntPlusPlus();
UFUNCTION(BlueprintCallable, Category = "My DLL Library")
static int getIntPlusPlusFromDll(int a,int b);
UFUNCTION(BlueprintCallable, Category = "My DLL Library")
static void freeDLL();
};
在.cpp文件写下一下代码,主要名称和.h里的一致
// Fill out your copyright notice in the Description page of Project Settings.
#include “MyBlueprintFunctionLibrary.h”
#include <Runtime/Core/Public/Misc/Paths.h>
typedef int(*_getIntPlusPlus)(int a,int b);//Declare a method to store the DLL method getCircleArea.
_getIntPlusPlus m_getIntPlusPlusFromDll;
void* v_dllHandle;
#pragma region Load DLL
// Method to import a DLL.
bool UMyBlueprintFunctionLibrary::importDLL(FString folder, FString name)
{
FString filePath = *FPaths::GamePluginsDir() + folder + “/” + name;
if (FPaths::FileExists(filePath))
{
v_dllHandle = FPlatformProcess::GetDllHandle(*filePath); // Retrieve the DLL.
if (v_dllHandle != NULL)
{
return true;
}
}
return false; // Return an error.
}
#pragma endregion Load DLL
#pragma region Import Methods
bool UMyBlueprintFunctionLibrary::importMethodGetIntPlusPlus()
{
if (v_dllHandle != NULL)
{
m_getIntPlusPlusFromDll = NULL;
FString procName = “getIntPlusPlus”; //函数名
m_getIntPlusPlusFromDll = (_getIntPlusPlus)FPlatformProcess::GetDllExport(v_dllHandle, *procName);
if (m_getIntPlusPlusFromDll != NULL)
{
return true;
}
}
return false; // Return an error.
}
#pragma endregion Import Methods
#pragma region Method Calls
int UMyBlueprintFunctionLibrary::getIntPlusPlusFromDll(int a,int b)
{
if (m_getIntPlusPlusFromDll != NULL)
{
int out = int(m_getIntPlusPlusFromDll(a,b)); // Call the DLL method with arguments corresponding to the exact signature and return type of the method.
return out;
}
return -32202; // Return an error.
}
#pragma endregion Method Calls
#pragma region Unload DLL
// If you love something set it free.
void UMyBlueprintFunctionLibrary::freeDLL()
{
if (v_dllHandle != NULL)
{
m_getIntPlusPlusFromDll = NULL;
FPlatformProcess::FreeDllHandle(v_dllHandle);
v_dllHandle = NULL;
}
}
#pragma endregion Unload DLL
最后保存,然后在蓝图中调用就可以了,
运行之后没问题,接下来就是打包了,打包完之后文件目录如下,这时候我们再运行打包好的程序,发现dll文件调用是失败的,其实是因为打包后的项目没有找到dll文件。
这时我们需要把之前在项目创建的放置dll文件的Piugins文件夹拷贝到打包好的test1文件中,(我的项目名是test1,所以是test1文件,不同项目名的文件夹名称是不一样的,这里要看你自己创建的项目名)
保存,然后再运行就可以了。