Exporting strings

BlitzMax Forums/BlitzMax Programming/Exporting strings

verfum(Posted 2007) [#1]
What's the best method of passing a string to an external library? I have a DLL written in C and I want to pass string data to it. I can get string data into blitz from the DLL using the String.FromCString function. How do I do the other way around?
I can pre-allocate the memory for the string in my DLL, but how do I pass the pointer to this allocated memory to blitz and how do I get blitz to copy the string into the memory that this pointer is pointing to?

Cheers anyone for any help :P


grable(Posted 2007) [#2]
Use SomeString.ToCString() and remember to free it when done.

Preallocating in the DLL isnt needed, as bmx already copies the string to the new pointer.


verfum(Posted 2007) [#3]
Thanks grable for your quick reply :P

So is the DLL is responsible for deleting the memory that blitz allocated for the string?
So my function like this will be ok?

Function myReadString:Byte Ptr(a:Int, b:Int)
Local cString:Byte Ptr
Local blitzString:String = ReadString(a,b)
cString = blitzString.ToCString();
Return cString
End Function


verfum(Posted 2007) [#4]
Just checked, and yep, that worked.
Thanks very much grable.

Cheers!