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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. BSTR in C++

BSTR in C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comhelp
13 Posts 5 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.
  • A Axonn Echysttas

    Hi everybody. I got a tiny question about a strange problem I'm having when I'm trying to receive a string from Visual Basic into C++. I got a VB string saying "Arial". But when I'm receiving it in C++, it becomes "Ail". Probably some wide characters versus normal characters problem, as i get 1 character and the next not, and then the next, I get. In order to make the conversion I'm using these two functions. //Finds out the length of a BSTR. long GetLengthOfBSTR (BSTR Target) { long iRet = 0; //Result to return. long iCursor = 0; //Interator through the BSTR. bool bForever = false; //To cycle forever. //If the string is NULL. if (Target == NULL) return(0); //Returning zero length. while (!bForever) //Cycling forever. { unsigned int uiPiece = Target[iCursor]; //Piece by piece. //Until reaching the null termination of the string. if (uiPiece == 0) return(iRet); //Returning the computed length. else iRet++; //Increasing the length. iCursor++; //Increasing the BSTR iterator. } return -1; //Mostly to avoid warnings ::- ). } //Converts a BSTR to a Char. Needed when working with strings with Visual Basic. char *ConvertBSTRToChar (BSTR Target) { char *sResult; //Result to return. long iCounter; //Iterator. long iLength; //Length of Target. iCounter = iLength = GetLengthOfBSTR(Target); //Initializing. sResult = new char[iLength + 1]; //Creating a character big enough to hold the result. strcpy(sResult, ""); //Initializing. for (iCounter = 0; iCounter < iLength; iCounter++) //Going through the Target BSTR. { unsigned int uiPiece = Target[iCounter]; //Taking it piece by piece. sResult[iCounter] = uiPiece; //Adding it in the final result. sResult[iCounter + 1] = '\0'; //Adding null termination. } return sResult; //Returning computed char. } And then I do: LPCSTR c; c = ConvertBSTRToChar(StuffFromVB); MessageBox(0, c, "Test", 0); SO? Anybody here to enlighten me a bit about this? : ). Thank you very much for your time here anyway!

    -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

    A Offline
    A Offline
    Arman S
    wrote on last edited by
    #3

    Your functions are wrong. Note that a bstr is not required to be null terminated. If you want to get the size of a BSTR, use for example wcslen. To convert between BSTR and C-style strings, use W2A, OLE2A macros from <AtlBase.h>. Also see CComBSTR class.

    -- ===== Arman

    A CPalliniC L 3 Replies Last reply
    0
    • A Arman S

      Your functions are wrong. Note that a bstr is not required to be null terminated. If you want to get the size of a BSTR, use for example wcslen. To convert between BSTR and C-style strings, use W2A, OLE2A macros from <AtlBase.h>. Also see CComBSTR class.

      -- ===== Arman

      A Offline
      A Offline
      Axonn Echysttas
      wrote on last edited by
      #4

      I replaced with wcslen and still same problem. I would prefer to understand how to convert it corectly, manually. That way I don't have to depend on more links and stuff that can break from one windows release to the other : ).

      -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

      CPalliniC 1 Reply Last reply
      0
      • N Nibu babu thomas

        Why do you have to roll out convertion functions for BSTR's on your own. We already have ConvertStringToBSTR and ConvertBSTRToString. Why not use them. Link to comsupp.lib for using them.


        Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com

        A Offline
        A Offline
        Axonn Echysttas
        wrote on last edited by
        #5

        LPCSTR d = _com_util::ConvertBSTRToString(FontInformation.Name); MessageBox(0, d, 0, 0); This still returns garbage data : ( ... what do you make of: "???r" instead of "Arial". And it's sure that it's a BSTR there. For example, my function works but doesn't process the BSTR correctly. It would be easier for me to fix my function ... if only I knew what's wrong : D.

        -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

        N 1 Reply Last reply
        0
        • A Arman S

          Your functions are wrong. Note that a bstr is not required to be null terminated. If you want to get the size of a BSTR, use for example wcslen. To convert between BSTR and C-style strings, use W2A, OLE2A macros from <AtlBase.h>. Also see CComBSTR class.

          -- ===== Arman

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

          Arman Z. Sahakyan wrote:

          Note that a bstr is not required to be null terminated

          Actually the above is not true, see, for instance: http://www.ecs.syr.edu/faculty/fawcett/handouts/CSE775/Presentations/BruceMcKinneyPapers/COMstrings.htm[^] Anyway I agree on the overall argument, since BSTR may contain NULL inside too. :)

          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.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • A Axonn Echysttas

            LPCSTR d = _com_util::ConvertBSTRToString(FontInformation.Name); MessageBox(0, d, 0, 0); This still returns garbage data : ( ... what do you make of: "???r" instead of "Arial". And it's sure that it's a BSTR there. For example, my function works but doesn't process the BSTR correctly. It would be easier for me to fix my function ... if only I knew what's wrong : D.

            -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

            N Offline
            N Offline
            Nibu babu thomas
            wrote on last edited by
            #7

            Axonn Echysttas wrote:

            This still returns garbage data : ( ... what do you make of: "???r" instead of "Arial". And it's sure that it's a BSTR there. For example, my function works but doesn't process the BSTR correctly. It would be easier for me to fix my function ... if only I knew what's wrong : D.

            Watch memory for this variable to see if the contents is in the variable as you expect it to be.


            Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com

            1 Reply Last reply
            0
            • A Axonn Echysttas

              I replaced with wcslen and still same problem. I would prefer to understand how to convert it corectly, manually. That way I don't have to depend on more links and stuff that can break from one windows release to the other : ).

              -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

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

              Axonn Echysttas wrote:

              I would prefer to understand how to convert it corectly, manually

              Hence you have to be careful. Have a look at this http://www.ecs.syr.edu/faculty/fawcett/handouts/CSE775/Presentations/BruceMcKinneyPapers/COMstrings.htm[^]. :)

              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.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • A Arman S

                Your functions are wrong. Note that a bstr is not required to be null terminated. If you want to get the size of a BSTR, use for example wcslen. To convert between BSTR and C-style strings, use W2A, OLE2A macros from <AtlBase.h>. Also see CComBSTR class.

                -- ===== Arman

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

                Arman Z. Sahakyan wrote:

                Note that a bstr is not required to be null terminated.

                Actually BSTR is terminated by double byte NULL. \0\0 Best Wishes, -David Delaune

                1 Reply Last reply
                0
                • A Axonn Echysttas

                  Hi everybody. I got a tiny question about a strange problem I'm having when I'm trying to receive a string from Visual Basic into C++. I got a VB string saying "Arial". But when I'm receiving it in C++, it becomes "Ail". Probably some wide characters versus normal characters problem, as i get 1 character and the next not, and then the next, I get. In order to make the conversion I'm using these two functions. //Finds out the length of a BSTR. long GetLengthOfBSTR (BSTR Target) { long iRet = 0; //Result to return. long iCursor = 0; //Interator through the BSTR. bool bForever = false; //To cycle forever. //If the string is NULL. if (Target == NULL) return(0); //Returning zero length. while (!bForever) //Cycling forever. { unsigned int uiPiece = Target[iCursor]; //Piece by piece. //Until reaching the null termination of the string. if (uiPiece == 0) return(iRet); //Returning the computed length. else iRet++; //Increasing the length. iCursor++; //Increasing the BSTR iterator. } return -1; //Mostly to avoid warnings ::- ). } //Converts a BSTR to a Char. Needed when working with strings with Visual Basic. char *ConvertBSTRToChar (BSTR Target) { char *sResult; //Result to return. long iCounter; //Iterator. long iLength; //Length of Target. iCounter = iLength = GetLengthOfBSTR(Target); //Initializing. sResult = new char[iLength + 1]; //Creating a character big enough to hold the result. strcpy(sResult, ""); //Initializing. for (iCounter = 0; iCounter < iLength; iCounter++) //Going through the Target BSTR. { unsigned int uiPiece = Target[iCounter]; //Taking it piece by piece. sResult[iCounter] = uiPiece; //Adding it in the final result. sResult[iCounter + 1] = '\0'; //Adding null termination. } return sResult; //Returning computed char. } And then I do: LPCSTR c; c = ConvertBSTRToChar(StuffFromVB); MessageBox(0, c, "Test", 0); SO? Anybody here to enlighten me a bit about this? : ). Thank you very much for your time here anyway!

                  -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

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

                  Hi, Try using: template BSTR ConvertToBSTR(T a_Str) { return(_bstr_t(a_Str).copy()); } It's the same function I posted in your article two years ago. :) Best Wishes, -Randor (David Delaune)

                  A 1 Reply Last reply
                  0
                  • L Lost User

                    Hi, Try using: template BSTR ConvertToBSTR(T a_Str) { return(_bstr_t(a_Str).copy()); } It's the same function I posted in your article two years ago. :) Best Wishes, -Randor (David Delaune)

                    A Offline
                    A Offline
                    Axonn Echysttas
                    wrote on last edited by
                    #11

                    Hhahahaah, Hi : D. That should be a lesson to me huh? To better remember stuff. Hm, however, this is to return a BSTR, I need the other way around. A Char from a BSTR. : D.

                    -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

                    L 1 Reply Last reply
                    0
                    • A Axonn Echysttas

                      Hhahahaah, Hi : D. That should be a lesson to me huh? To better remember stuff. Hm, however, this is to return a BSTR, I need the other way around. A Char from a BSTR. : D.

                      -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

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

                      Hi again, The _bstr_t has a built-in char* operator. So this means you can just do something like this: BSTR test; char buffer[SOME_LENTH]; _bstr_t bstr(test,true); strcpy(buffer,(char*)bstr); The above code does not check buffer length. A simple new/delete using the BSTR length should take care of that. :) Best Wishes, Randor (David Delaune)

                      A 1 Reply Last reply
                      0
                      • L Lost User

                        Hi again, The _bstr_t has a built-in char* operator. So this means you can just do something like this: BSTR test; char buffer[SOME_LENTH]; _bstr_t bstr(test,true); strcpy(buffer,(char*)bstr); The above code does not check buffer length. A simple new/delete using the BSTR length should take care of that. :) Best Wishes, Randor (David Delaune)

                        A Offline
                        A Offline
                        Axonn Echysttas
                        wrote on last edited by
                        #13

                        What's very interesting is that I managed to solve everything just like this: LPCSTR a = (LPCSTR)myBSTR; MessageBox(0, a, 0, 0); //-> it works, if you can believe it!!! Probably because I send only non-Unicode stuff from VB. Anyway, this is how it's gonna stay for a while and I don't intend to ever switch this program to Unicode. Works just fine like it is.

                        -= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^

                        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