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

Converting data types (again)...

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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
Best way to accomplish this task? 3 88
Explain Unit of Work pattern 2 69
Issues with C++ Class 19 101
VIDEO DATASOURCE Control Resize issue 2 30
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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