Hi xLeaves,
thanks a lot for sharing the solution for the problem!
I would say exchanging text between languages is an challenge in general. For example - std::string in C++ is even more interesting, as the place of the data depends on the string length (if interested, see second reply here).
ThinBASIC has an ideal symbiosis with PowerBASIC for STRING data type, as it is the same internally.
You correctly picked ASCIIZ to change the data between FreeBASIC and thinBASIC. The same would apply for example for pure C and thinBasic.
If I may note one thing - your approach is perfectly safe, yet may pose a performance issue.
You basically perform extra allocation:
- your DLL allocates the memory for result - text allocated at memory location 1
- thinBasic duplicates the memory of result - text allocated at memory location 2
- you return the duplicated value and possibly assign it to some variable - text allocated at memory location 3
In many cases it is okay, in other cases you could run into speed issues:
- if the amount of data is large
- especially if you would perform such operation in a loop
If you can use ASCIIZ directly, without conversion, and if you see ahead the maximum possible size for the string the returned pointer points to, you can shorten your code to:
Dim azMyResult As asciiz * 512 at Process_ListChild_Ori(pHandle) ' Note I expect the string from freebasic to not be larger than 512 bytes, feel free to change that number
' Now azMyResult can be used to further print the value, but assigning the data from FreeBasci to azMyResult did not create a new allocation, azMyResult maps over the original memory
Alternatively, if you are okay with the copy, your code can also use the PeekZ$:
Function Process_ListChild(ByVal pHandle As Long) As String
Return PeekZ$(Process_ListChild_Ori(pHandle)) ' Note PEEKZ$, so you don't have to specify ASCIIZ
End Function
P
Bookmarks