UE4.27 C++ calling DLL

1. First add a x64 dynamic link library in the UE C++ project, name it RouteInterface, and put it under Source;
insert image description here
insert image description here
2. Create two folders under Source, Include and Lisbs, which are used to save the header files of the library And lib file
insert image description here
3. Add a class under the project RouteInterface: TestDll, and save the header file under Include/RouteInterface, and add a header file ExportGlobal.h ExportGlobal.h that imports and exports
insert image description here
symbols

#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. Set the configuration, the header file contains the directory
insert image description here
5. Add [post-generation event]
insert image description here

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

6. After compiling, RouteInterface.dll will be generated in: F:\004_Ue4\RotorUAV_26_27\Binaries\Win64 directory
Compilation option: Debug x64 insert image description here
insert image description here
RouteInterface.lib will be generated in F:\004_Ue4\RotorUAV_26_27\Source\Libs directory
insert image description here
7. In UE C++ The project always adds filters, RouteInterface and adds two header files.
insert image description here
8. Add in 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"));

Whole file content:

// 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. Call in the class AMainPlayerController to
include the header file:

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

transfer:

	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

Run Options: Developer Editor and Win64
insert image description here

operation result:
insert image description here

aaa

Guess you like

Origin blog.csdn.net/wb175208/article/details/128496570