Solved

Converting variables...

Posted on 1999-01-11
6
300 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now