Solved

COM: Returning a string from a method

Posted on 2001-07-02
2
339 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 116
how to monitor remote shell execution on linux 9 113
isEverywhere  challenge 19 90
firstChar challenge 13 144
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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