Hi,
I have a simple dll with one exported function which calls a callback function in the client application passing a pointer to a UDT having two fields.
All works well. Tested with clients written in both Purebasic (the language in which the dll is created in) and Powerbasic.
However, in Powerbasic I am getting a crash on a very strange line and I cannot figure out quite why?
The UDT in question has a field which points to a character array (of unknown length). When I access this string via Peek$() then no problem and no crashes. However, I am trying to access the string through a pointer because of the unknown length etc. I am aware of the potential pitfalls of doing this, but I thought that I had taken care to avoid those.
The code compiles ok and the string passed from the dll is retrieved fine. However, the crash occurs on the simple assignment Let x = str1 which is puzzling!
If I use a proper bona-fide string pointer with two levels of indirection then all works fine. But I do not understand why the above code should crash on the simple string assignment? Is Powerbasic attempting to release the memory in str1 via SysFreeString()? This would certainly explain the crash, but it wouldn't make sense for PB to do this!
Much appreciated if someone can shed some light on this? I can make the dll available if it is required.
Thanks.
Stephen.
I have a simple dll with one exported function which calls a callback function in the client application passing a pointer to a UDT having two fields.
All works well. Tested with clients written in both Purebasic (the language in which the dll is created in) and Powerbasic.
However, in Powerbasic I am getting a crash on a very strange line and I cannot figure out quite why?
The UDT in question has a field which points to a character array (of unknown length). When I access this string via Peek$() then no problem and no crashes. However, I am trying to access the string through a pointer because of the unknown length etc. I am aware of the potential pitfalls of doing this, but I thought that I had taken care to avoid those.
Code:
#Compile Exe #Dim All Type test a As Long b As Long 'Points to a null-terminated character array of arbitrary length. End Type Declare Sub InvokeCallback Lib "dll5.DLL" Alias "InvokeCallback" (ByVal address As Long) 'The following is called by the dll. Sub myCallback(structure As test) Local str1 As String, ptrString As Long Ptr Local x As String 'Place the address of the character array into the str1 string. ptrString = VarPtr(str1) @ptrString = structure.b 'Let us take a look. ? str1 'Fine. Let x = str1 'CRASH! @ptrString = 0 'Avoid Powerbasic attempting to free the dll string. End Sub Function PBMain () As Long InvokeCallback(CodePtr(myCallback)) End Function
If I use a proper bona-fide string pointer with two levels of indirection then all works fine. But I do not understand why the above code should crash on the simple string assignment? Is Powerbasic attempting to release the memory in str1 via SysFreeString()? This would certainly explain the crash, but it wouldn't make sense for PB to do this!
Much appreciated if someone can shed some light on this? I can make the dll available if it is required.
Thanks.
Stephen.
Comment