Solved

Returning a BSTR from ActiveX

Posted on 2000-03-01
2
1,051 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 35
windows 10 how make picture as desktop background 2 62
has22 challenge 11 95
canBalance challenge 34 85
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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