Discussion:
string memory free between C++ native client and dotnet assembly
(too old to reply)
pipi
2009-04-16 03:43:50 UTC
Permalink
I use native c++ client app to call a dotnet assembly through CCW (COM
callable wrapper)
In dotnet component, I used the following to raise event
====================================================================
[ComVisible(true)]
public delegate void Evt2DeveloperDelegate( string value, string
subject);
public interface ITestEvents
{
void Evt2Developer([MarshalAs(UnmanagedType.BStr)] string
value, [MarshalAs(UnmanagedType.BStr)] string subject);
}


And C++ will use the following to handle the incoming event
====================================================================
HRESULT __stdcall Evt2Developer(BSTR value,BSTR subject)
{
BSTR bstrString=AsciiToBSTR(val);
//print bstring
/* SysFreeString(value); this will failed */
}


BSTR AsciiToBSTR(const char* pszFName)
{
WCHAR wszURL[MAX_PATH];
::MultiByteToWideChar(CP_ACP, 0, pszFName, -1, wszURL, MAX_PATH);
return SysAllocString(wszURL);
}


====================================================================

How to free the string memory? C++ (call SysFreeString(value); will
failed ) or Dotnet GC
Any help appreciate
Giovanni Dicanio
2009-04-16 08:42:31 UTC
Permalink
Post by pipi
I use native c++ client app to call a dotnet assembly through CCW (COM
callable wrapper)
In dotnet component, I used the following to raise event
====================================================================
[ComVisible(true)]
public delegate void Evt2DeveloperDelegate( string value, string
subject);
public interface ITestEvents
{
void Evt2Developer([MarshalAs(UnmanagedType.BStr)] string
value, [MarshalAs(UnmanagedType.BStr)] string subject);
}
And C++ will use the following to handle the incoming event
====================================================================
HRESULT __stdcall Evt2Developer(BSTR value,BSTR subject)
{
BSTR bstrString=AsciiToBSTR(val);
//print bstring
/* SysFreeString(value); this will failed */
}
The BSTR instances ('value' and 'subject') are passed to the C++ handler as
input parameters, right?
So, you can access (read) them, but you don't have to free them.

Instead, SysFreeString() should be called on the 'bstrString' variable (not
on 'value' variable), that you allocated via AsciiToBSTR() call.

HTH,
Giovanni
pipi
2009-04-17 02:47:45 UTC
Permalink
On 4月16日, 下午4時42分, "Giovanni Dicanio"
Post by Giovanni Dicanio
Post by pipi
I use native c++ client app to call a dotnet assembly through CCW (COM
callable wrapper)
In dotnet component, I used the following to raise event
====================================================================
[ComVisible(true)]
public delegate void Evt2DeveloperDelegate( string value, string
subject);
public interface ITestEvents
{
void Evt2Developer([MarshalAs(UnmanagedType.BStr)] string
value, [MarshalAs(UnmanagedType.BStr)] string subject);
}
And C++ will use the following to handle the incoming event
====================================================================
HRESULT __stdcall Evt2Developer(BSTR value,BSTR subject)
{
BSTR bstrString=AsciiToBSTR(val);
//print bstring
/* SysFreeString(value); this will failed */
}
The BSTR instances ('value' and 'subject') are passed to the C++ handler as
input parameters, right?
So, you can access (read) them, but you don't have to free them.
Instead, SysFreeString() should be called on the 'bstrString' variable (not
on 'value' variable), that you allocated via AsciiToBSTR() call.
HTH,
Giovanni- 隱藏被引用文字 -
- 顯示被引用文字 -
Hi Giovanni,
It's really help me
Thanks a lot
Giovanni Dicanio
2009-04-17 09:42:26 UTC
Permalink
Post by pipi
It's really help me
Thanks a lot
You're welcome.

Giovanni

Loading...