BSTR --> CString ?
-
donno whether this is the fastest or easiest, but I've been coding it like this: CString str = static_cast( bstr );
Does that work ? I don't see how it can..... Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
-
What's the fastest and easiest way to convert a
BSTR
to a MFCCString
? Daniel ;) --------------------------- Never change a running system!_bstr_t is a wrapper class that does the conversion for you, so you can do this: CString s((_bstr_t)BSTR); This will create a _bstr_t, which will return a char * to the CString constructor. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
-
_bstr_t is a wrapper class that does the conversion for you, so you can do this: CString s((_bstr_t)BSTR); This will create a _bstr_t, which will return a char * to the CString constructor. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
-
_bstr_t is a wrapper class that does the conversion for you, so you can do this: CString s((_bstr_t)BSTR); This will create a _bstr_t, which will return a char * to the CString constructor. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
Thanks! Daniel ;) --------------------------- Never change a running system!
-
Does that work ? I don't see how it can..... Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
Yes, this works, but be careful: by definition a NULL BSTR is an empty string. This cast can be dangerous. Your solution is safer. Q261186 - Computer Randomly Plays Classical Music
-
No worries :-) Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
-
What's the fastest and easiest way to convert a
BSTR
to a MFCCString
? Daniel ;) --------------------------- Never change a running system!the best & easy way to conver BSTR to string is use the com_util helper as follows: _bstr_t tmpBSTR = _com_util::ConvertStringToBSTR(szString); or the other way around: CString szString = _com_util::ConvertBSTRToString((BSTR)tmpBSTR); you can check out the definitions of this functions in COMUTIL.H hope this helps ya Yaron Ask not what the application can do for you, ask what you can do for your application
-
the best & easy way to conver BSTR to string is use the com_util helper as follows: _bstr_t tmpBSTR = _com_util::ConvertStringToBSTR(szString); or the other way around: CString szString = _com_util::ConvertBSTRToString((BSTR)tmpBSTR); you can check out the definitions of this functions in COMUTIL.H hope this helps ya Yaron Ask not what the application can do for you, ask what you can do for your application
Nope, the best and easiest way to construct a CString from a BSTR is to use the LPCWSTR constructor that CString has. So:
CString str( (LPCWSTR)bstr );
should do what you want. Note that this works because a BSTR is also a LPCWSTR (although a LPCWSTR is not necessarily a BSTR!). This method means that no temporary copies will be made, the Unicode-to-MBCS conversion will be made inside CString and no unnecessary string manipulation happens. Using _bstr_t will also work but with the overhead of a temporary copy of both the _bstr_t and the char* (_bstr_t will first construct itself from BSTR, then it will make an MBCS version and then CString will make a copy of it).
Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!
-
Nope, the best and easiest way to construct a CString from a BSTR is to use the LPCWSTR constructor that CString has. So:
CString str( (LPCWSTR)bstr );
should do what you want. Note that this works because a BSTR is also a LPCWSTR (although a LPCWSTR is not necessarily a BSTR!). This method means that no temporary copies will be made, the Unicode-to-MBCS conversion will be made inside CString and no unnecessary string manipulation happens. Using _bstr_t will also work but with the overhead of a temporary copy of both the _bstr_t and the char* (_bstr_t will first construct itself from BSTR, then it will make an MBCS version and then CString will make a copy of it).
Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!
The casting is unneeded here, since: typedef wchar_t *LPCWSTR; typedef wchar_t *BSTR; There is no need for casting, unless bstr is not a pure BSTR. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeard -
The casting is unneeded here, since: typedef wchar_t *LPCWSTR; typedef wchar_t *BSTR; There is no need for casting, unless bstr is not a pure BSTR. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeard -
Correct. It does however make it clear what constructor we are using - but then again we might not care :-)
Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!
It is a gratuitious cast and has the potential to break the code in the future. NEVER use an explicit cast unless you have to. -------- There are 10 types of people in this world. Those who know binary and those who don't.
-
What's the fastest and easiest way to convert a
BSTR
to a MFCCString
? Daniel ;) --------------------------- Never change a running system!CString str = bstr;
Regards, Alvaro There is much pleasure to be gained from useless knowledge. - Bertrand Russell