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

x
?
Solved

covert char * to BSTR *

Posted on 2001-07-15
5
Medium Priority
?
558 Views
Last Modified: 2008-02-26
I have a Unicode string which i defined like this:
BSTR str1=sysAllocStringLen(L"Hello",5);

But now I need to convert it from (char *)
(Simple casting isn't good enough)
0
Comment
Question by:gshriki
5 Comments
 
LVL 3

Expert Comment

by:jlsjls
ID: 6284816
You could use the API-function : WideCharToMultiByte : this function maps a wide character string (BSTR) to a multibyte character string (char*).
0
 

Author Comment

by:gshriki
ID: 6284823
I need the exact opposite:
I need to convert char* to BSTR
A code example will be appreciated.
0
 

Accepted Solution

by:
ducpham earned 800 total points
ID: 6284831
you can use _bstr_t object as follows:

#include <comdef.h>
....
{
...
    char* s = "Hello";
    _bstr_t t(s);
    BSTR bs = t.copy();
....
}

Regards,
:)
0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6284876
another nice way for performing conversions:

BSTR method x(char* sz) {
  USES_CONVERSION;

  return A2BSTR(sz);
}

You might take a look at the following MSDN article:

http://msdn.microsoft.com/library/en-us/wceatl/htm/atlapi2_42.asp

0
 

Author Comment

by:gshriki
ID: 6285668
can u plz give me the BSTR to char* conversion?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

927 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