I made a c++ builder ActiveX Library. When I made my fist OCX that connect to socket and check connection and also read from socket and write on socket. All of the function is working but the function (Read from socket and send the string to ActiveX container) return type is BSTR (string) not worked. after running the project when this code run the program suddenly closed. How can I send data that I read from socket to ActiveX container with this function?
//============ c++ builder xe8================
//I change the code to just return simple output"123"
//but it can not retun and c# program closed
BSTR STDMETHODCALLTYPE TSock4Impl::Read()
{
WCHAR ch[10];
ch[0]='1';
ch[1]='2';
ch[2]='3';
return ch;
}
//=============c# code ================
private void Form1_Load(object sender, EventArgs e)
{
//label1.Text = axVinaSock41.Read();
int a = axSock41.ConStatus();
label1.Text = Convert.ToString(a);
label1.Text = axVinaSock41.Read();// in this line the program was closed.
}
My problem is solved. When creating a BSTR and passing it between COM objects, must take care of the memory it uses.
BSTR STDMETHODCALLTYPE TSock4Impl::Read()
{
WCHAR ch[10];
ch[0]=L'1';
ch[1]=L'2';
ch[2]=L'3';
return ::SysAllocStringLen(ch, 3);
}
Reference: Allocating and Releasing Memory for a BSTR