Solved

Converting data types (again)...

Posted on 1999-01-20
4
147 Views
Last Modified: 2013-11-25
I have this function in some .h file included in my ATL COM program:

function(char *);

and need to pass this variable in the ATL COM code into the "char *" part of the above function:

BSTR SomeData;

How do I do that?

So, I need to call this function in my ATL COM code like this:

BSTR SomeData;

// Conver BSTR to char (what is the code needed here)

function(charOfSomeData);

Thanks...
0
Comment
Question by:loyaliser
  • 2
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 20 total points
ID: 1184667
You can make use of _bstr_t class that comes with Visual C++ 5.0 or later, which you are likely using since you are using ATL.

#include <comdef.h>

BSTR SomeData;

_bstr_t bstrData(SomeData, true);

function(bstrData);


This is because _bstr_t class overloads the operator char*.

operator char*( ) const throw( _com_error );

You should also handle the exception that might be thrown using try-catch in the above code.
0
 
LVL 1

Author Comment

by:loyaliser
ID: 1184668
I will give it a try tomorrow morning... thanks...
0
 
LVL 1

Author Comment

by:loyaliser
ID: 1184669
I hope you don't mind, but what would be the code for doing it the other way around... char [] to BSTR?

Thanks...
0
 
LVL 23

Expert Comment

by:chensu
ID: 1184670
Use the ATL conversion macro A2BSTR.

USES_CONVERSION;
A2BSTR();

Or, simply use _bstr_t since it has a constructor

_bstr_t( const char* s2 ) throw( _com_error );

and use _bstr_t in place of BSTR.

You may also use CComBSTR class in ATL.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

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