Passing a string to a BlitzMax dll?
BlitzMax Forums/BlitzMax Programming/Passing a string to a BlitzMax dll?
| ||
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! |
| ||
It's possible to make DLL with bmax ???? :) If you have a link for me ! |
| ||
Actually you could just have the functions inside an exe and then rename it as dll no? They have specific entry points. |
| ||
How would that work? Most exes wont have their function names visible. |
| ||
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 |
| ||
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. |
| ||
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) |
| ||
It works...not sure about freeing the memory, though. |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
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 |
| ||
you can make a dll with blitz max??? link ? source ? |
| ||
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 |
| ||
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. |