Solved

Converting data types (again)...

Posted on 1999-01-20
4
150 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
[X]
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
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

705 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