Solved

Converting variables...

Posted on 1999-01-11
6
305 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

26 Experts available now in Live!

Get 1:1 Help Now