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.
  • 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