How can I call a COM object from UE4 C++ Code?

I’m having trouble successfully calling a COM object from UE4 c++ code.

Here’s a minimal Visual Studio C++ console app that successfully creates and interacts with the COM object I want. How can I accomplish this in a UE4 C++ project?

If I try using #import it seems to create the .tlh and .tli files in a location that it can’t find on build. I tried copying those files to were it expected, which allows it to build and run but then I just get memory access violations when I actually try to create the COM object.

#include "stdafx.h"

#import "C:\Users\Thomas\Desktop\Temp\vpinmame_23\vpinmame.dll"

using namespace VPinMAMELib;

int _tmain(int argc, _TCHAR* argv[])
	IControllerPtr vpm(__uuidof(Controller));
	vpm->GameName = "FT_L5";
	vpm->Run(0, 99);
	while (getchar() == 0);

	return 0;

Some other examples can be found in VisualStudioSourceCodeAccessor.cpp, where we import and use EnvDTE.

To load a dll please check /Engine/Source/Runtime/Engine/Private/PhysicsEngine/PhysXLibs.cpp there you can see how to load a dll from a path.

when working with windows headers add

#include "AllowWindowsPlatformTypes.h"
	//include ms headers here
#include "HideWindowsPlatformTypes.h"

For com objects unreal has its com ptr wrapper: TComPtr.

#include "COMPointer.h"
TComPtr<ComObjType> ComObj;

I looked up the TypeLib ID in OLE/COM Object Viewer and tried to do an #import the way it looks like you are in VisualStudioSourceCodeAccessor.cpp but it gives me an error saying it can’t load the .tlh file. (see screenshot)

I’m pretty lost… (If it matters, this is only a simple UE4 C++ project, not a full UE4 source build project.)

All i need is a single instance of the Controller. Which In the simple console app I got by: IControllerPtr vpm(__uuidof(Controller));

If I’m close, could you perhaps write some psuedo code that would give me a VPinMAMELib::Controller object?

I’m still confused as to the proper way to do call COM from UE4. However, I just realized a big part of my problem is that I was trying to communicate wiht a 32-bit COM lib from UE4 running as a 64-bit application.