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. Append BStr

Append BStr

Scheduled Pinned Locked Moved C / C++ / MFC
performancealgorithmsdebugginghelpannouncement
27 Posts 6 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.
  • K KarstenK

    To free up the strings is correct and necessary, BUT NOT in a subroutine. Make it after the sub routine call. :doh:

    Press F1 for help or google it. Greetings from Germany

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

    This my calling code tell me where do i free BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }

    “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

    M K 2 Replies Last reply
    0
    • M Mogaambo

      This my calling code tell me where do i free BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }

      “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

      M Offline
      M Offline
      Madhu Nair 0
      wrote on last edited by
      #13

      Mogaambo wrote:

      BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n");

      What about changing BSTR to _bstr_t ? Defined in comutil.h The MSDN link says that, A _bstr_t object encapsulates the BSTR data type. The class manages resource allocation and deallocation through function calls to SysAllocString and SysFreeString and other BSTR APIs when appropriate. The _bstr_t class uses reference counting to avoid excessive overhead. --++++++--- Use the += operator of _bstr_t to append characters to the end. if value is also declared as a type of _bstr_t then code can be re-written as,

      value += strComma;
      value += strNewLine;

      :thumbsup:

      modified on Wednesday, May 13, 2009 5:12 AM

      M 1 Reply Last reply
      0
      • M Mogaambo

        Here is my calling code. value variable is of type BSTR BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }

        “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
        #14

        It looks like the problem is in your bad mixing of char and wchar calls: with the following line

        BSTR st3 = ::SysAllocStringByteLen(NULL,destLen+srcLen);

        you're allocating (destLen + srcLen + 1) bytes. while in the following calls

        wcscpy(st3, dest);
        wcscat(st3, src);

        you need, (destLen + srcLen + 2) bytes. :)

        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
        • M Mogaambo

          This my calling code tell me where do i free BSTR GetNewRow() { value=GetLoggedInUser(); BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n"); value=StringAppend(value,strComma); value=StringAppend(value,strCategory); value=StringAppend(value,strComma); value=StringAppend(value,strItemName); value=StringAppend(value,strComma); value=StringAppend(value,strInstallDate); value=StringAppend(value,strComma); value=StringAppend(value,strVersion); value=StringAppend(value,strComma); value=StringAppend(value,strValue1); value=StringAppend(value,strComma); value=StringAppend(value,strValue2); value=StringAppend(value,strComma); value=StringAppend(value,strValue3); value=StringAppend(value,strNewLine); ::SysFreeString(strComma); ::SysFreeString(strNewLine); return value; }

          “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

          K Offline
          K Offline
          KarstenK
          wrote on last edited by
          #15

          this is very lame code. You better make at first the output string with CString (or otherwise) and at last ::SysAllocString() :rolleyes:

          Press F1 for help or google it. Greetings from Germany

          M 1 Reply Last reply
          0
          • M Madhu Nair 0

            Mogaambo wrote:

            BSTR strComma=::SysAllocString(L","); BSTR strNewLine=::SysAllocString(L"\n");

            What about changing BSTR to _bstr_t ? Defined in comutil.h The MSDN link says that, A _bstr_t object encapsulates the BSTR data type. The class manages resource allocation and deallocation through function calls to SysAllocString and SysFreeString and other BSTR APIs when appropriate. The _bstr_t class uses reference counting to avoid excessive overhead. --++++++--- Use the += operator of _bstr_t to append characters to the end. if value is also declared as a type of _bstr_t then code can be re-written as,

            value += strComma;
            value += strNewLine;

            :thumbsup:

            modified on Wednesday, May 13, 2009 5:12 AM

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

            Thanks to all programming bees in helping me to solve my problems.

            “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

            1 Reply Last reply
            0
            • CPalliniC CPallini

              It looks like the problem is in your bad mixing of char and wchar calls: with the following line

              BSTR st3 = ::SysAllocStringByteLen(NULL,destLen+srcLen);

              you're allocating (destLen + srcLen + 1) bytes. while in the following calls

              wcscpy(st3, dest);
              wcscat(st3, src);

              you need, (destLen + srcLen + 2) bytes. :)

              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
              #17

              Thanks working great now . i don't know the +2 funda , actually i am c# developer but my firm wants me to develop project in c++. ::SysFreeString(dest); can i free this in StringAppend function itself or after my function returns.

              “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

                Thanks working great now . i don't know the +2 funda , actually i am c# developer but my firm wants me to develop project in c++. ::SysFreeString(dest); can i free this in StringAppend function itself or after my function returns.

                “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
                #18

                Technically you can do it inside. However you probably should revise your design, for instance using a prototype like the following for your function:

                HRESULT StringAppend( BSTR * pdst, const BSTR src);

                or, better, using, as suggested, the handy _bstr_t wrapper. :)

                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

                  Technically you can do it inside. However you probably should revise your design, for instance using a prototype like the following for your function:

                  HRESULT StringAppend( BSTR * pdst, const BSTR src);

                  or, better, using, as suggested, the handy _bstr_t wrapper. :)

                  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
                  #19

                  1. BSTR * pdst why pointer here. BSTR is itself a pointer. 2. What code you have write for StringAppend function ? I want to take some idea from you.It would be very helpful for me.It's a humble request from a programmer to a professional.

                  “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

                    1. BSTR * pdst why pointer here. BSTR is itself a pointer. 2. What code you have write for StringAppend function ? I want to take some idea from you.It would be very helpful for me.It's a humble request from a programmer to a professional.

                    “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
                    #20

                    Mogaambo wrote:

                    1. BSTR * pdst why pointer here. BSTR is itself a pointer.

                    Because the function will internally change the value of the original pointer, i.e. pdst is a INOUT parameter (this way you state clearly that the function may alter the original dst string).

                    Mogaambo wrote:

                    2. What code you have write for StringAppend function ?

                    Something like

                    HRESULT StringAppend(BSTR * pdest, const BSTR src)
                    {
                    if (pdest == NULL) return E_FAIL;
                    long destLen=::SysStringLen(*pdest);
                    long srcLen=::SysStringLen(src);

                    BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen);
                    if( tmp == NULL) return E_FAIL;
                    wcscpy(tmp, *pdest);
                    wcscat(tmp, src);
                    ::SysFreeString(*pdest);

                    pdest = &tmp;
                    *pdest = tmp;
                    return S_OK;
                    }

                    Beware: I didn't check this code (moreover I've used just the very basic E_FAIL failure return value, a good function should be more informative on failure.

                    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]

                    modified on Wednesday, May 13, 2009 8:02 AM

                    In testa che avete, signor di Ceprano?

                    M 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Mogaambo wrote:

                      1. BSTR * pdst why pointer here. BSTR is itself a pointer.

                      Because the function will internally change the value of the original pointer, i.e. pdst is a INOUT parameter (this way you state clearly that the function may alter the original dst string).

                      Mogaambo wrote:

                      2. What code you have write for StringAppend function ?

                      Something like

                      HRESULT StringAppend(BSTR * pdest, const BSTR src)
                      {
                      if (pdest == NULL) return E_FAIL;
                      long destLen=::SysStringLen(*pdest);
                      long srcLen=::SysStringLen(src);

                      BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen);
                      if( tmp == NULL) return E_FAIL;
                      wcscpy(tmp, *pdest);
                      wcscat(tmp, src);
                      ::SysFreeString(*pdest);

                      pdest = &tmp;
                      *pdest = tmp;
                      return S_OK;
                      }

                      Beware: I didn't check this code (moreover I've used just the very basic E_FAIL failure return value, a good function should be more informative on failure.

                      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]

                      modified on Wednesday, May 13, 2009 8:02 AM

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

                      BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen); dude you haven't added +2 bytes, it is crashing as that of mine, but when i added +2 it is running fine, so whenever i want allocate BSTR do i have 2 add + bytes. BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen+2);

                      “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 2 Replies Last reply
                      0
                      • K KarstenK

                        this is very lame code. You better make at first the output string with CString (or otherwise) and at last ::SysAllocString() :rolleyes:

                        Press F1 for help or google it. Greetings from Germany

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

                        What code you have written if you have to append the BSTR. It's a humble request from programmer to professional.

                        “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

                        K 1 Reply Last reply
                        0
                        • M Mogaambo

                          BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen); dude you haven't added +2 bytes, it is crashing as that of mine, but when i added +2 it is running fine, so whenever i want allocate BSTR do i have 2 add + bytes. BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen+2);

                          “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
                          #23

                          Mogaambo wrote:

                          dude you haven't added +2 bytes

                          I know, I don't need to (I'm using SysAllocStringLen, not SysAllocStringByteLen).

                          Mogaambo wrote:

                          it is crashing as that of mine, but when i added +2 it is running fine

                          This is very strange.

                          Mogaambo wrote:

                          so whenever i want allocate BSTR do i have 2 add + bytes

                          Nope. Even if your point about the crash is true (and is true, I know) this is a wrong conclusion. :)

                          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
                          • M Mogaambo

                            What code you have written if you have to append the BSTR. It's a humble request from programmer to professional.

                            “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

                            K Offline
                            K Offline
                            KarstenK
                            wrote on last edited by
                            #24

                            I would first make a complete string in a CString or TCHAR-array and at last create a BSTR.

                            Press F1 for help or google it. Greetings from Germany

                            1 Reply Last reply
                            0
                            • M Mogaambo

                              BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen); dude you haven't added +2 bytes, it is crashing as that of mine, but when i added +2 it is running fine, so whenever i want allocate BSTR do i have 2 add + bytes. BSTR tmp = ::SysAllocStringLen(NULL,destLen+srcLen+2);

                              “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
                              #25

                              I made a silly error in my code, now fixed (please see again the posted snippet). It should be OK now, the following test runs fine:

                              BSTR dest = SysAllocString(L"Hi ");
                              BSTR s1,s2,s3,s4;
                              s1 = SysAllocString(L"People, ");
                              s2 = SysAllocString(L"How do ");
                              s3 = SysAllocString(L"you ");
                              s4 = SysAllocString(L"do?");
                              HRESULT hr;
                              hr = StringAppend(&dest, s1);
                              hr = StringAppend(&dest, s2);
                              hr = StringAppend(&dest, s3);
                              hr = StringAppend(&dest, s4);

                              MessageBoxW(dest, L"Info");

                              SysFreeString(s1);
                              SysFreeString(s2);
                              SysFreeString(s3);
                              SysFreeString(s4);
                              SysFreeString(dest);

                              :)

                              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

                                I made a silly error in my code, now fixed (please see again the posted snippet). It should be OK now, the following test runs fine:

                                BSTR dest = SysAllocString(L"Hi ");
                                BSTR s1,s2,s3,s4;
                                s1 = SysAllocString(L"People, ");
                                s2 = SysAllocString(L"How do ");
                                s3 = SysAllocString(L"you ");
                                s4 = SysAllocString(L"do?");
                                HRESULT hr;
                                hr = StringAppend(&dest, s1);
                                hr = StringAppend(&dest, s2);
                                hr = StringAppend(&dest, s3);
                                hr = StringAppend(&dest, s4);

                                MessageBoxW(dest, L"Info");

                                SysFreeString(s1);
                                SysFreeString(s2);
                                SysFreeString(s3);
                                SysFreeString(s4);
                                SysFreeString(dest);

                                :)

                                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
                                #26

                                If you run your code in debug mode , it crashes at line where you freeing the variable, but runs in release mode.

                                “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

                                  If you run your code in debug mode , it crashes at line where you freeing the variable, but runs in release mode.

                                  “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
                                  #27

                                  My test works fine in DEBUG mode on my machine. What inputs are you providing? :)

                                  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