Solved

casting from BSTR <-> char *

Posted on 2000-02-27
7
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

752 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