Passing a string to a BlitzMax dll?

BlitzMax Forums/BlitzMax Programming/Passing a string to a BlitzMax dll?

JoshK(Posted 2006) [#1]
BlitzMax dlls crash when a string is passed to them (using BlitzPlus). What is the correct way to handle this?

I also am having some strange errors when my dll uses DebugLog or RuntimeError, so I got rid of those commands.

Other than that, the dll compilation is working great!


Filax(Posted 2006) [#2]
It's possible to make DLL with bmax ???? :) If you have a link
for me !


ozak(Posted 2006) [#3]
Actually you could just have the functions inside an exe and then rename it as dll no? They have specific entry points.


Azathoth(Posted 2006) [#4]
How would that work? Most exes wont have their function names visible.


Dreamora(Posted 2006) [#5]
Currently DLL support is in test stage.

Strings as nearly all data from outside comes in through Byte Ptr (I assume you have that).
This enforces that you, at least on string, use freemem before returning whatever you wanted to return.
Main point is, if you want to work with Blitz, is that you return 1 at the end of the operation.

The following function is "a minimum function" that should work:

Function PrintText:Int(_text:Byte Ptr)
	Print "Test: " + String.fromcstring(_text)
	MemFree _text
	Return 0
End Function



Azathoth(Posted 2006) [#6]
The freeing of the string shouldn't be done by DLL if it was allocated by the main program, it has no idea if the program is still using the ptr.


Dreamora(Posted 2006) [#7]
Normally, right.
In this case, where it interfaces with old Blitz which actually don't have anything like pointer, it is a different thing, as you won't be able to free it again from within Blitz (I actually don't know if B3D / B+ frees the string after the DLL command returns automatically or not)


JoshK(Posted 2006) [#8]
It works...not sure about freeing the memory, though.


Azathoth(Posted 2006) [#9]
Judging by the mvsc demo.dll that comes with Blitzplus, it doesn't free the string from Blitzplus only its own copy that was allocated in the previous call.


JoshK(Posted 2006) [#10]
Main point is, if you want to work with Blitz, is that you return 1 at the end of the operation.

So if you have a function that doesn't return a value, you have to make it return an integer (0)? I noticed a crash when a Blitz3D program ended, with the debugger on.


JoshK(Posted 2006) [#11]
Main point is, if you want to work with Blitz, is that you return 1 at the end of the operation.

Please explain why an integer return value is needed.


JoshK(Posted 2007) [#12]
I have determined that the MemFree in the above example will crash BlitzMax. You should not free the byte ptr that gets passed to the dll!

Function PrintText(htext:Byte Ptr)
	text$=String.fromcstring(htext)
	Print text
EndFunction



Paul "Taiphoz"(Posted 2007) [#13]
you can make a dll with blitz max??? link ? source ?


ziggy(Posted 2007) [#14]
In the calling program, the String should be properly counted by the garbage collector.
But in the DLL program, using Byte Ptr should avoid the usage of the GC reference counter, so as long as the function is not running on a separated thread, the string object references should be the same in the calling program, before and after the function call, so you shouldn't have to free memory. In fact, when you do text$ = string.fromcstring(htext) you're creating a new and separated BlitzMax string object. Strings are the only objects that doesn't increase reference counting when assigned. they just duplicate objects, this way, they work as if they where 'regular' variables.

And the real problem here is freeing memory when the calling program still has at last one string reference count of it (the one placed in the calling sentence).
So my advice whould be to not free memory as long as you are not creating multithread dlls. I see no way to do this without confusing the GC


Azathoth(Posted 2007) [#15]
The program calling ToCString or ToWString should be the one freeing the memory, if your Dll does it that will make it incompatible with other programming languages.