Solved

Converting data types (again)...

Posted on 1999-01-20
4
145 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

867 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

19 Experts available now in Live!

Get 1:1 Help Now