Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Returning a BSTR from ActiveX

Posted on 2000-03-01
2
1,054 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 96
Host to IP 7 80
zeroFront challenge 7 112
"Black Box" Testing of Control System Software 2 70
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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