Solved

Converting variables...

Posted on 1999-01-11
6
308 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 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