Adding third party DLL path for plugin

I am using a third party dll with import library and .h file , so far I am able to get it working if I place the dll in the binaries folder of the game project , however this is undesirable for a plugin.

but if I move it somewhere else and put the path in the build.cs file then UE4 starts to look for the dll in standard OS locations and hence complains about missing DLL.

How should I add my dll in build.cs so that the plugin knows where to find it (Plugin folder … thirdparty etc) ?

P.S I’m not using function pointer approach, instead calling function from header file using the import library.

1 Like

Ok so to return the help I got from various sources of the community I’ll list the procedure for linking a third party dll with import library for a UE4 plugin.

Import library avoids the mess that happens with function pointers instead you can call functions directly using a header

Its mostly straightforward but lack of docs made me confused.

So a DLL with import library will come with three files minimum:

1.The DLL itself

2.The .h header file that declares all the functions of the DLL that you can call

3.The .lib file or import library which contains address of the header file functions in the DLL

For this example my library was called free image , you can find it here
Here’s what you got to do:

1. In your plugin’s build.cs add the following lines:

PublicDelayLoadDLLs.Add("FreeImage.dll");

PublicAdditionalLibraries.Add(Path.Combine(FreeImageDirectory, "FreeImage.lib"));

Here the FreeImageDirectory is a string pointing to the location of the folder where .lib file is located respective to the build.cs file of the module, so in this example FreeImageDirectory = “…//…//ThirdParty//FreeImage”

Since our DLL is third party custom lib and cannot be found among standard windows dlls The line PublicDelayLoadDLLs avoids the engine from trying to load it immediately on startup, as engine would by default look in standard windows locations to search our DLL (Which it obviously wont find)

Now since our DLL is in one of the folders in our plugin (you can use any folder , I used third party for consistency)
We will load that DLL manually from the correct path as soon as the plugin module is loaded by the engine

here’s how

2 In your plugin’s main cpp file (like for a plugin named ABCD you’ll have a ABCD.cpp)

you’ll find a function named StartupModule which is called when module is loaded by the engine

all you have to do is load our DLL from the folder using *DLLHandle = FPlatformProcess::GetDllHandle(Path);

where path indicates absolute location of our DLL file.

you can use IPluginManager::Get().FindPlugin(“PluginName”)->GetBaseDir(); to help in getting the path

as soon as you call *DLLHandle = FPlatformProcess::GetDllHandle(Path); the engine will load your dll from your desired location

make sure to call FPlatformProcess::FreeDllHandle(DLLHandle); to release the DLL in the function ShutdownModule

3 In your project put the .h that came with DLL amongst the other headers , e.g I put my FreeImage.h in the private folder of my plugin source code

done

if everything went as expected you can just include the header in any file in the plugin and call any of the function from DLL directly as if you had the source code at your disposal, no messy function pointers

Hope this helps

3 Likes

i want to call .dll functions but i don’t know where and how to call.

If you’ve done something like in my answer , then you need to just add the header files for the DLL and call the functions from there. Or if you are using a different method of calling DLL (Function pointers and stuff) then you should look at VLC plugin on the community

Hi Command Shepard, I’m currently just trying to add an external library to my project. I follow the initial procedure you have here, but my ABCD.cpp file doesn’t have anything written in it. Maybe it’s because this is a generic project and not a plugin? :confused: I can actually include the header in my code and it doesn’t break, but the moment I call any function it doesn’t even load the project… I thought that maybe the DLLs are not being found… It just gives an error saying that the module may not be properly set up… Do you have any idea why this might occur?
Any feedback would be greatly appreciated! Thanks!

hmm, seems like you haven’t added the DLL to PublicDelayLoadDLLs or equivalent leading the engine to search the DLL as soon as your module (Game) loads , by default it’ll search in system32 folder where your dll probably isn’t (not that you’d want it to be there anyways).

As I understand you are trying to load DLL for a game from its main game module rather than a plugin and so you are unable to find the startup module function for your primary game module!

However its not necessary to load the DLL in that exact function you can load it anytime when you like in any function like BeginPlay or anything , just make sure to load it before you call any functions from it and unload (release the DLL Handle) when your game exits.

But, If you really want to load it on module startup (probably safer way) then read on:

in your ABCD.cpp file you might have something similar to following

IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, ABCD, "ABCD" );

Now in ABCD.h you can define a class say FWachrnoGame which extends FDefaultGameModuleImpl and override the StartupModule function there and add all your dll loading in there.

Finally in the ABCD.cpp file edit the Macro to look like following

IMPLEMENT_PRIMARY_GAME_MODULE( FWachrnoGame, ABCD, "ABCD" );

That should do it. Sources

woooohooooo it worked :smiley: After I managed to understand how to set up that class! Thanks for the awesome help!

I tried your method, but it doesn’t work.

Tried this method, it works. commenting so I can see it in my history later - the delayed load DLL was my issue.

I have the same problem that you had. I have C++ shared library created from matlab. i have .dll .lib & .h files.I want to use it inside one of my functions. can you help me on how to set up this to work please?

Commander Shepard! You are a Hero to me.

I am still running into an issue
I did this in my plugins .build.cs file

 PublicDelayLoadDLLs.Add("myDll.dll");
 
 PublicAdditionalLibraries.Add(Path.Combine(libpath, "myLib.lib"));

And I added all the dlls to binaries folder inside my plugin folder.
But I am getting compile time error when trying to call any of the function in my library that I am trying to add using the Dlls.
I understand that GetDllHandle is required for runtime issue. But how do I fix the compile time issues related to the library. I added the header files as well