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. correct way to copy bstr ?

correct way to copy bstr ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 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.
  • M Offline
    M Offline
    Mogaambo
    wrote on last edited by
    #1

    BSTR strLoggedInUser=GetLoggedInUser();//is this line of code is correct or i use wcscpy here. BSTR value=::SysAllocStringLen(L"",::SysStringLen(strLoggedInUser)); wcscpy(value,strLoggedInUser); ::SysFreeString(strLoggedInUser); /////////////////////////////////////////////////////////////////////// HRESULT hr = pclsObj->Get(L"Manufacturer", 0, &vtProp, 0, 0); BSTR strItemName =vtProp.vt==VT_NULL?L"-NA-":vtProp.bstrVal;// here too is this line of code is correct or i use wcscpy here.

    “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

    _ G 2 Replies Last reply
    0
    • M Mogaambo

      BSTR strLoggedInUser=GetLoggedInUser();//is this line of code is correct or i use wcscpy here. BSTR value=::SysAllocStringLen(L"",::SysStringLen(strLoggedInUser)); wcscpy(value,strLoggedInUser); ::SysFreeString(strLoggedInUser); /////////////////////////////////////////////////////////////////////// HRESULT hr = pclsObj->Get(L"Manufacturer", 0, &vtProp, 0, 0); BSTR strItemName =vtProp.vt==VT_NULL?L"-NA-":vtProp.bstrVal;// here too is this line of code is correct or i use wcscpy here.

      “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      BSTR value=::SysAllocString(strLoggedInUser);

      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

      1 Reply Last reply
      0
      • M Mogaambo

        BSTR strLoggedInUser=GetLoggedInUser();//is this line of code is correct or i use wcscpy here. BSTR value=::SysAllocStringLen(L"",::SysStringLen(strLoggedInUser)); wcscpy(value,strLoggedInUser); ::SysFreeString(strLoggedInUser); /////////////////////////////////////////////////////////////////////// HRESULT hr = pclsObj->Get(L"Manufacturer", 0, &vtProp, 0, 0); BSTR strItemName =vtProp.vt==VT_NULL?L"-NA-":vtProp.bstrVal;// here too is this line of code is correct or i use wcscpy here.

        “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

        G Offline
        G Offline
        Garth J Lancaster
        wrote on last edited by
        #3

        let me suggest something for you ... http://lmgtfy.com/?q=copy+bstr[^]

        M 1 Reply Last reply
        0
        • G Garth J Lancaster

          let me suggest something for you ... http://lmgtfy.com/?q=copy+bstr[^]

          M Offline
          M Offline
          Mogaambo
          wrote on last edited by
          #4

          i know how to copy bstr, but my problem is that when i assign BSTR b=L"hello"; some where in code the value of b changes to " some garbage value". but if i use wcscpy, the value of b doesn't change. please read my question and answer me.

          “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

          T CPalliniC 2 Replies Last reply
          0
          • M Mogaambo

            i know how to copy bstr, but my problem is that when i assign BSTR b=L"hello"; some where in code the value of b changes to " some garbage value". but if i use wcscpy, the value of b doesn't change. please read my question and answer me.

            “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

            T Offline
            T Offline
            tolw
            wrote on last edited by
            #5

            Take a look at: http://msdn.microsoft.com/en-us/library/ms221069.aspx[^] It is clearly stated that asigning anything to a BSTR should be done using SysAllocString. So using wcscpy or a regular asignment operator (=) is wrong by definition. You can try something like this (I assume GetLoggedInUser() returns a wchar *):

            BSTR strLoggedInUser = ::SysAllocStringLen( GetLoggedInUser() );

            //..do something..

            //Call this when you are sure the BSTR is no longer used!!
            ::SysFreeString(strLoggedInUser);

            1 Reply Last reply
            0
            • M Mogaambo

              i know how to copy bstr, but my problem is that when i assign BSTR b=L"hello"; some where in code the value of b changes to " some garbage value". but if i use wcscpy, the value of b doesn't change. please read my question and answer me.

              “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

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

              Mogaambo wrote:

              BSTR b=L"hello";

              That is bad. :)

              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.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              M 1 Reply Last reply
              0
              • CPalliniC CPallini

                Mogaambo wrote:

                BSTR b=L"hello";

                That is bad. :)

                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.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                M Offline
                M Offline
                Mogaambo
                wrote on last edited by
                #7

                BSTR b=SysAllocString(L"hello"); BSTR c=b;// here wcscpy should be used or it will work

                “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

                CPalliniC 1 Reply Last reply
                0
                • M Mogaambo

                  BSTR b=SysAllocString(L"hello"); BSTR c=b;// here wcscpy should be used or it will work

                  “You will never be a leader unless you first learn to follow and be led.” –Tiorio "Coming together is a beginning, staying together is progress, and working together is success." Henry Ford

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

                  Mogaambo wrote:

                  BSTR b=SysAllocString(L"hello");

                  The above is fine.

                  Mogaambo wrote:

                  BSTR c=b;// here wcscpy should be used or it will work

                  The above is bad. Use, instead

                  BSTR c = SysAllocString(b);

                  Please read [^]. :)

                  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.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  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