C++ interface question

BlitzMax Forums/BlitzMax Programming/C++ interface question

JoshK(Posted 2008) [#1]
Here is a C++ type definition:
	typedef struct NewtonCollisionInfoRecordTag
	{
		typedef struct NewtonHeightFieldCollisionTag
		{
			int m_width;
			int m_height;
			int m_gridsDiagonals;
			dFloat m_horizonalScale;
			dFloat m_verticalScale;
			short *m_elevation;
			char *m_atributes;
		} NewtonHeightFieldCollision;
	} NewtonCollisionInfoRecord;


Here is my BlitzMax version:
Type TNewtonCollisionInfoRecordTag
EndType

Type TNewtonHeightFieldCollisionTag Extends TNewtonCollisionInfoRecordTag
	Field m_width
	Field m_height
	Field m_gridsDiagonals
	Field m_horizonalScale#
	Field m_verticalScale#
	Field m_elevation:Short Ptr
	Field m_atributes:Byte Ptr
EndType


Can I pass a BlitzMax type like this to a dll using a Byte Ptr parameter? Will it work? So far my results do not seem correct.


Dreamora(Posted 2008) [#2]
No you can not pass BM objects out. BM will just crash.
If you need objects within C++ do it vice versa by having the class in C++ and create extern types. Those are never handled by the GC so you don't risk a GC crash which is guaranteed if you try to byte ptr your objects (you would need to clone the object data through memcpy and send that byte ptr to the dll ... extra work that can be avoided by using extern types and C++ construction / deconstruction)


JoshK(Posted 2008) [#3]
I see people use structures all the time with Windows API commands.

The external dll is only going to fill in values to the structure.


Dreamora(Posted 2008) [#4]
Then it should be safe (if the function is not calling anything else within the DLL, just do a straight job and return) to use the byte ptr.
Although if it just fills in data, why not let the DLL generate data and return a memory block that you can use to generate the object and memfree it then?

if it does not work, then one reason might be that BM objects are larger than what you define. Don't forget that there are 8bytes before the [0] pointer part (class id, ref counter) so you might need to offset. (don't know, never used BM objects outside after I got GC killed several times)


JoshK(Posted 2008) [#5]
Because it's not my dll.

Anyways, it works. I had a couple of fields wrong, but it works fine now.


Czar Flavius(Posted 2008) [#6]
	typedef struct NewtonCollisionInfoRecordTag
	{
		typedef struct NewtonHeightFieldCollisionTag
		{
			int m_width;
			int m_height;
			int m_gridsDiagonals;
			dFloat m_horizonalScale;
			dFloat m_verticalScale;
			short *m_elevation;
			char *m_atributes;
		} NewtonHeightFieldCollision;
	} NewtonCollisionInfoRecord;


I hate C++. What is all that clutter?

Type TNewtonCollisionInfoRecordTag
EndType

Type TNewtonHeightFieldCollisionTag Extends TNewtonCollisionInfoRecordTag
	Field m_width
	Field m_height
	Field m_gridsDiagonals
	Field m_horizonalScale#
	Field m_verticalScale#
	Field m_elevation:Short Ptr
	Field m_atributes:Byte Ptr
EndType

I love BlitzMax.