Interfacing with C ?

BlitzMax Forums/BlitzMax Beginners Area/Interfacing with C ?

Pharmhaus(Posted 2009) [#1]
Hi,
I try to integrate some external C code into my app but i've never done something before that.
I have some problems with calling extern c functions, especially with converting blitzmax strings to c char etc.

one of my current approaches looks similiar to this:
SuperStrict

Import "test2.c"

Extern "C"
	Function Dummy:Int(Test:Byte Ptr)' int Dummy(char Test) {....}
End Extern

 
Function Test:Int(Alpha:String)'0 for sucess, -1 for failure
	Local Str:Byte Ptr
	Local _Ret:Int
	
	Str = Alpha.ToCString()
	_Ret:Int = Dummy(Str)
	
	MemFree(Str)
	Return _Ret
End Function


But it's not working right... , Any ideas?

Thanks!


Czar Flavius(Posted 2009) [#2]
Shouldn't Dummy take char[] and not char?


Pharmhaus(Posted 2009) [#3]
Isn't there a way for just converting to char?
It's just a single letter as input...


Gabriel(Posted 2009) [#4]
Typically you pass arrays around as pointers in C. So use char* and it won't matter how long it is. I think this is how Blitz expects ToCString and FromCString to be handled in any case.


Czar Flavius(Posted 2009) [#5]
If you want to do that, try Byte rather than Byte Ptr. But it's probably much easier to use a char pointer that just points to a single character. Well actually it would need to point to two characters, as the second character should be the termination character, so make sure you bear this in mind. This bit me in the bum once before when deaing with a "string" of "1" character. This is why I hate C. character character character character character


markcw(Posted 2009) [#6]
Have you read "C for Blitzers"?
http://www.blitzbasic.com/Community/posts.php?topic=82509


Brucey(Posted 2009) [#7]
I wouldn't pass a Byte as a parameter to C. BlitzMax doesn't always appreciate it (data corruption, etc). Better to pass an Int and cast to char (if you really need to).

And, given also that a BlitzMax character is 16bit...

Just a thought.


gman(Posted 2009) [#8]
use char* and $z. $z handles the ptr cleanup for you.
' c_test.bmx

Import "c_test.c"

c_test("this is a test")

Extern "C"
	Function c_test(test_string$z)
EndExtern

// c_test.c

void c_test(char* test_string) {
	printf(test_string);
}



Yahfree(Posted 2009) [#9]
http://blitzbasic.com/Community/posts.php?topic=77916#873971

May help, it's my battle with C/C++.

towards the end I start to integrate them with Bmax.