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. C / C++ / MFC
  4. BSTR supports Unicode

BSTR supports Unicode

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 4 Posters 1 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.
  • S Offline
    S Offline
    san123pune
    wrote on last edited by
    #1

    Hi , Is BSTR support UNICODE? I have a component method that takes BSTR as argument. That component is build with UNICODE support. In that method, I have written this code.
    TCHAR tChar[255]; _stprintf(tChar,_T("%s"),(LPCTSTR)bstrName); // then print
    Is it correct way ? Thanks in advance

    S L T 3 Replies Last reply
    0
    • S san123pune

      Hi , Is BSTR support UNICODE? I have a component method that takes BSTR as argument. That component is build with UNICODE support. In that method, I have written this code.
      TCHAR tChar[255]; _stprintf(tChar,_T("%s"),(LPCTSTR)bstrName); // then print
      Is it correct way ? Thanks in advance

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      A BSTR is a Unicode string (but beware: a Unicode string is ***NOT*** a BSTR). No conversion like your doing may be needed.

      Steve

      1 Reply Last reply
      0
      • S san123pune

        Hi , Is BSTR support UNICODE? I have a component method that takes BSTR as argument. That component is build with UNICODE support. In that method, I have written this code.
        TCHAR tChar[255]; _stprintf(tChar,_T("%s"),(LPCTSTR)bstrName); // then print
        Is it correct way ? Thanks in advance

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

        Hello, i delete my post because is not correct!!! Take a look at next post from Stephen Hewitt Regards termal -- modified at 9:44 Tuesday 3rd April, 2007

        S 1 Reply Last reply
        0
        • L Lost User

          Hello, i delete my post because is not correct!!! Take a look at next post from Stephen Hewitt Regards termal -- modified at 9:44 Tuesday 3rd April, 2007

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          termal wrote:

          BSTR bstr = _T("text");

          This is a serious coding error. For a start a BSTR is length prefixed. This means that the length of the BSTR is stored before the pointer. Your string is not. Secondly BSTRs are always Unicode; use L"your string" not _T. Finally, in most instances, a specific allocater is assumed. Your code should look like this:

          BSTR bstr = SysAllocString(L"text");

          To free the string use SysFreeString. No offence, but this is basic COM; I recommend doing some serious reading as creating software lacking these fundamentals will end in tears

          Steve

          L 1 Reply Last reply
          0
          • S Stephen Hewitt

            termal wrote:

            BSTR bstr = _T("text");

            This is a serious coding error. For a start a BSTR is length prefixed. This means that the length of the BSTR is stored before the pointer. Your string is not. Secondly BSTRs are always Unicode; use L"your string" not _T. Finally, in most instances, a specific allocater is assumed. Your code should look like this:

            BSTR bstr = SysAllocString(L"text");

            To free the string use SysFreeString. No offence, but this is basic COM; I recommend doing some serious reading as creating software lacking these fundamentals will end in tears

            Steve

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

            Hello, thank you for explanation how to use BSTR! with best regards! termal

            S 1 Reply Last reply
            0
            • L Lost User

              Hello, thank you for explanation how to use BSTR! with best regards! termal

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              There are classes such as ATL's CComBSTR and _bstr_t (not part of ATL) which make working with BSTRs easier and safer.

              Steve

              1 Reply Last reply
              0
              • S san123pune

                Hi , Is BSTR support UNICODE? I have a component method that takes BSTR as argument. That component is build with UNICODE support. In that method, I have written this code.
                TCHAR tChar[255]; _stprintf(tChar,_T("%s"),(LPCTSTR)bstrName); // then print
                Is it correct way ? Thanks in advance

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #7

                I know I'm being a bit anal, but if you are going to _T and TCHAR, then you need to write you app to build in both ANSI and UNICODE. Your code gives the impression that it might build in ANSI but it in fact will not work. To make your code work in both ansi and UNICODE TCHAR tChar[255]; _stprintf(tChar,_T("%ls"),(LPCWSTR)bstrName); // then print

                Tim Smith I'm going to patent thought. I have yet to see any prior art.

                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