Solved

casting from BSTR <-> char *

Posted on 2000-02-27
7
359 Views
Last Modified: 2010-04-10
Hi folks,

am i allowed to do:

char *str1 = strdup("hello");
BSTR str2 = (BSTR) str1;

or vice-versa?

what happens if i do this, and if it is not legal, how can i convert from wchar to char?

thanks and regards.
0
Comment
Question by:meverest
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2562347
No a BSTR uses a different format to represent strings.  It has an initial length byte and the string must be dynmaically allocated by...something...I'm nost sure of those details.  The string that you are starting with is a C-style string, it is a NUL terminated string and it is not necesarily dynamically allocated.  

To convert a C-stly string to a BSTR you use the SysAllocString() function defined in oleauto.h.  Like

BSTR str2 = SysAllocString("hello");
0
 
LVL 22

Expert Comment

by:nietod
ID: 2562352
>> how can i convert from wchar to char?
use WideCharToMultiByte().
0
 
LVL 32

Expert Comment

by:jhance
ID: 2562732
If you're using MFC (actually I think this works in non-MFC apps as well), you can also use the string-type conversion macros in afxpriv.h.

For instance, to convert from WCHAR to CHAR, you can use the W2T macro.

There is a whole family of these that convert from almost any string type to almost any other.  (I say "almost" since there is probably some type that is not supported.)
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 23

Expert Comment

by:chensu
ID: 2562788
The macros that jhance mentioned are actually from ATL. See

TN059: Using MFC MBCS/Unicode Conversion Macros
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/visualc/vcmfc/_mfcnotes_tn059.htm

String Conversion Macros
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/visualc/vcmfc/_atl_string_conversion_macros.htm

If you are using Visual C++ 5.0 or above, you may also use the _bstr_t class since it has the operator wchar_t*, char*.

_bstr_t
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/visualc/vclang/_pluslang__bstr_t.htm
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2563143
nietod> To convert a C-stly string to a BSTR you use the
 nietod> SysAllocString() function defined in oleauto.h.  Like

 nietod> BSTR str2 = SysAllocString("hello");

Your code doesn't compile.  I think you meant to show

  BSTR str2 = SysAllocString(L"hello");

..B ekiM
0
 
LVL 22

Expert Comment

by:nietod
ID: 2563184
Thanks--everyone.
0
 
LVL 37

Author Comment

by:meverest
ID: 2563600
thanks netiod,

although i am already using SysAllocString() (and SysReAllocString()), i found what i was after when lloking into WideCharToMultiByte and related functions.

regards.
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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

16 Experts available now in Live!

Get 1:1 Help Now