Library Integration in C

BlitzMax Forums/BlitzMax Programming/Library Integration in C

ingenius(Posted 2011) [#1]
Hi guys,

I have to integrate a library in C and I have the binary of the library and one header.
In all the documentation and examples I've read, some things I could fix and not others.

Things I've been able to solve.

* C
char QNX_GetVersionRequest(int *hCommPort, unsigned char *version, unsigned char *length);


* Blitzmax
Function QNX_GetVersionRequest:Byte(hCommPort:Int Ptr, version:Byte Ptr, length:Byte Ptr);


Things that I could not solve

* C
typedef struct _QNXCurrencyAssignStruct {
	unsigned char EscrowCode[30];
	unsigned char CountryType[30];
	unsigned long value[30];
} QNXCurrencyAssignStruct;

char QNX_GetCurrencyAssignRequest(int *hCommPort, unsigned char *NumberofElement, QNXCurrencyAssignStruct *QNXCurrencyAssig) ;


* BllitzMax
???


The last few days I've been asking people in the IRC channel, thanks to all who have patience to me.

Now, I'm not a brilliant C programmer and I need some help.
Thanks
I


col(Posted 2011) [#2]
Hiya,

You want to use the contents of the 'QNXCurrencyAssignStruct' inside your BMax code?

If so then initially you think you can do this :-
Type QNXCurrencyAssignStruct
	Field EscrowCode:Byte[30]
	Field CountryType:Byte[30]
	Field value:Long[30]
EndType


But unfortunately that won't work! If you 'Print SizeOf(QNXCurrencyAssignStruct), then you'll get 12 as the result. Because BMax will use a pointer to each array, with a pointer being 4 bytes, instead of showing as each array being a size of 30.

Ultimately, you want the final size of the 'QNXCurrencyAssignStruct' to equal 30 (char) + 30(char) + ( 30 * 4(long) ). I'm sure long is 4 bytes in C, and 8 bytes in BMax.
You'll have to flesh out the Type Fields and NOT use arrays within the Type :-
Type QNXCurrencyAssignStruct
	'Because of 'unsigned char EscrowCode[30]' this equates to 30 bytes
	'Make sure EscrowCode0 to 7 adds up to 30 bytes :-
	'7 * 4 = 28 ( Int = 4 bytes )
	'	+
	'1 * 2 = 2 ( Short = 2 bytes )
	'	-----
	'	 30
	'
	Field EscrowCode0,EscrowCode1,EscrowCode2,EscrowCode3,EscrowCode4
	Field EscrowCode5,EscrowCode6,EscrowCode7:Short
	
	'[CountryType0.....] Flesh these out as well
	'[value0.....] and Flesh this out too
	'long = 4 bytes in c, and 8 bytes in BMax.
EndType

Extern
	Function QNX_GetCurrencyAssignRequest:Byte(hCommPort:Int Ptr, NumberofElement:Byte Ptr, QNXCurrencyAssig:Byte Ptr)
EndExtern


The other thing that matters is how you want to access the data in your BMax code.
If you want to access EscrowCode as a string ( used as a string of char in the C code ) then you can easily add a Method inside the type
Method GetEscrowCode$()
	Return String.FromCString(Varptr EscrowCode0)
EndMethod



Then to use in your code you would use :-
Local QNXCAS:QNXCurrencyAssignStruct = New QNXCurrencyAssignStruct
Local Result:Byte = QNX_GetCurrencyAssignRequest(Port,NumElement,QNXCAS)

Print QNXCAS.GetEscrowCode()


Of course if you want to access the individual the elements of the array then there are multiple ways of achieving this too.

Hope this helps.

If you need any more help then feel free to ask.
If that's not what you wanted then I'm sorry :) I've babbled on about something totally unrelated :P

Last edited 2011