Interfacing with C ?
BlitzMax Forums/BlitzMax Beginners Area/Interfacing with C ?
| ||
| 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! |
| ||
| Shouldn't Dummy take char[] and not char? |
| ||
| Isn't there a way for just converting to char? It's just a single letter as input... |
| ||
| 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. |
| ||
| 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 |
| ||
| Have you read "C for Blitzers"? http://www.blitzbasic.com/Community/posts.php?topic=82509 |
| ||
| 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. |
| ||
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);
}
|
| ||
| 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. |