Hi all!
I’m trying to load some methods from a C# .dll, but i’ve been stuck for days.
Before, to get a little familiar with the topic i’ve followed a guide to import a C++ dll and I didn’t have any problems, blueprints nodes are working correctly.
With a C# dll this line returns always NULL:
m_getCsInvertedBoolFromDll = (_getCsInvertedBool)FPlatformProcess::GetDllExport(v_csdllHandle, *procName);
This is what i’ve done:
I’ve created a new "Class Library (.Net Framework), changed the build target with myproject Plugins folder. The DLL contains this code:
namespace CsDllUe
{
public class CreateAndLinkCSFile
{[DllExport(ExportName = "TestFunction", CallingConvention = CallingConvention.StdCall)] static public void TestFunction() { } [DllExport("getCSInvertedBool", CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.Bool)] static public bool getCSInvertedBool(bool boolState) { return !boolState; } [DllExport("getCSCircleArea", CallingConvention = CallingConvention.StdCall)] static public float getCSCircleArea(float radius) { return (3.1416f * (radius * radius)); }
}
So, i’ve created a new Blueprint Function Library that contains this code:
#include "CreateAndLinkDLLProj.h"
#include "CreateAndLinkDLLTutBFL.h"
typedef bool(*_getCsInvertedBool)(bool boolState);
typedef float(*_getCsCircleArea)(float radius);
_getCsInvertedBool m_getCsInvertedBoolFromDll;
_getCsCircleArea m_getCsCircleAreaFromDll;
void* v_dllHandle;
void* v_csdllHandle;
#pragma region Load DLL
//Method to import a C# DLL
bool UCreateAndLinkDLLTutBFL::importCsDLL(FString folder, FString name)
{
FString filePath = *FPaths::ProjectPluginsDir() + folder + "/" + name;
if (FPaths::FileExists(filePath)) {
v_csdllHandle = FPlatformProcess::GetDllHandle(*filePath); // Retrieve the DLL.
if (v_csdllHandle != NULL)
{
return true;
}
}
return false; //Return an error.
}
#pragma endregion Load DLL
// Imports the method getInvertedBool from the C# DLL.
bool UCreateAndLinkDLLTutBFL::importMethodGetCSInvertedBool()
{
if (v_csdllHandle != NULL)
{
m_getCsInvertedBoolFromDll = NULL;
FString procName = "getCSInvertedBool"; // Needs to be the exact name of the DLL method.
m_getCsInvertedBoolFromDll = (_getCsInvertedBool)FPlatformProcess::GetDllExport(v_csdllHandle, *procName);
if (m_getCsInvertedBoolFromDll != NULL)
{
return true;
}
}
return false; // Return an error.
}
// Imports the method getCircleArea from the C# DLL.
bool UCreateAndLinkDLLTutBFL::importMethodGetCSCircleArea()
{
if (v_csdllHandle != NULL)
{
m_getCsCircleAreaFromDll = NULL;
FString procName = "getCSCircleArea"; // Needs to be the exact name of the DLL method.
m_getCsCircleAreaFromDll = (_getCsCircleArea)FPlatformProcess::GetDllExport(v_csdllHandle, *procName);
if (m_getCsCircleAreaFromDll != NULL)
{
return true;
}
}
return false; // Return an error.
}
// Calls the method getInvertedBoolFromDll that was imported from the C# DLL.
bool UCreateAndLinkDLLTutBFL::getCSInvertedBoolFromDll(bool boolState)
{
/*if (m_getCsInvertedBoolFromDll != NULL)
{
bool out = m_getCsInvertedBoolFromDll(boolState); // Call the DLL method with arguments corresponding to the exact signature and return type of the method.
return out;
}
return boolState; // Return an error.*/
return m_getCsInvertedBoolFromDll(boolState);
}
// Calls the method m_getCircleAreaFromDll that was imported from the DLL.
float UCreateAndLinkDLLTutBFL::getCSCircleAreaFromDll(float radius)
{
if (m_getCsCircleAreaFromDll != NULL)
{
float out = m_getCsCircleAreaFromDll(radius); // Call the DLL method with arguments corresponding to the exact signature and return type of the method.
return out;
}
return -32202.0F; // Return an error.
}
The dll is retrieved correctly, but the import methods returns always false (GetDllExport returns NULL).
I’m starting to think that is not possible to read methods from a C# dll.
Someone can help me please?
Thank you.
I’ve already read the following guides/pages:
Linking DLLs - UE4 Guidebook
Is it possible to call a managed .NET dll from UE4 C++ project?