Solved

casting from BSTR <-> char *

Posted on 2000-02-27
7
357 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
Comment Utility
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
Comment Utility
>> how can i convert from wchar to char?
use WideCharToMultiByte().
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks--everyone.
0
 
LVL 37

Author Comment

by:meverest
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

744 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

8 Experts available now in Live!

Get 1:1 Help Now