COMMUNITY

Can't call a C++ interface function from external code

I have some Haxe code that I’m compiling to a static C++ library, which I would like to then use within a third party program. I’ve had some success prior to this attempt, where I could build a library and call it, but I’m now getting a little bit deeper and running into an issue calling a function on an interface implementation. My c++ is terrible (I’m just doing a proof of concept), so I’m hoping I’m just making a stupid mistake.

I have an interface named Project, with an implementing class DefaultProject. I’m trying to call a function named initializeModel on that class. In Haxe, that function’s signature is:

public function initializeModel(?modelPath:String):Model {}

These classes are compiled into a library which is included in my C++ Visual Studio project.

So in my code, first I get the project - this seems to work OK - and then call the function:

::Dynamic project = mtk->readProjectFromPath(projectPath);
::Dynamic model = ::Project_obj::initializeModel(project, modelFilePath);

On the second line, I get a read access violation within the DefaultProject.cpp file, in the __Field function. Here’s the code that was generated from Haxe:

hx::Val DefaultProject_obj::__Field(const ::String &inName,hx::PropertyAccess inCallProp)
{
 	switch(inName.length) {
 	<snip>
 	case 15:
 		if (HX_FIELD_EQ(inName,"initializeModel") ) { return hx::Val( initializeModel_dyn() ); }
 		break;
        return super::__Field(inName,inCallProp);
}

The error occurs on the switch line:

Unhandled exception thrown: read access violation.
**inName** was 0x11. 

In case it helps, the initializeModel call on the interface looks like this:

static inline ::Dynamic initializeModel( ::Dynamic _hx_,::String modelPath) {
        return (_hx_.mPtr->*( hx::interface_cast< ::com::corp::model::Project_obj *>(_hx_.mPtr->_hx_getInterface(0x99a3e6a7)))->_hx_initializeModel)(modelPath);
}

So what it looks like to me is that I have the proper instance of DefaultProject, but there’s a problem getting the function pointer for the implementation function. Any idea how I can debug this issue? Thanks!

Turns out that adding a define to my visual studio project fixed the whole thing. That define was HXCPP_ALIGN_ALLOC

The full list of defines that I ended up with is:

HXCPP_STACK_TRACE
HX_WINDOWS
HXCPP_ALIGN_ALLOC
HXCPP_API_LEVEL=400
HX_WINRT
NDEBUG
_CONSOLE