Solved

COM: Returning a string from a method

Posted on 2001-07-02
2
340 Views
Last Modified: 2013-11-20
Hi,

I am writing an ActiveX control for use with Automation.
But now I am stuck (again).

All I simply want to do is to create a string (A BSTR I assume) and return it from my ActiveX method to the caller. How do I do that (the correct way)?

If I allocate memory for the string using SysAllocString() and then return that pointer is it then the responsibility of the caller to deallocate it, or must the callee do that somehow? I would guess that it was the responsibility of the caller since this is the case with AddRef/Release.

PS: I already know all about converting between BSTR/Unicode/ANSI - it is the allocation/deallocation that I am concerned about.

Jacob Marner, B.Sc.
0
Comment
Question by:felonius
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
dhodge earned 100 total points
ID: 6245634
Releasing the memory is the task of the calling code.  Alternatively just use one of the BSTR wrapper classes in your calling code then it will automatically release the memory.
0
 
LVL 12

Expert Comment

by:migel
ID: 6245668
Hi!
you can write like this:

HRESULT CMyControl::GetSTring(BSTR* pbstr)
{
_bstr_t str("This is a test!");
*pbstr = str.copy();
 return S_OKAY;
}
Caller must free this string  - COM convention.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question