
Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
> The CLR will free the BSTR during the marshaling process if the
> function has been declared correctly.
Hi Mattias
Here's the declaration and how it is used in the C# code.
//*******DECLARATION and method IMPLEMENTATION**********//
HRESULT GetValue([in] BSTR bstrName, [out,retval] BSTR* pbstrValue);
STDMETHODIMP MyNativeClass::GetValue ( BSTR bstrName, BSTR* pbstrValue)
{
std::wstring strValue = GetValue( std::wstring( CW2W(bstrName) ),
true );
*pbstrValue = ::SysAllocString( strValue.c_str() );
return S_OK; }
//*******USAGE in C# code**********//
if (objNative.GetValue( "_Source" ) == string.Empty)
OR
m_SomeVariable.AssignVal( objNative.GetValue( "_Source" ) )
Now, will the String onject be collected by GC.
Regards,
Pawan Arya
TDC - 30 Aug 2006 13:29 GMT
Well, you didn't actually show your C# p/invoke declaration (assuming
you don't have an interop assembly), but it looks *highly* likely from
your C# usage that the BSTR will get marshalled correctly (copied into
managed memory and then freed).
Tom
> > The CLR will free the BSTR during the marshaling process if the
> > function has been declared correctly.
[quoted text clipped - 24 lines]
> Regards,
> Pawan Arya