SysReAllocString - newbie confused!
Hi everyone,
I've written a class and put a COM wrapper around it. The class (and COM) takes a BSTR as an input parameter which is modified and then sent back out to whatever called the COM. I've declared the BSTR as [in, out] in the IDL and I'm using:
SysReAllocString(&strParameters, (OLECHAR*)chstrParams);
to alter the BSTR strParameters that is the input to the COM. I then have to call delete[] on the char* chstrParams. At this point the BSTR still appears to be valid, but when the COM exits I get an unhandled exception error. Am I dealing with returning the altered BSTR correctly and the re-allocation of the string?
Thanks for your help.
Re: SysReAllocString - newbie confused!
BSTR cannot be the [in,out] parameter. Only BSTR*.
Code:
HRESULT cX::funcX(... /*[in,out]*/ BSTR * strParameters,...)
{...
SysReAllocString( strParameters, (OLECHAR*)chstrParams);
...
}
Re: SysReAllocString - newbie confused!
It's always good to think in VB first and then define the IDL interface to your COM DLL.
For instance, if you have a "(ByRef P As String)" (or "P As String") you'll translate it as ([in,out]BSTR *P); and if you have a "(ByVal Q As String)" you'll translate it as ([in]BSTR Q);
It's almost mechanical, but you need to get used to that.
You can even try writing an empty VB DLL with the desired interface, and use OLEVIEW to check exactly how you need to write the IDL interface in your COM DLL. OLEVIEW is a good learning tool.
Re: SysReAllocString - newbie confused!
Thanks guys I've taken what you've said on board and managed to get everything going now. Cheers!