How to convert a CString to _bstr_t
-
Hi, Can any one expplain How to convert a CString to _bstr_t...... thanks in advance
Thanks a lot
-
Hi, Can any one expplain How to convert a CString to _bstr_t...... thanks in advance
Thanks a lot
One of the constructors of
_bstr_t
takes aconst char*
as argument. Since CString has a type conversion operatorLPCTSTR
that returns aconst char*
you can do the following:CString str = "The string";
_bstr_t bstrTest( str ); // When you declare your _bstr_t variable, or
bstrTest = _bstr_t( str ); // when assigning the variable
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
One of the constructors of
_bstr_t
takes aconst char*
as argument. Since CString has a type conversion operatorLPCTSTR
that returns aconst char*
you can do the following:CString str = "The string";
_bstr_t bstrTest( str ); // When you declare your _bstr_t variable, or
bstrTest = _bstr_t( str ); // when assigning the variable
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown[fx: pedant mode on]
Roger Stoltz wrote:
Since CString has a type conversion operator LPCTSTR that returns a const char* you can do the following:
No it doesn't. It returns const TCHAR*, which in a UNICODE build won't be the same as const char*. However, _bstr_t has a constructor that accepts const wchar_t* as well as one for const char*, so it will work in either case. [fx: pedant mode off] :)
Steve S Developer for hire
-
[fx: pedant mode on]
Roger Stoltz wrote:
Since CString has a type conversion operator LPCTSTR that returns a const char* you can do the following:
No it doesn't. It returns const TCHAR*, which in a UNICODE build won't be the same as const char*. However, _bstr_t has a constructor that accepts const wchar_t* as well as one for const char*, so it will work in either case. [fx: pedant mode off] :)
Steve S Developer for hire
Now you're just being picky Steve... but you're right of course. :-D However, I think the OP got the picture and depending on the OP's level of expertise it may have been confusing to start talking about unicode.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Now you're just being picky Steve... but you're right of course. :-D However, I think the OP got the picture and depending on the OP's level of expertise it may have been confusing to start talking about unicode.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknownOf course I'm being picky. When you get to my age, it's one of the few pleasures left in life that doesn't cost money or end up causing some kind of muscular spasm... X| I agree that it might have been confusing, which is one of the problems of CP. It's hard to know what level the OP is at (or in some cases, they think they're at), and that makes it difficult sometimes to give the 'right' answer, because they may end up worse off than before. I did like the thread here[^] where someone else came in and finished explaining it while I was asleep :)
Steve S Developer for hire