[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

COM: Returning a string from a method

Posted on 2001-07-02
2
Medium Priority
?
344 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 300 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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