Solved

Returning a BSTR from ActiveX

Posted on 2000-03-01
2
1,046 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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

7 Experts available now in Live!

Get 1:1 Help Now