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.
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:
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
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
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
Follow this question
Once you sign in you will be able to subscribe for any updates here