Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: why C# program in closed when I used OCX method with string return type?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Sep 8, 2016 5:53 AM Last Post By: 8908183 8908183 Threads: [ Previous | Next ]
Haniyeh Ghassami

Posts: 2
Registered: 9/30/16
why C# program in closed when I used OCX method with string return type?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 2, 2016 11:55 PM
Hi. I am new here and this is my first topic. I make 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?
Haniyeh Ghassami

Posts: 2
Registered: 9/30/16
Re: why C# program in closed when I used OCX method with string return type?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 3, 2016 2:30 AM   in response to: Haniyeh Ghassami in response to: Haniyeh Ghassami
Haniyeh Ghassami wrote:

My problem is solved. When create BSTR and pass it between COM objects, must take care the memory they use in order to avoid memory leaks.

// shows using the Win32 function
// to allocate memory for the string:
BSTR bstrStatus = ::SysAllocString(L"Some text");
if (bstrStatus != NULL)
{
pBrowser->put_StatusText(bstrStatus);
// Free the string:
::SysFreeString(bstrStatus);
}

Reference: https://msdn.microsoft.com/en-us/library/xda6xzx7.aspx

Remy Lebeau (Te...


Posts: 8,732
Registered: 12/23/01
Re: why C# program in closed when I used OCX method with string returntype?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 3, 2016 2:54 PM   in response to: Haniyeh Ghassami in response to: Haniyeh Ghassami
Haniyeh wrote:

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.

This was asked and answered on StackOverflow:

why C# program is closed when I used my OCX method with string return type?
http://stackoverflow.com/questions/39304113/

Read() is not allocating memory for the output BSTR correctly. Use the WideString
class to help you:

return WideString(ch, 3).Detach();


otherwise you have to call SysAllocStringLen() directly:

return SysAllocStringLen(ch, 3);


--
Remy Lebeau (TeamB)
8908183 8908183

Posts: 1
Registered: 10/8/16
Re: why C# program in closed when I used OCX method with string return type?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 8, 2016 5:52 AM   in response to: Haniyeh Ghassami in response to: Haniyeh Ghassami
why C# program is closed when I used my OCX method with string return type?

see this link:

http://stackoverflow.com/questions/39304113/

https://clicksite.ir/layout.php?t=%D8%A2%D9%85%D9%88%D8%B2%D8%B4_%D8%B3%DB%8C_%D8%B4%D8%A7%D8%B1%D9%BE&id=59&act=cat

Edited by: 8908183 8908183 on Sep 8, 2016 5:53 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02