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 in sprintf

BSTR in sprintf

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
5 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.
  • I Offline
    I Offline
    itkid
    wrote on last edited by
    #1

    Hi, I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name); Thanks

    D J T 3 Replies Last reply
    0
    • I itkid

      Hi, I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name); Thanks

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      itkid wrote:

      ...however it is showing only first charactor.

      What is it? How are you verifying this? This works fine for me:

      void main( void )
      {
      BSTR name(_T("David"));
      TCHAR buff[32];
      _stprintf(buff, _T("%s"), name);
      }


      "Take only what you need and leave the land as you found it." - Native American Proverb

      S 1 Reply Last reply
      0
      • I itkid

        Hi, I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name); Thanks

        J Offline
        J Offline
        James R Twine
        wrote on last edited by
        #3

        That will happen if you try to treat a wide (UNICODE/BSTR) string as an narrow (ANSI) string.    If using the ANSI version of sprintf(...), %s means ANSI string and %S (uppercase 'S') means UNICODE string.  For the UNICODE version, the meaning of the format specifiers is reversed.    Additionally, if you pass a wide version of a string as the format specification to the (ANSI version of) sprintf(...), only the first character will be formatted into the buffer ('N' in your example above).    Peace! -=- James


        If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        DeleteFXPFiles & CheckFavorites (Please rate this post!)

        1 Reply Last reply
        0
        • I itkid

          Hi, I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name); Thanks

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          itkid wrote:

          I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name);

          What about this

          CString str;
          str.Format("Name =%s", name);
          BSTR buff= str.AllocSysString()

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta VC Forum Q&A :- I/ IV

          1 Reply Last reply
          0
          • D David Crow

            itkid wrote:

            ...however it is showing only first charactor.

            What is it? How are you verifying this? This works fine for me:

            void main( void )
            {
            BSTR name(_T("David"));
            TCHAR buff[32];
            _stprintf(buff, _T("%s"), name);
            }


            "Take only what you need and leave the land as you found it." - Native American Proverb

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

            DavidCrow wrote:

            BSTR name(_T("David"));

            This is dodgy. A BSTR is not just a wide string - It has a length prefixed before the first character (before where the pointer points). Code like this means that this length prefix is not initialized. This is a serious error and bloody hard to track down. Here are a few examples which show some of the problems: BSTR name(L"David"); // BAD CODE!!!!! UINT Len = SysStringLen(name); // Wrong length will be returned. SysFreeString(name); // Crash if you're lucky! Steve

            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