Runtime error when referencing external library in Unreal Engine

0

I followed the steps from this tutorial:

https://wiki.unrealengine.com/How_to_Link_External_C_Libraries_.dll_.lib_With_Your_Project_%26_Package_With_Game,_Fast_And_Easy

I was able to successively reference the external c++ library and the project compiles just fine. However, at runtime, when I instantiate an object from external library, I get the following error:

Exception thrown at 0x00007FFD7244FD31 (mscordacwks.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000000. occurred Source information is missing from the debug information for this module

and the stack is:

mscordacwks.dll!ClrDataAccess::EnumMemDumpAllThreadsStack(enum CLRDataEnumMemoryFlags) Unknown Non-user code. Symbols loaded. mscordacwks.dll!ClrDataAccess::EnumMemoryRegionsWorkerMicroTriage(enum CLRDataEnumMemoryFlags) Unknown Non-user code. Symbols loaded. mscordacwks.dll!ClrDataAccess::EnumMemoryRegionsWrapper(enum CLRDataEnumMemoryFlags) Unknown Non-user code. Symbols loaded. mscordacwks.dll!ClrDataAccess::EnumMemoryRegions(struct ICLRDataEnumMemoryRegionsCallback ,unsigned int,enum CLRDataEnumMemoryFlags) Unknown Non-user code. Symbols loaded. dbgcore.dll!GenGetProcessInfo(unsigned long,struct _MINIDUMP_STATE ,struct _INTERNAL_PROCESS ,struct _LIST_ENTRY *) Unknown Non-user code. Symbols loaded. dbgcore.dll!MiniDumpProvideDump() Unknown Non-user code. Symbols loaded. dbgcore.dll!MiniDumpWriteDump() Unknown Non-user code. Symbols loaded. UE4Editor-Core.dll!00007ffdb17e95fb() Unknown No symbols loaded. UE4Editor-Core.dll!00007ffdb17ded6a() Unknown No symbols loaded. UE4Editor-Core.dll!00007ffdb17d2865() Unknown No symbols loaded. UE4Editor-Core.dll!00007ffdb17e0864() Unknown No symbols loaded. [External Code] Annotated Frame

the line where the error occurs:

CSharpClassExposer* scharpClassExposer = nullptr;
scharpClassExposer = new CSharpClassExposer();

The external library that I am referencing is C++ CLI (managed C++) which further wraps C# dll and I am sure it has something to do with this. However, in my separate test project where I use the same managed library with native c++ console client instead of Unreal Engine, it all works fine.

Any idea how to solve this?

c++
unreal-engine4
asked on Stack Overflow Mar 8, 2018 by Ivan

1 Answer

2

I've got stuck with the same issue as yours ( i've got exception from Windows core dlls ) when i called basic method/functions/constructor of external libraries.

I had to tell to the Engine which DLL has to be load, corresponding to the .lib you gave to the array PublicAdditionalLibraries

PublicDelayLoadDLLs is another Array you can access from your Build.cs and you have to fill it with every .dll you want to link.

I've something like this in my Build.cs :

PublicAdditionalLibraries.Add(Path.Combine(LibrariesPath, "myExternalLib.lib")); PublicDelayLoadDLLs.Add("myExternalLib.dll");

Then when your Module is loaded, you have to tell the Engine which DLL it has to load. Eg, In your void MyModule::StartupModule() method you have to set the Libraries folder

FString AbsPath = FileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*MyLibPath); FPlatformProcess::AddDllDirectory(AbsPath)

Then ask to load the library :

void* DLLHandle3 = FPlatformProcess::GetDllHandle( L"myExternalLib.dll" );

These steps fix the Exception thrown when i used some of the external libraries methods.

If i've well understood what i've found on the Unreal Forum, The engine use the .lib to compile and know which function exists, but need the .dll to load dynamically all dependencies not included in the final executable. ( kind of weird for a cpp developer, that's why i'm not sure about this part )

Hope it helps

answered on Stack Overflow Mar 15, 2018 by Marcassin • edited Jun 16, 2020 by yoyo

User contributions licensed under CC BY-SA 3.0