ue4蓝图调用c++dll动态库

ue4蓝图调用c++dll动态库

最近公司要用ue4做一些项目,对于我是写java的来说,属于半路出家,研究好久的ue4蓝图调用c++dll库的功能,记录一下。

创建c++dll库

首先我们需要一个c++的dll文件,没有就自己创建。下面是我创建的详细过程。

  1. 使用软件:vs2019
  2. 首先创建项目,选择c++动态链接类库。然后点击下一步直接创建就行,很简单。在这里插入图片描述
  3. 项目创建完以后,新建类,我创建的名字是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;
}

  1. 在.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

  1. 这样基本就可以了,然后生成项目,在项目目录里找到\x64\Release文件夹,找到dll文件在这里插入图片描述

  2. 然后新建ue项目,我是直接创建第三人称的项目测试的。在ue4项目创建Plugins/MyTutoriaDlls文件,这里文件名应该是可以自取的,但是我是看别人用这个,我也跟着用,最后把dll文件拷贝到这个文件夹中

  3. 在这里插入图片描述
    进入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文件,不同项目名的文件夹名称是不一样的,这里要看你自己创建的项目名)
在这里插入图片描述
在这里插入图片描述
保存,然后再运行就可以了。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值