Solved

Returning a BSTR from ActiveX

Posted on 2000-03-01
2
1,063 Views
Last Modified: 2013-11-20
I'm embarassed to be asking this Q but here goes:

I have an ActiveX control which defines a method (TheString) to return a string (BSTR). It sounds simple, but I can never get it to return the proper string or avoid an exception error. Here is what I have: (I've also tried it with BSTR ...SysAlloc... with the same failure).

The control sits on a VB form. The string returned always looks like "???"

I am running this under Win 98.

BSTR CESTEditCtrl::TheString()
{
char szMyString[256];
lstrcpy (szMyString, ">BKM>DR PC"); USES_CONVERSION;
try
{
_bstr_t MyBstr(A2COLE(szMyString));
return MyBstr;
}
catch (_com_error &e)
{
//error
}
}

Any thoughts?
Thanks!
0
Comment
Question by:lisab
2 Comments
 
LVL 8

Expert Comment

by:gelbert
ID: 2574581
There is a function
BSTR _com_util::ConvertStringToBSTR()
You need to include <comutil.h> and link with comsupp.lib
This function converts char* to BSTR
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 50 total points
ID: 2574743
The problem is that you're allocating the _bstr_t locally. You get the BSTR, then the destructor of _bstr_t frees that BSTR. You end up returning an invalid BSTR.

Using _com_util or _bstr_t here is wrong, if you ask me. MFC is already in memory; why not ask it to do the work instead of bringing in extra, redundant classes?

To have exactly the functionality you coded above, you would be better off writing something like this:


BSTR CESTEditCtrl::TheString()
{
   char szMyString[256];
   lstrcpy (szMyString, ">BKM>DR PC");
   USES_CONVERSION;
   try
   {
      CString strTemp(szMyString);
      BSTR MyBstr = strTemp.AllocSysString();
      return MyBstr;
   }
   catch (CMemoryException* pEx)
   {
      pEX->Delete();
      return NULL;
   }
}


If you're really hard-coding that string, then your best bet is this:


BSTR CESTEditCtrl::TheString()
{
   BSTR bstr = ::SysAllocString(L">BKM>DR PC");
   return bstr;
}


..B ekiM


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
Tab names are off by one 5 54
twoTwo  challenge 35 113
tripleUp challenge 7 93
wordmultiple challenge 12 156
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.

733 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