UE4.27 C++调用DLL

1.首先在UE C++项目中添加一个x64动态链接库,取名RouteInterface,放到Source下面;
在这里插入图片描述
在这里插入图片描述
2.在Source下面新建两个文件夹Include和Lisbs,这两个文件夹用来保存库的头文件和lib文件
在这里插入图片描述
3.在项目RouteInterface下面添加一个类:TestDll,并且把头文件保存到Include/RouteInterface下面,并且在添加一个导入导出符号的头文件ExportGlobal.h
在这里插入图片描述
ExportGlobal.h

#pragma once

#ifdef ROUTEINTERFACE_EXPORTS
#define EXPORT_LIB __declspec(dllexport)
#else
#define EXPORT_LIB __declspec(dllimport)
#endif

TestDll.h

#pragma once
#include "ExportGlobal.h"

/**
测试使用
*/
class EXPORT_LIB TestDll {
    
    
public:
	int add(int a, int b);
};

TestDll.cpp

#include "pch.h"
#include "RouteInterface/TestDll.h"

int TestDll::add(int a, int b) {
    
    
	return a + b;
}

4.设置配置,头文件包含目录
在这里插入图片描述
5.添加【生成后事件】
在这里插入图片描述

copy $(TargetPath) $(ProjectDir)..\..\Binaries\Win64\
copy $(OutDir)$(TargetName).lib $(ProjectDir)..\Libs\

6.编译以后,RouteInterface.dll生成在:F:\004_Ue4\RotorUAV_26_27\Binaries\Win64 目录下
编译选项:Debug x64 在这里插入图片描述
在这里插入图片描述
RouteInterface.lib生成在F:\004_Ue4\RotorUAV_26_27\Source\Libs目录下
在这里插入图片描述
7.在UE C++工程总添加过滤器,RouteInterface并且把两个头文件添加上去
在这里插入图片描述
8.在RotorUAV.Build.cs中添加

using System.IO;
private string ModulePath 
	{
    
    
        get
        {
    
    
			return ModuleDirectory;
        }
	}

	private string ThirdPartyPath
    {
    
    
        get
        {
    
    
			return Path.GetFullPath(Path.Combine(ModulePath, "../Libs"));
        }
    }
		/**
		 * PublicIncludePaths为通向此模块内部包含文件的所有路径列表,不向其他模块公开(至少有一个包含到Private路径,
		 * 如果要避免相对路径,则回更多)
		 */
		PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "..\\include"));
        //附加库列表,PublicAdditionalLibraries是一组包含其他预编译库的列表(.lib文件的名称列表,包含后缀)...
        PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyPath, "RouteInterface.lib"));

整文件内容:

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

using UnrealBuildTool;
using System.IO;

public class RotorUAV : ModuleRules { 

	private string ModulePath 
	{
        get
        {
			return ModuleDirectory;
        }
	}

	private string ThirdPartyPath
    {
        get
        {
			return Path.GetFullPath(Path.Combine(ModulePath, "../Libs"));
        }
    }

	public RotorUAV(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "UMG","Niagara"});

		PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

		// Uncomment if you are using Slate UI
		// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

		// Uncomment if you are using online features
		// PrivateDependencyModuleNames.Add("OnlineSubsystem");

		// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true

		/**
		 * PublicIncludePaths为通向此模块内部包含文件的所有路径列表,不向其他模块公开(至少有一个包含到Private路径,
		 * 如果要避免相对路径,则回更多)
		 */
		PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "..\\include"));
        //附加库列表,PublicAdditionalLibraries是一组包含其他预编译库的列表(.lib文件的名称列表,包含后缀)...
        PublicAdditionalLibraries.Add(Path.Combine(ThirdPartyPath, "RouteInterface.lib"));
    }
}

9.在类AMainPlayerController中调用
包含头文件:

#include "../Include/RouteInterface/TestDll.h"

调用:

	TestDll testDll;
	int32 a = testDll.add(6, 5);
// Fill out your copyright notice in the Description page of Project Settings.


#include "MainPlayerController.h"
#include "Blueprint/UserWidget.h"
#include "../Include/RouteInterface/TestDll.h"

AMainPlayerController::AMainPlayerController() {
    
    

}

//这个宏是为了显示调试信息使用
PRAGMA_DISABLE_OPTIMIZATION

void AMainPlayerController::BeginPlay() {
    
    
	TestDll testDll;
	int32 a = testDll.add(6, 5);

	if (uavMainHUDAsset) {
    
    
		uavMainHUD = CreateWidget<UUserWidget>(this, uavMainHUDAsset);
	}
	uavMainHUD->AddToViewport();
	uavMainHUD->SetVisibility(ESlateVisibility::Visible);

	//鼠标在游戏和界面中同时起作用
	FInputModeGameAndUI gameAndUI;
	SetInputMode(gameAndUI);
	bShowMouseCursor = true;
}

PRAGMA_ENABLE_OPTIMIZATION

运行选项:Developter Editor 和 Win64
在这里插入图片描述

运行结果:
在这里插入图片描述

aaa

猜你喜欢

转载自blog.csdn.net/wb175208/article/details/128496570
今日推荐