Hello, I ultimately want to get Lua working in UE4 for end-user scripting purposes. To this end I have followed the wiki tutorial on linking DLLs as a first/intermediate step. I can nearly complete the tutorial, but fail to actually get the DLL function.
I created a win64 release DLL called “KarmasDLLs.dll”, with symbol exporting. In it is this function:
KARMASDLLS_API float getCircleArea(float radius)
{
return (float)3.1415926535 * (radius*radius);
}
I compiled this DLL and placed it in my UnrealProjectDirectory/Plugins/KarmasDLLs/ folder. My project is a modification of the C++ quick start guide’s project. I added a ULibraries class as the tutorial instructs. Actually, the tutorial was slightly ambiguous & I am a noob, and so I created ULibraries.cpp & ULibraries.h instead of Libraries.cpp & Libraries.h. Consequently, my relevant class is UULibraries. I will fix this once I understand how to grab/use the DLL function.
My ULibraries.h is:
#pragma once
#include "GameFramework/Actor.h"
#include "ULibraries.generated.h"
UCLASS()
class UULibraries : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
UFUNCTION(BlueprintCallable, Category = "Karma Libraries")
static float getCircleAreaDLL(float radius);
};
While the ULibraries.cpp is:
#include "BasicClass.h"
#include "ULibraries.h"
typedef float(*_getCircleArea)(float radius); //our typedef is must be the same type of our dll function.
UULibraries::UULibraries(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
}
float UULibraries::getCircleAreaDLL(float radius)
{
FString filePath = FPaths::Combine( *FPaths::GamePluginsDir(), TEXT("KarmasDLLs/"), TEXT("KarmasDLLs.dll")); // get the plugin path, add the folder to contain the dll, and add the dll name
if (FPaths::FileExists(filePath))
{
void *DLLHandle;
DLLHandle = FPlatformProcess::GetDllHandle(*filePath); // get dll
if (DLLHandle != NULL)
{
_getCircleArea DLLgetCircleArea = NULL; // the container for our dll function
FString procName = "getCircleArea"; // the exact name of our dll function to recover
DLLgetCircleArea = (_getCircleArea)FPlatformProcess::GetDllExport(DLLHandle, *procName); // get the dll function need
if (DLLgetCircleArea != NULL)
{
float out = DLLgetCircleArea(radius); // call our dll function
return out; // return to UE
} else {
return -3.00f;
}
} else {
return -2.00f;
}
}
return -1.00f;
}
When I play the game -3 is printed to the screen (I elsewhere call this function and print the result to screen, of course). This indicates that the following function call returns NULL:
(_getCircleArea)FPlatformProcess::GetDllExport(DLLHandle, *procName);
What have I done wrong? What can I do to fix this? Are there any other resources on how to work with DLLs than that lone tutorial and a scant few scattered Unreal Answers posts? As far as I can tell all of the arguments I am supplying to that function are correct.
Thank you!