[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Returning a BSTR from ActiveX

Posted on 2000-03-01
2
Medium Priority
?
1,113 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 200 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: 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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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…
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

830 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