How do I convert to BSTR?
How do I convert to BSTR?
I just purchased PureBasic this past weekend so I’m still new to the language. I’m trying to use PureBasic to interface with other programs. I was using the very cool Interface Generator tool, but I’m having some problems.
Here’s an example of the C++ function I’m trying to access:
[VOffset($24)] HRESULT LogMessage([in] BSTR in_Message,
[in, optional, defaultvalue=8] siSeverity in_Severity)
The interface generator creates code that looks like this:
Interface Application Extends IDispatch
LogMessage(a,b)
EndInterface
I did some searches on the forums and I found some info about converting strings to BSTR, but nothing that works so far. If I try and pass it a string I get an error: “Bad parameter type, number expected instead of string” so I think I need to pass it a pointer, but I’m not sure.
Does anyone know how I can convert a string into a BSTR (a Unicode string?) and get the pointer?
Here’s an example of the C++ function I’m trying to access:
[VOffset($24)] HRESULT LogMessage([in] BSTR in_Message,
[in, optional, defaultvalue=8] siSeverity in_Severity)
The interface generator creates code that looks like this:
Interface Application Extends IDispatch
LogMessage(a,b)
EndInterface
I did some searches on the forums and I found some info about converting strings to BSTR, but nothing that works so far. If I try and pass it a string I get an error: “Bad parameter type, number expected instead of string” so I think I need to pass it a pointer, but I’m not sure.
Does anyone know how I can convert a string into a BSTR (a Unicode string?) and get the pointer?
did you try the prototype with 'p-bstr' type of purebasic 4 ?
something like that:
something like that:
Code: Select all
Prototype.l LogMessage(InMessage.p-bstr,DefaultValue=8)No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
-
Bonne_den_kule
- Addict

- Posts: 841
- Joined: Mon Jun 07, 2004 7:10 pm
Thank you! Prototypes look like the way to go 
I was reading about prototypes in the readme file and it's still not clear to me how to use it with a DLL. Could I do something like this? not that this works, I really don't understand how this should come together just yet.
also, can I use 'Interface' like I was before to access COM objects? If so how do I prototype the COM object? I guess I would still need to lookup the required types for the parameters.
sample interface:
I was reading about prototypes in the readme file and it's still not clear to me how to use it with a DLL. Could I do something like this? not that this works, I really don't understand how this should come together just yet.
Code: Select all
OpenLibrary(0, "core.dll")
Prototype.l LogMessage(InMessage.p-bstr,DefaultValue=8)
LogMessage ( "test...",8 )
also, can I use 'Interface' like I was before to access COM objects? If so how do I prototype the COM object? I guess I would still need to lookup the required types for the parameters.
sample interface:
Code: Select all
Interface OGLTexture Extends IDispatch
get_Width(a)
get_Height(a)
get_FullName(a)
EndInterface
About the prototype, it just define a new type (like a structure, interface etc.). So you have to affect this type to a variable, and use GetFunction() to get the address of the real function:kyleh wrote:Code: Select all
OpenLibrary(0, "core.dll") Prototype.l LogMessage(InMessage.p-bstr,DefaultValue=8) LogMessage ( "test...",8 )
Code: Select all
Prototype.l LogMessagePrototype(InMessage.p-bstr,DefaultValue=8)
OpenLibrary(0, "core.dll")
LogMessage.LogMessagePrototype = GetFunction(0, "LogMessage")
if LogMessage ; is the function really found ?
LogMessage ( "test...")
endif
When I use this I'm getting an Assembler Error.
PureBasic.asm [68]:
CALL _SYS_ToUnicode@4
error: undefined symbol
I'm sure I'm still missing something, but I can't find any information on this error.
PureBasic.asm [68]:
CALL _SYS_ToUnicode@4
error: undefined symbol
I'm sure I'm still missing something, but I can't find any information on this error.
[/code]Code: Select all
Prototype.l LogMessagePrototype(InMessage.p-bstr,DefaultValue=8) OpenLibrary(0, "core.dll") LogMessage.LogMessagePrototype = GetFunction(0, "LogMessage") if LogMessage ; is the function really found ? LogMessage ( "test...") endif

