• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

COM: Returning a string from a method

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
felonius
Asked:
felonius
1 Solution
 
dhodgeCommented:
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
 
migelCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now