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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Doubt on CString and BSTR LPWSTR

Doubt on CString and BSTR LPWSTR

Scheduled Pinned Locked Moved C / C++ / MFC
questionworkspace
7 Posts 5 Posters 0 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.
  • R Offline
    R Offline
    Raj Prathap
    wrote on last edited by
    #1

    Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But, CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja Pratap

    N CPalliniC L M 4 Replies Last reply
    0
    • R Raj Prathap

      Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But, CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja Pratap

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      Raj Prathap wrote:

      whether the first byte is the data or the length??

      Starting address of a BSTR variable always points to real data that is the character string and length is stored at an offset minus starting address of a BSTR variable. For eg:

      BSTR bstrLengthTest = ::SysAllocString( L"Nibu" );
      DWORD dwLength = \*(DWORD\*)((DWORD)bstrLengthTest-sizeof(DWORD));
      cout << endl<< "String length in bytes is: " <<  dwLength << endl;
      

      Hence BSTR always works both ways but an LCWSTR won't always work both ways.


      Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com

      1 Reply Last reply
      0
      • R Raj Prathap

        Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But, CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja Pratap

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Raj Prathap wrote:

        But the contents in that pointer are different. BSTR is a wide char string with length in the first byte.

        Actually the length field is 4 bytes long.

        Raj Prathap wrote:

        BSTR bst = AllocSysString("bstr");

        The above should be: BSTR bst = SysAllocString(L"bstr");

        Raj Prathap wrote:

        The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies

        The trick is in SysAllocString that returs a pointer to the data string, not to the length prefix, see here http://msdn2.microsoft.com/en-us/library/ms221069.aspx[^] for a better explanation. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        R 1 Reply Last reply
        0
        • R Raj Prathap

          Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But, CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja Pratap

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Raj Prathap wrote:

          how the compiler identifies.

          It doesn't the code tells it (declares) what the types are. as in the following "declarations": LPWSTR lpw BSTR bst Also....

          Raj Prathap wrote:

          My doubt is how at run time the decision on the first byte is decided?

          Raj Prathap wrote:

          how the compiler identifies.

          The compiler does NOT do ANYTHING at "run time".

          1 Reply Last reply
          0
          • R Raj Prathap

            Hi All, I have on basic doubt. In windows environment both BSTR and LPWSTR are typedef ed to unsigned short *. But the contents in that pointer are different. BSTR is a wide char string with length in the first byte. LPWSTR is a null terminated string. But, CString str; LPWSTR lpw = L"lpwstr"; BSTR bst = AllocSysString("bstr"); str = lpw; //how at runtime identified whether first char is length or actual data? str = bsw; //Both of the statements work fine The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies. Thanks and regards, Raja Pratap

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            You have to keep track yourself of which strings are BSTRs and which are just C-style strings. It would be nice if the compiler could do that, but the fact that BSTR and LPWSTR are typedef'd to the same thing makes that impossible.

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

            1 Reply Last reply
            0
            • CPalliniC CPallini

              Raj Prathap wrote:

              But the contents in that pointer are different. BSTR is a wide char string with length in the first byte.

              Actually the length field is 4 bytes long.

              Raj Prathap wrote:

              BSTR bst = AllocSysString("bstr");

              The above should be: BSTR bst = SysAllocString(L"bstr");

              Raj Prathap wrote:

              The code works fine, and behaves as we expect. My doubt is how at run time the decision on the first byte is decided? i.e whether the first byte is the data or the length?? how the compiler identifies

              The trick is in SysAllocString that returs a pointer to the data string, not to the length prefix, see here http://msdn2.microsoft.com/en-us/library/ms221069.aspx[^] for a better explanation. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              R Offline
              R Offline
              Raj Prathap
              wrote on last edited by
              #6

              Thanks. The link provided is really helpful. I was not clear about the parts of the BSTR i.e length+data+terminator. I used think terminator would not be there since the length is already there. Any ways with the link to MSDN article, all doubts are cleared. Thanks a ton. /pratap

              CPalliniC 1 Reply Last reply
              0
              • R Raj Prathap

                Thanks. The link provided is really helpful. I was not clear about the parts of the BSTR i.e length+data+terminator. I used think terminator would not be there since the length is already there. Any ways with the link to MSDN article, all doubts are cleared. Thanks a ton. /pratap

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                You are welcome :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                In testa che avete, signor di Ceprano?

                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