Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Converting variables...

Posted on 1999-01-11
6
Medium Priority
?
336 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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 80 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

926 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