Solved

covert char * to BSTR *

Posted on 2001-07-15
5
550 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 200 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lambda for random numbers problem 7 113
C++ Class Serialization, Encapsulation and Formatted Output 8 69
Exception thrown at 0x00007FFD5BC81F28 7 44
What is sub-make ? 2 61
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…
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 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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

803 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