Noob Trying to call function from DLL

Hello,

I have been following the Wiki Tutorial here, its a great tutorial but of course I’m having trouble with my DLL.

Here is a code snippet:

if (FPaths::FileExists(filePath))
{
	void *DLLHandle;
	DLLHandle = FPlatformProcess::GetDllHandle(*filePath); // retrieve the DLL
	if (DLLHandle != NULL)
	{
		_Return5 DLLReturn5 = NULL;
		FString procName = "Return5";
		DLLReturn5 = (_Return5)FPlatformProcess::GetDllExport(DLLHandle, *procName);
		if (DLLReturn5 != NULL)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(DLLReturn5()));
		}
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("DLL ERROR!!"));
	}
}
else
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("DLL NOT FOUND!!"));
}

The code above is pretty much straight from the Wiki except I’ve added some debug messages. I get the “DLL ERROR!!” message which means GetDllExport was unable to retrieve my function.

My question is: How do I debug this? I’m pretty new with Unreal so I’m not quite sure how to go about this. Is there a way to see why GetDllExport failed? Surely this engine has error messages somewhere…right?

Additional details: The function in my DLL is a simple function which returns the number 5 as an int while this is a simple function created for testing, the DLL has other more complex functions. I have typed the function name correctly.

Someone please help me.

Just to be clear, if you’re seeing “DLL ERROR”, it’s FPlatformProcess::GetDllHandle that is failing, not GetDllExport. If you have engine source code (which you should have if you’re on 4.7, you should be able to step into that function and see why it’s failing.

Ugh, sorry about that, was pretty tired when I posted this.

Back on topic, I supposed my question should have been about debugging techniques in Unreal.

Am I right in saying debugging in Unreal (in situations like this) is basically just jumping straight into the engine source and figuring stuff out? (I am totally okay with that!)

At this time, most of my experience is with Unity and they throw errors and warnings to the console whenever something is wrong. Which I guess they have to, since there is no source access, but does Unreal do anything similar to this at all?

Are there any sources on debugging in Unreal?

I’m using 4.6.1, built from source. Oh another question, why not, is the documentation for previous versions of the engine hosted somewhere? My beloved 4.6 docs got replaced by new 4.7 docs.

If you’ve got the engine built from source, then that’s great - you can just go right ahead and step through any code you need!

We do log warnings and errors to the output log (in visual studio, or Window → Developer → Output Log in UE4 Editor), so there might be something in there.

There’s a little article here which explains how you can get set up with the Visual Studio debugging visualizers which help debugging a lot.

I think I figured out what the problem was, I was trying to load a 32 bit DLL. I will have to create a 64 bit version of my DLL and try again.

Hopefully that was the only issue, I will post another update when I make more (or less) progress.

Ah yeah, you’d have to have an x64 compiled dll to load into a 64 bit process. Let us know if there’re any other issues…

Just in the interest of visibility, this seems to have been due to trying to load a 32bit dll into an x64 process, which isn’t possible. (see comments)

Hey, sorry, took a bit of a break on this project, I’m now back to it. I built a x64 version of the DLL and it was able to load the DLL without any problems. However the function call to FPlatformProcess::GetDllExport failed, the reason for this is because the function names in my DLL are mangled and I was passing in the unmangled function name. When I pass the mangled name, I was able to successfully call my function from the DLL.

I have a follow up question, this isn’t specific to UE4 at all, but I hope someone could help me out.

I know I can build the DLL with unmangled names by specifying extern “C”, however in my case this isn’t really an option as my DLL has heavy usage of templates so I can’t compile with that specifier. So my question is, is it “normal” to call FPlatformProcess::GetDllExport with a mangled function name? I know it works, but is this good practice? It seems kinda sketchy because if I change my DLL’s source, the compiler may generate a different mangled name which means I’d have to edit my call to GetDllExport. If this isn’t good practice, what should I do instead?

Reminder: i am noob, all insights are appreciated!

Generally if you’re trying to interact with a DLL manually with proc handles and the likes, you only really want to deal with a handful (or better still, one/two) bridges between the two libraries.
If you actually link to the object then you can let the compiler deal with most of this gubbins :slight_smile:

Perhaps you could post what your intention is? It’s possible there’s a simpler way forward :slight_smile:

I’m trying to extend the UE to support an alternate form of input. I have a DLL which processes the input and the plan is to write a class in Unreal which communicates with the DLL, interprets the data and has UE4 perform the requested action.

I have done this before in Unity, I’m trying to transition over to UE4

In Unity, to call a function from a DLL you must load the the process (using its equivalents of GetDllExpots and GetDllHandle). I had a crazy realization… my DLL is in C++, Unreal compiles C++, so instead of performing run time dynamic linking by calling those barbaric GetDll functions, I used load time dynamic linking. Here is a nice resource

As you say, let the compiler deal with it :wink:

Currently I am able to have my alternate input method interact with UE4, but Im doing it in the sketchiest way. I created an Actor class and in the Tick function I invoke my DLL’s update function. Here is the code:

void AFilmTIMEDLLActor::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

	FVector lPos = updateFT(); // DLL function

	SetActorLocation(lPos);
}

Super simple I know, baby steps :slight_smile:

Now this is less than optimal because as expected, this means my DLL only works when the game is playing. It should be updating all the time, even if the game isn’t playing. This is something I need to fix in the future, but again, baby steps.

Also another problem I had was I couldn’t get Unreal to find my DLL, on engine startup it would throw a DLL not found error and terminate itself. My less than optimal solution was to put my DLL in the same directory as UE4Editor.exe.

Another problem I currently have is my DLL only works once, that is if I press play my updateFT function (above) works. If I stop and play again, it doesn’t, need to restart UE4.

Problems man. Im learning though :slight_smile:

So I’m not sure how familiar you are with Unreal’s module structure, but for editor builds, we actually build every module as a DLL and load them dynamically at runtime. You can read a little more about this here

It sounds like you might get a lot more mileage by simply compiling your external DLL as part of the unreal build chain by porting it to a module. That is of course if you actually have control over your external DLL, and it’s not some third party thing?

I’m not very familiar with the Unreal modular structure…yet. That link is a very nice reference, thanks!

I have control over my DLL. By “…compiling your external DLL as part of the unreal build chain by porting it to a module” do you mean I should look at these examples and get my DLL’s functions running in an Unreal Module and eliminate the need for an external DLL?

Those examples look like a good explanation of plugins. To Unreal Build Tool, plugins are really just modules that cannot be linked to from game modules, so they add functionality without needing any communication from the game.

I think if you want to be calling your module’s functions from inside your game, it’ll need to be a module. You should be able to do this by just creating a new sub-folder underneath your game’s Source folder with an associated .Build.cs file.

If you have the VehicleGame sample project, its loading screen is implemented as a separate module. You can see it has a public VehicleGameLoadingScreen.h header which is included from the main VehicleGame module which is called from inside AVehicleHUD_Menu::ShowLoadingScreen().

Hopefully that gives you something to look at :slight_smile:

I’ll check out that sample, thanks!

Here’s the thing I don’t technically need to call the functions from game play. I just thought of an example, hopefully it better describes what I’m trying to do.

Let’s say I wanted to edit my game world using an xbox controller (bare with me). This doesn’t have to be during game play. So I want to write a plug-in / module / something which does the following :

 For the currently selected game object, move it in the direction the controller's analog stick is pointing. 

This has nothing to do with game play but rather design of the world and interacting with the unreal editor itself. Rather than using the mouse to position an object in my world, I want to use an xbox controller. So if I were to build my game into a standalone exe, none of this code would be built into it. This is purely another way of interacting with the editor.

Now this isn’t what I’m actually doing (the actual system is more complex) but it’s the same idea.

Hopefully that describes what I’m trying to do here a little better. If I didn’t absolutely confuse the heck outta you, how would you go about doing that?

EDIT: and say the Dll gives you the current direction the controller’s analog stick is being held in.

Sorry, looks like my reply went awry (poet?)

It seems to me like some combination of an editor plugin and/or game module would be a nice fit for your problem. Definitely worth looking into them in depth!