BSTR / string
-
I have a method in an ATL COM component declared as follows: STDMETHODIMP CHash::Digest(BSTR szString, BSTR* szRetval) And I want to copy the szString variable into a string object, so I tried the following: //copy the source into a string object string source; source = *szString; However when I do this I only get the first character... what am I doing wrong? According to the documentation I found I should be able to do this but this doesn't work either: string source = szString; Help...? :confused:
-
I have a method in an ATL COM component declared as follows: STDMETHODIMP CHash::Digest(BSTR szString, BSTR* szRetval) And I want to copy the szString variable into a string object, so I tried the following: //copy the source into a string object string source; source = *szString; However when I do this I only get the first character... what am I doing wrong? According to the documentation I found I should be able to do this but this doesn't work either: string source = szString; Help...? :confused:
The reason is that the string STL class is templated for types of char. Since a BSTR is double byte the string template assignment operator sees the second byte of the first char of your BSTR value as a null and terminates the string and stops continuing. If you want to copy the BSTR value into a STL "string" class then use the STL wstring template which is for Wide charater strings (double byte). wstring source; source = szString; Or convert the szString to a single byte string using one of the converstion functions available like wcstombs and then copy it into your char string source variable. or use the USES_CONVERSION macro and use OLE2A() like follows. Digest(BSTR szString, ...) { USES_CONVERSION; string source = OLE2A(szString); } Cheers, -Erik
-
I have a method in an ATL COM component declared as follows: STDMETHODIMP CHash::Digest(BSTR szString, BSTR* szRetval) And I want to copy the szString variable into a string object, so I tried the following: //copy the source into a string object string source; source = *szString; However when I do this I only get the first character... what am I doing wrong? According to the documentation I found I should be able to do this but this doesn't work either: string source = szString; Help...? :confused:
/* ------------------------------------------------------------------------- Function: FromBstrToStr Converts a BSTR to ( not double byte...), well it´s no the best method, but it seems to be the easiest one, without leaking memory ( well maybe...) ------------------------------------------------------------------------ */ string FromBstrToStr(BSTR TextB) { string ret; _bstr_t bstrConv(TextB, FALSE); ret = (const char *) bstrConv; return ret; }
-
I have a method in an ATL COM component declared as follows: STDMETHODIMP CHash::Digest(BSTR szString, BSTR* szRetval) And I want to copy the szString variable into a string object, so I tried the following: //copy the source into a string object string source; source = *szString; However when I do this I only get the first character... what am I doing wrong? According to the documentation I found I should be able to do this but this doesn't work either: string source = szString; Help...? :confused:
/* ------------------------------------------------------------------------- Function: FromBstrToStr Converts a BSTR to ( not double byte...), well it´s no the best method, but it seems to be the easiest one, without leaking memory ( well maybe...) ------------------------------------------------------------------------ */ string FromBstrToStr(BSTR TextB) { string ret; _bstr_t bstrConv(TextB, FALSE); ret = (const char *) bstrConv; return ret; }