Board index » Visual Studio » Converting BSTR to char*

Converting BSTR to char*

Visual Studio31
Hi NG.



First of, I'm new to VC++ and C++ in general, so don't bite my head of

for asking noob questions please :)



I'm trying to build a rather simple DLL and I need to convert a BSTR to

char*.



I've searched and found a lot of examples where people use ATL macros or

functions, but whenever I try to write anything ATL related, my compiler

starts to whine as if it had never heared of ATL.



My qyestions are:

-----------------

1) if my project doesn't have ATL enabled or whatever, how do I

inport/include somthing to enable/access ATL stuff?



2) how do I make this conversion with and without ATL stuff?



Any help would be greatly appreaciated, thanks :)



/Aidal



PS. I'm using VC++ 6.0.


-
 

Re:Converting BSTR to char*

Aidal wrote:



Quote
Hi NG.



First of, I'm new to VC++ and C++ in general, so don't bite my head of

for asking noob questions please :)



I'm trying to build a rather simple DLL and I need to convert a BSTR to

char*.





First, you should realize that a BSTR is a (special kind of) wide

character string (16-bit), whereas char* is a narrow (8-bit) string.



Second, you may not really be wanting a char*. If you are in a Unicode

build (a wise choice, usually) then you probably want a wchar_t*. Learn

to use TCHAR, LPTSTR, etc, which are narrow in ANSI builds and wide in

UNICODE builds.



Third, you probably really want a const char* (or const wchar_t*).



Fourth, use of BSTR is much easier if you use the _bstr_t wrapper. This

actually maintains both a narrow and wide string internally, and has

cast operators for both const char* and const wchar_t*.



C++ string handling in Windows is not easy. Good luck!



David Wilkinson

-

Re:Converting BSTR to char*

Aidal <no@address.com>wrote:

Quote


First of, I'm new to VC++ and C++ in general, so don't bite my head of

for asking noob questions please :)



I'm trying to build a rather simple DLL and I need to convert a BSTR to

char*.



I've searched and found a lot of examples where people use ATL macros or

functions, but whenever I try to write anything ATL related, my compiler

starts to whine as if it had never heared of ATL.



My qyestions are:

-----------------

1) if my project doesn't have ATL enabled or whatever, how do I

inport/include somthing to enable/access ATL stuff?



MSDN is your friend.



#include <atlbase.h>



Quote
2) how do I make this conversion with and without ATL stuff?



There are many ways. One way:



USES_CONVERSION;

LPSTR szAsciiVersion = W2A(pBSTR);



You can also use WideCharToMultiByte, or the C runtime library equivalent,

wcstombs.



There is also the CComBSTR wrapper class, which can let you manipulate

BSTRs. Again, MSDN is your friend.



Quote
PS. I'm using VC++ 6.0.



You are using an antique. That product is 8 years old and 3 major

revisions behind.

--

- Tim Roberts, timr@probo.com

Providenza & Boekelheide, Inc.

-

Re:Converting BSTR to char*

"Aidal" wrote:

Quote
First of, I'm new to VC++ and C++ in general, so don't

bite my head of for asking noob questions please :)



No, we're not feed on human brains, I promise. :)



Quote
I'm trying to build a rather simple DLL and I need to

convert a BSTR to char*.



I've searched and found a lot of examples where people use

ATL macros or functions, but whenever I try to write

anything ATL related, my compiler starts to whine as if it

had never heared of ATL.



Probably, it really hadn't. So, you will need to tell him

about ATL a little bit.



Quote
My qyestions are:

-----------------

1) if my project doesn't have ATL enabled or whatever, how

do I inport/include somthing to enable/access ATL stuff?



All you need to do is to include correct header file. When

you're looking up for function/class in MSDN, then always

pay attention to "Requirements" section in the bottom of

page. You'll find there everything you need in order to use

this function/class: headers, libs, OS versions, etc..



However, if your intent is to create COM object with help of

ATL, then including header file won't be enough. In that

case I suggest you to go through ATL tutorial to get the

whole picture:



"ATL Tutorial"

msdn.microsoft.com/library/en-us/vcmfc98/html/_atl_atl_tutorial.asp">msdn.microsoft.com/library/en-us/vcmfc98/html/_atl_atl_tutorial.asp



Quote


2) how do I make this conversion with and without ATL

stuff?





String conversion can be achived without ATL, too. The

easiest way is to use `_bstr_t' class. It's part of COM

compiler support, i.e. set of C++ classes provided by

compiler vendor in order to alleviate COM harshness.



[Note: _bstr_t conversion (BSTR->const char*) in VC++ 6.0

is performed in memory allocated on stack, so if you have

really large string to convert, you can cause stack

overflow. In further version of VS it was changed so _bstr_t

always allocates memory on heap for conversion.]



Basically, conversion between wide character string

(wchar_t) and narrow character strings (char) performed with

Win32 API functons: MultiByteToWideChar and

WideCharToMultiByte. All other classes, frameworks etc. use

these functions internally hiding from developer nasty

details of conversion: buffer allocations, error checking

and so on.



I strongly suggest you to read following article:



"Chapter 6: Strings"

msdn.microsoft.com/library/en-us/dnw32dev/html/ora_apiprog6_topic1.asp">msdn.microsoft.com/library/en-us/dnw32dev/html/ora_apiprog6_topic1.asp



This is excerpt from VB programming book. However, this

chapter gives excellent overview on existing strings in

Windows world and how to convert between different kinds of

strings.





HTH

Alex





-

Re:Converting BSTR to char*

"Alex Blekhman" wrote:

Quote
No, we're not feed on human brains, I promise. :)



Speek for yourself.



--

- Mark Randall

www.temporal-solutions.co.uk">www.temporal-solutions.co.uk

www.awportals.com">www.awportals.com





-

Re:Converting BSTR to char*

Tim Roberts skrev:

Quote
Aidal <no@address.com>wrote:

>First of, I'm new to VC++ and C++ in general, so don't bite my head of

>for asking noob questions please :)

>

>I'm trying to build a rather simple DLL and I need to convert a BSTR to

>char*.

>

>I've searched and found a lot of examples where people use ATL macros or

>functions, but whenever I try to write anything ATL related, my compiler

>starts to whine as if it had never heared of ATL.

>

>My qyestions are:

>-----------------

>1) if my project doesn't have ATL enabled or whatever, how do I

>inport/include somthing to enable/access ATL stuff?



MSDN is your friend.



#include <atlbase.h>



>2) how do I make this conversion with and without ATL stuff?



There are many ways. One way:



USES_CONVERSION;

LPSTR szAsciiVersion = W2A(pBSTR);



You can also use WideCharToMultiByte, or the C runtime library equivalent,

wcstombs.



There is also the CComBSTR wrapper class, which can let you manipulate

BSTRs. Again, MSDN is your friend.



>PS. I'm using VC++ 6.0.



You are using an antique. That product is 8 years old and 3 major

revisions behind.



Thanks for your reply Tim :)



I will try this out right away - and I did search for how to enable ATL

but I must just not have hit the right phrase because there wasn't

anything about it.



I do know that my envionment is an antique hehe, but I normally work

with .NET and not C++ and since this was the version I had available I

thought that would do. Besides the application that has to call this

code is even older.



/Aidal

-