I've spent a lot of time figuring our how to set up UELibrary for my custom project. The official documentation is helpful but unfortunately incomplete. I've compiled all of my findings in one post + added an example on how you can extend the base API.
Our goal is to build a game as a shared .dll library that we can use in an external Win32 application.
Follow the following steps:
-
Clone the Unreal Engine source code.
-
Find this line of code in SlateApplication.cpp and remove it:
// If we are embedded inside another app then we never need to be "active" bAppIsActive = !GUELibraryOverrideSettings.bIsEmbedded;
This will fix the mouse bug.
-
(Optional) Find this line of code in DesktopPlatformBase.cpp and replace it:
// Append any other options Arguments += " -Progress -NoEngineChanges -NoHotReloadFromIDE";
with this line:
// Append any other options Arguments += " -Progress";
This will solve the "Engine modules are out of date" error when you open your projects in a custom built UnrealEditor.
-
Compile the Unreal Engine source code. Run Setup.bat, GenerateProjectFiles.bat, open UE5.sln. Select Development Editor configuration, right click on UE5 and press Build.
-
Open Engine\Binaries\Win64\UnrealEditor.exe. Create a new Blank C++ project (no starter content required). Let's name it UELibExtended.
-
Navigate to your project location and open UELibExtended.sln. Find UELibExtended\Source\UELibExtended.Target.cs and modify it:
// Copyright Epic Games, Inc. All Rights Reserved. using UnrealBuildTool; using System.Collections.Generic; public class UELibExtendedTarget : TargetRules { public UELibExtendedTarget( TargetInfo Target) : base(Target) { Type = TargetType.Game; DefaultBuildSettings = BuildSettingsVersion.V2; IncludeOrderVersion = EngineIncludeOrderVersion.Unreal5_1; // new lines of code GlobalDefinitions.Add("UE_LIBRARY_ENABLED=1"); LinkType = TargetLinkType.Monolithic; bShouldCompileAsDLL = true; ExtraModuleNames.AddRange(new string [] { "UELibExtended", "UELibrary"}); } }
This will build your game as a monolith .dll file and expose
UELibrary_
(e.g.UELibrary_WndProc
) functions. -
Select Development Editor configuration, right click on UELibExtended project and build it. This will generate the necessary modules for the UE5 Editor.
-
Open UELibExtended.uproject and cook the content (Platforms -> Windows -> Cook Content). This will transform raw assets into platform specific packages.
-
Select Development configuration and compile UELibExtended project. This will generate
UELibExtended\Binaries\Win64\UELibExtended.dll
library. and correspondingUELibExtended.lib
file -
Open Developer Command Prompt for VS 2022 from the Windows Start menu. Run these commands:
cd path\to\UELibExtended.dll dumpbin /exports UELibExtended.dll
Look for
UELibrary_InitA
,UELibrary_InitW
,UELibrary_Shutdown
,UELibrary_Tick
andUELibrary_WndProc
symbols. If they are missing make sure that you've saved the UELibExtended.Target.cs file and compiledUELibExtended
project in Development, not Development Editor configuration. -
Clone UELibApp project. This is a Win32 app that will embed the UELibExtended game.
Inside of this repo you will find the UELib folder:
UELibApp\ UELib\ include\ lib\
I have configured
UELibApp
project to search for header files in theinclude
folder and look forUELibExtended.lib
file in thelib
folder. -
Copy
UnrealEngine\Engine\Source\Runtime\UELibrary\Public\UELibraryAPI.h
toUELibApp\UELib\include
. -
Modify the path to your
UELibExtended.uproject
file in theUELibApp.cpp
:// replace ..\\..\\..\\UELibExtended\\UELibExtended.uproject with the full path to your .uproject int Error = UELibrary_Init(hInstance, hWnd, L"..\\..\\..\\UELibExtended\\UELibExtended.uproject");
-
Copy
UELibExtended\Binaries\Win64\UELibExtended.lib
toUELibApp\UELib\lib
. -
Open UELibApp.sln, change the configuration to Release and compile it.
-
Copy all .dll files from
UELibExtended\Binaries\Win64
toUELibApp\x64\Release
and run the project.
You need to repeat the last 3 steps every time you recompile UELibExtended game. You can automate this with PostBuild steps. I'm too lazy to learn it lol.
You can expose more functions to the external application. The official documentation is very vague on what to do so we will craft our own example. Let's expose a UELibExtended_CountActors
function that will return a number of actors in the current level.
-
Create blank
UELibExtended\Source\UELibExtended\UELibExtendedApi.h
andUELibExtended\Source\UELibExtended\UELibExtendedApi.cpp
files. -
Generate project files by right clicking on
UELibExtended.uproject
and selecting the Generate Visual Studio project files option. -
Modify header file
UELibExtendedApi.h
:// Copyright Epic Games, Inc. All Rights Reserved. #pragma once #ifdef UELIBEXTENDED_LIBRARY_DLL_EXPORT #define UELIBEXTENDEDLIBRARYAPI __declspec(dllexport) #else #define UELIBEXTENDEDLIBRARYAPI __declspec(dllimport) #endif extern "C" { UELIBEXTENDEDLIBRARYAPI int UELibExtended_CountActors(); }
-
Modify source file
UELibExtendedApi.cpp
:// Copyright Epic Games, Inc. All Rights Reserved. #define UELIBEXTENDED_LIBRARY_DLL_EXPORT #include "UELibExtendedApi.h" #undef UELIBEXTENDED_LIBRARY_DLL_EXPORT #include "EngineMinimal.h" UELIBEXTENDEDLIBRARYAPI int UELibExtended_CountActors() { if (!GEngine) { return 0; } for (const FWorldContext& Context : GEngine->GetWorldContexts()) { if (Context.WorldType == EWorldType::Game) { return Context.World()->GetActorCount(); } } return 0; }
-
Compile
UELibExtended
project in Development configuration. Examine UELibExtended.dll with the dumpbin command and make sureUELibExtended_CountActors
is exposed. -
Copy
UELibExtended\Source\UELibExtended\UELibExtendedApi.h
toUELibApp\UELib\include
-
Copy
UELibExtended\Binaries\Win64\UELibExtended.lib
toUELibApp\UELib\lib
. -
Copy all .dll files from
UELibExtended\Binaries\Win64
toUELibApp\x64\Release
and run the project. -
Compile
UELibApp
project and run it. If there are some errors make sure that you've copiedUELibExtendedApi.h
,UELibExtended.lib
andUELibExtended.dll
files as described above. -
Modify the
UELibApp.cpp
to include the newUELibExtendedApi.h
header. Modify Windows Message Processing callback to show the actor count on right mouse button click:// include new header #include "UELibExtendedApi.h" // a lot of code here... // modify WM_RBUTTONUP in WndProc function case WM_RBUTTONUP: { OutputDebugStringVar(L"Actor count :%d", UELibExtended_CountActors()); return UELibrary_WndProc(hWnd, message, wParam, lParam); } break;
-
Compile
UELibApp
project and run it. Click the right mouse button to spawn a dialog box.