Solved

Converting variables...

Posted on 1999-01-11
6
318 Views
Last Modified: 2008-02-01
How do I copy a BSTR variable to a char variable?

How do I copy a char variable (of array type) to a BSTR variable?
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
6 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1181868
What's a BSTR?
0
 
LVL 1

Author Comment

by:loyaliser
ID: 1181869
BSTR is a COM variable type. I am building a COM in Visual C++ 6.0 and one of my functions accepts a parameter of type BSTR. I need to convert the BSTR variable to a char so that I can pass it to a function that accepts a char parameter.

That's it... make any sense? Thanks...
0
 
LVL 1

Author Comment

by:loyaliser
ID: 1181870
As far as I know BSTR is similar to the String data type, however, I don't know if it shares the same API methods.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:fkurucz
ID: 1181871
the class _bstr_t defined in <comdef.h> automates the conversion from and to BSTR or char*.

intern the following functions are used:

 BSTR _com_util::ConvertStringToBSTR(const char* pSrc)

 char* _com_util::ConvertBSTRToString(BSTR pSrc)

bye,
  feri
0
 
LVL 2

Accepted Solution

by:
rayb earned 20 total points
ID: 1181872

There are several ways to perform this action.  One I frequently use is snprintf, which can do the job like this:
{
  const int
    max= 255;
  char
    buffer[max];

  _snprintf( buffer, max, "%S", your_bstr_here );
}

Notice that the "%S" is a capital S.

Although this method works, the preferred method is to use the WideCharToMultiByte function.

Another preferred method works in the context of ATL and uses the USES_CONVERSION and W2A macros.

To do the inverse of these operations, use the MultiByteToWideChar, SysAllocString, USES_CONVERSION and A2W functions and macros.

0
 
LVL 1

Author Comment

by:loyaliser
ID: 1181873
Yeah... I need the method that works in the ATL, because the code is within an ATL COM object.

Thanks...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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 clear a vector as well as how to detect empty vectors in C++.

730 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