Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

casting from BSTR <-> char *

Posted on 2000-02-27
7
Medium Priority
?
374 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 300 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

670 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