Hello,
Right now I am trying to learn how to load third party libraries in a correct way. For test purpose I have build two dll, one in C# that does the logic, one in c++ CLR with dllexport that wraps around the .NET library
The C# library code
namespace NumGenerator
{
public static class NumGen
{
public static int] GenerateNumbers()
{
int] res = new int[100];
for(int i = 0; i < res.Length; i++)
{
res* = i * i;
}
return res;
}
}
}
C++ h file
#pragma once
extern "C" {
__declspec(dllexport) void __cdecl GetNumbers(int* nums, int& count);
}
C++ cpp file
#include <memory>
void __cdecl GetNumbers(int* nums, int& count)
{
array<int>^ numsManaged = NumGenerator::NumGen::GenerateNumbers();
pin_ptr<int> numNative = &numsManaged[0];
int* res = numNative;
count = numsManaged->Length;
std::memcpy(nums, res, sizeof(int) * count);
}
And then I call it in plugin
//typedef void (*GetNumFunc)(int*, int&);
FString LibraryPath = TEXT("NativeClrLibrary.dll");
void* handle = FPlatformProcess::GetDllHandle(*LibraryPath);
GetNumFunc getNum = (GetNumFunc)FPlatformProcess::GetDllExport(handle, L"GetNumbers");
if (getNum != NULL) {
int a[200];
int count;
getNum(a, count);
}
I am loading the c++ dll in a plugin that loads c# dll, but I managed it only in a very specific way, I can’t seem to find any information on this topic and it would be great if someone would clarify.
- How is Build.cs connected with dlls? I tried adding to PublicRuntimeLibraryPaths, RuntimeDependencies, PublicDelayLoadDLLs but none of them did affect the result in any way. It still only loads if dll is in Binaries folder or a full path is specified. Is there any way i can change the lookup path (setting PATH vatiable doesn’t do anything either)?
- The second one was very tricky. It turned out that lookup path for c++ dll was different than FPlatformProcess::GetDllHandle, it looked only in Engine/Binaries directory,so I had to copy the C# dll there, so my C++ dll could link to it. When I did all these steps, the code worked, but it doesn’t seem the right solution to me.
- What is the proper way to install dlls from a plugin? I have found people simply copying libraries using the Build.cs file. Is there any other advised method for doing such things.
With the method I’m stuck right now I can’t understand how should I structure my future plugin, so that other people could use it in their projects and how will it work in release where no Engine folder exists?