Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. BSTR / string

BSTR / string

Scheduled Pinned Locked Moved COM
c++comhelpquestion
4 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    William Bartholomew
    wrote on last edited by
    #1

    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:

    E L 3 Replies Last reply
    0
    • W William Bartholomew

      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:

      E Offline
      E Offline
      Erik Thompson
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • W William Bartholomew

        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:

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        /* ------------------------------------------------------------------------- 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; }

        1 Reply Last reply
        0
        • W William Bartholomew

          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:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          /* ------------------------------------------------------------------------- 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; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups