Solved

COM: Returning a string from a method

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now