Hi everyone
I have followed ‘Linking DLL’ tutorial and it worked for me however I have problem binding class. Can somebody help me ? Here is my example code.
exampledll.h
#ifndef EXAMPLE_DLL_H
#define EXAMPLE_DLL_H
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(_WIN32)
#ifdef EXAMPLEDLL_EXPORTS
#define EXAMPLE_DLL_API __declspec(dllexport)
#else
#define EXAMPLE_DLL_API __declspec(dllimport)
#endif
#endif
#if defined(_WIN32)
#define EXAMPLE_DLL_APIENTRY __cdecl
#else
#define EXAMPLE_DLL_APIENTRY
#endif
EXAMPLE_DLL_API int exampleNumber; //works
EXAMPLE_DLL_API int EXAMPLE_DLL_APIENTRY addAndGiveMeExampleNumber(int toAdd); //works
class EXAMPLE_DLL_API someFun
{
public:
someFun(){};
~someFun(){};
int callMe(); // would like to call that
};
#if defined(__cplusplus)
}
#endif
#endif
exampledll.cpp
#include "stdafx.h"
#include "ExampleDLL.h"
EXAMPLE_DLL_API int EXAMPLE_DLL_APIENTRY addAndGiveMeExampleNumber(int toAdd)
{
exampleNumber += toAdd;
return toAdd;
}
int someFun::callMe()
{
return 4;
}
In my UE4 project:
gettingDLL.h
#include "[dllPath]ExampleDLL.h"
#include <iostream>
typedef int(*_addAndGiveMeExampleNumber)(int toAdd);
class gettingDLL
{
public:
gettingDLL(){};
~gettingDLL(){};
void init();
//pure test example
void *exampleDLLHandle;
_addAndGiveMeExampleNumber exAddAndGiveMeExampleNumber = NULL;
};
gettingDLL.cpp
void gettingDLL::init()
{
FString exampledllpath = "[dllPath]ExampleDLL.dll";
if (FPaths::FileExists(exampledllpath))
{
FString sAddAndGiveMeExampleNumber = "addAndGiveMeExampleNumber";
exAddAndGiveMeExampleNumber = (_addAndGiveMeExampleNumber)FPlatformProcess::GetDllExport(exampleDLLHandle, *sAddAndGiveMeExampleNumber);
}
else
{
}
}
Bump. I have this exact same issue, and my code looks pretty much the same as above (following this tutorial: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums).
My dll handle is not null, but I’m not able to bind a function.
Any suggestions?
Can you guys explain what you are trying to accomplish?
I’m trying to use Open Sound Control with UE4. There is a DLL that has been written for this purpose, and I’m trying to get UE4 to ‘talk to’ it. The DLL has functions which, when called, will pass information over UDP to another application. I’m trying to call these functions from UE4.
Ok, what is the actual problem? Does it not find the DLL? You may have to copy the third party DLL into the game’s or Engine’s Binaries directory for now.
See Loading DLLs from Plugins - Plugins - Epic Developer Community Forums
The DLL seems to be found, I believe.
I already had put the DLL into the Binaries → Win64 folder.
My code is below, roughly copied from the previously linked tutorial.
this → if (FPaths::FileExists(filePath)) … ← evaluates as true
this → if (DLLHandle != NULL) … ← evaluates as true
this → if (DLLgetCircleArea != NULL) … ← evaluates as false
And yes, I specifically added a method to my DLL called getCircleArea, just for the sake of testing this.
FString filePath = FPaths::Combine(*FPaths::GamePluginsDir(), TEXT("Content/"), TEXT("oscpack_1_0_2.dll")); // get the plugin path, add the folder to contain the dll, and add the dll name
if (FPaths::FileExists(filePath))
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("dll path exists"));
void *DLLHandle;
DLLHandle = FPlatformProcess::GetDllHandle(*filePath); // get dll
if (DLLHandle != NULL)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("dll handle is not 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)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("dll getcirclearea is not null"));
float out = DLLgetCircleArea(radius); // call our dll function
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Circle area: %f"), out);
}
return out;
}
}
}
Is EXAMPLEDLL_EXPORTS defined when you compile your DLL? It looks like the exported function is not found. Also, try opening your DLL in Dependency Walker (http://www.dependencywalker.com) and make sure that the function is exported and has the expected name.
Sorry for some confusion here.
The DLL posted by the OP is not my DLL. I just seem to have a similar problem so I latched on to this thread. My DLL is rather long and I confess that I do not understand all of it because it wasn’t written by me; I’ve just amended it by adding a few functions. So, I don’t believe that the EXAMPLEDLL_EXPORTS applies to me, I’m assuming. But I can’t say that I really know what it does or means, anyway.
However, I CAN say that Dependency Walker sees the getCircleArea function.
Dependency Walker does give one warning: “At least one delay-load dependency module was not found.” And it appears that module is “API-MS-WIN-APPMODEL-RUNTIME-L1-1-0.DLL.”
However, the original DLL has the same warning with Dependency Walker, and that DLL worked perfectly fine in a 32-bit version when I used it with UDK.
Solved.
The issue was that the DLL was not in the Binaries → Win64 folder.
To clarify from a confusion that I had in the comments of the OP, even though I had a copy of the DLL in the Binaries->WIn64 folder, I had neglected to amend the line:
FString filePath = FPaths::Combine(*FPaths::GameDir(), TEXT(“Content/”), TEXT(“oscpack_1_0_2.dll”));
to properly point to the correct directory
I changed that line to:
FString filePath = FPaths::Combine(*FPaths::GameDir(), TEXT(“Binaries/Win64/”), TEXT(“oscpack_1_0_2.dll”));
which now functions as expected.
Sorry to waste your time, gmpreussner, but I appreciate your assistance! And Dependency Walker is a great tool!!!
Hi gmpreussner!
Can you please guide me on how to use dll in UE4? I know how to create a Win32 dll console app in VS 2013. I’m having difficulty in following the instructions giving the DLL linking tutorial (A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums). I did not undestand the part after “Bind DLL to a blueprint function”!
I don’t know where should I declare the header file and source file (as described in the tutorial) after the PART that says “choose file-> add code to project…”
Can you please guide me through the necessary steps. Thank you so much anyways!
Hello Uninventive
I have the same problem as you when you followed ‘Linking DLL’ tutorial.
Do you solve it put your .dll file into the Binaries/Win64 folder? and which elements did you copy into this path? I am not sure if is only necessary the .dll or you copy the .h and .cpp files of the project “oscpack_1_0_2” into this path too
Sorry if I am a noob using the Unreal’s files
regards