Purebasic-Tipps and Code > Purebasic Tipps
PureBasic vs. PowerBasic (Bob's Opinion)
José Roca:
Regarding the p-bstr pseudotype, I only have found that it does an automatic conversion, i.e. it creates a BSTR that is passed to the called function, but nothing about what happens with these temporary bstrings. Maybe it will generate code to release them, but I don't know.
If it is passed by reference:
--- Quote ---When a method returns a string (typically a BSTR one), it asks you to provide a pointer into which it will fill the string. Here no automatic conversion is supported. What you need to do here is pass a pointer to a long variable, which will be filled with the BSTR pointer. Then you can use PeekS() to read the string. Then do not forget SysFreeString() of course.
Example:
If MyObject\SomeMethod(@bstr_sting) = #S_OK
Debug PeekS (bstr_string , -1, #PB_Unicode)
SysFreeString_(bstr_string)
EndIf
--- End quote ---
Regarding the BSTR returned by the PowerBasic function, your Pure Basic prototype
--- Code: ---Prototype.l ProtoRemoveAny( param1.p-bstr, param2.p-bstr )
--- End code ---
treats it as a long value, so it doesn't really know if it is a BSTR or not.
Probably you will have to do something like
--- Code: ---<long variable> = RemoveAny( s1$, s2$ )
PeekS <long variable>
SysFreeString <long variable>
--- End code ---
David Roberts:
--- Quote ---treats it as a long value, so it doesn't really know if it is a BSTR or not.
--- End quote ---
But I do - it is a Wstring.
--- Code: ---<long variable> = RemoveAny( s1$, s2$ )
PeekS <long variable>
SysFreeString <long variable>
--- End code ---
If it was a BSTR then the above would cause an access violation because PeekS, defaulting to reading unicode, would attempt to read a BSTR.
However, I now take your point about a memory leak.
If I changed back to 'Export as String', instead of 'Export as Wstring', then this works.
--- Code: ---ptr.l = RemoveAny( s1$, s2$ )
MessageRequester( "", PeekS( ptr.l, -1, #PB_Ascii ) )
SysFreeString_( ptr.l )
--- End code ---
The BSTR is now being read as Ascii and is then freed.
Alternatively, we could have
--- Code: ---ptr.l = RemoveAny( s1$, s2$ )
s3$ = PeekS( ptr.l, -1, #PB_Ascii )
SysFreeString_( ptr.l )
--- End code ---
The BSTR is read correctly and then converted to unicode.
Thanks, José.
Navigation
[0] Message Index
[*] Previous page
Go to full version