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. Length of VARIANT V_BSTR

Length of VARIANT V_BSTR

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomtestingtools
9 Posts 2 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 Offline
    K Offline
    Kreatief
    wrote on last edited by
    #1

    Hi, I am dealing with OLE automation. Now: I have to convert a string to V_BSTR to use it with OLE. This string is really long. About 500 chars and more! If I convert it to V_BSTR, then it cuts after some hundred bytes (about 230). How can I avoid it? I use SysAllocStringLen to convert it, with this, normally, it should be allocated enough space for it. But it seems it doesnt care! VARIANT v1; V_VT(&v1) = VT_BSTR; V_BSTR(&v1) = SysAllocStringLen(strToWc(selStr), 1500); strToWc() is a method by me, which converts a string to widechar. This one works. I checked, and the string is complete! DKT

    S 1 Reply Last reply
    0
    • K Kreatief

      Hi, I am dealing with OLE automation. Now: I have to convert a string to V_BSTR to use it with OLE. This string is really long. About 500 chars and more! If I convert it to V_BSTR, then it cuts after some hundred bytes (about 230). How can I avoid it? I use SysAllocStringLen to convert it, with this, normally, it should be allocated enough space for it. But it seems it doesnt care! VARIANT v1; V_VT(&v1) = VT_BSTR; V_BSTR(&v1) = SysAllocStringLen(strToWc(selStr), 1500); strToWc() is a method by me, which converts a string to widechar. This one works. I checked, and the string is complete! DKT

      S Offline
      S Offline
      Steen Krogsgaard
      wrote on last edited by
      #2

      If my memory serves me right then the "B" in BSTR stands for Byte - the byte that is used to hold the length of the string. This is the Pascal-type string representation. As a consequence, a BSTR cannot hold more then 255 chars. No way around it. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

      K 1 Reply Last reply
      0
      • S Steen Krogsgaard

        If my memory serves me right then the "B" in BSTR stands for Byte - the byte that is used to hold the length of the string. This is the Pascal-type string representation. As a consequence, a BSTR cannot hold more then 255 chars. No way around it. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

        K Offline
        K Offline
        Kreatief
        wrote on last edited by
        #3

        Hmmm thats strange... If I use a direct string: V_BSTR(&v) = SysAllocString(OLESTR("blabla")); then, it works. Even if the "blabla" string is more than 500 chars long! If I use it the way I mentioned before, then it behaves strange: The first time it cuts some bytes off, the second time and so on, it works! If I put the same command twice, then it never works... It seems as if the memory management is totally shit! DKT

        S 1 Reply Last reply
        0
        • K Kreatief

          Hmmm thats strange... If I use a direct string: V_BSTR(&v) = SysAllocString(OLESTR("blabla")); then, it works. Even if the "blabla" string is more than 500 chars long! If I use it the way I mentioned before, then it behaves strange: The first time it cuts some bytes off, the second time and so on, it works! If I put the same command twice, then it never works... It seems as if the memory management is totally shit! DKT

          S Offline
          S Offline
          Steen Krogsgaard
          wrote on last edited by
          #4

          I'm sorry, my memory certainly didn't serve me well. I think I confused it with the ANSI version of BSTR (I think it's called BSTRT). Just to be sure, you say your function converts it to a widechar, you mean unicode, right? A BSTR is a pointer to a location, where the first four bytes are the length part and the rest is the unicode string terminated by a double-zero. Could this in any way be the cause of your problem (e.g. a premature terminating double-zero?) Otherwise, I don't have any good ideas. Perhaps you could post more code? Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

          K 1 Reply Last reply
          0
          • S Steen Krogsgaard

            I'm sorry, my memory certainly didn't serve me well. I think I confused it with the ANSI version of BSTR (I think it's called BSTRT). Just to be sure, you say your function converts it to a widechar, you mean unicode, right? A BSTR is a pointer to a location, where the first four bytes are the length part and the rest is the unicode string terminated by a double-zero. Could this in any way be the cause of your problem (e.g. a premature terminating double-zero?) Otherwise, I don't have any good ideas. Perhaps you could post more code? Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

            K Offline
            K Offline
            Kreatief
            wrote on last edited by
            #5

            I checked the double zero, but it didnt make it better... Here some code: Function to convert String to widechar: OLECHAR* ViaExcelConnector::strToWc(const string &cnvrtData) const { OLECHAR cnvrt[500]; int i = 0; char cnvrtChr[500]; for(i=0; i

            S 2 Replies Last reply
            0
            • K Kreatief

              I checked the double zero, but it didnt make it better... Here some code: Function to convert String to widechar: OLECHAR* ViaExcelConnector::strToWc(const string &cnvrtData) const { OLECHAR cnvrt[500]; int i = 0; char cnvrtChr[500]; for(i=0; i

              S Offline
              S Offline
              Steen Krogsgaard
              wrote on last edited by
              #6

              Your code got f***ed up because you didn't check the "do not treat <'s as HTML tags" box. Anyway, you only allocate 500 chars to do the conversion, so it's no wonder that it wont' work for more than 500 chars. And you have a buffer overrun when cnvrtData is more than 500 chars since you don't check for max 500 chars in your for loop. There's no saying what will happen, but you will definitely get your memory screwed up. Furhtermore, you return a pointer to cnvrt which is a stack variable that will go out of scope (and be overwritten) when the function returns. Why do you move the content of cnvrtData into cnvrtChr? Can't you do the LPCSTR cast directly on cnvrtData? Besides, you should call MultiByteToWideChar with cchWideChar set to zero firt to get the length of the widechar, then allocate it and then convert it: int iLength = MultiByteToWideChar(CP_ACP,0,(LPCSTR)cnvrtData,-1,NULL,0); OLECHAR* cnvrt = new OLECHAR[iLength]; // perhaps iLength+1; MSDN is not clear on wether the terminating NULL is included in the length returned by MultiByteToWideChar MultiByteToWideChar(CP_ACP,0,(LPCSTR)cnvrtData,-1,cnvrt,iLength; // or iLength+1 return cnvrt but then you will have to remember to delete cnvrt (the return value from strToWc) with delete[] or you'll leak memory. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

              1 Reply Last reply
              0
              • K Kreatief

                I checked the double zero, but it didnt make it better... Here some code: Function to convert String to widechar: OLECHAR* ViaExcelConnector::strToWc(const string &cnvrtData) const { OLECHAR cnvrt[500]; int i = 0; char cnvrtChr[500]; for(i=0; i

                S Offline
                S Offline
                Steen Krogsgaard
                wrote on last edited by
                #7

                Oh, I forgot, you should check that the return value from MultiByteToWideChar(....,NULL,0)is non-zero. If it is zero, call GetLastError to get info on why it failed. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                K 1 Reply Last reply
                0
                • S Steen Krogsgaard

                  Oh, I forgot, you should check that the return value from MultiByteToWideChar(....,NULL,0)is non-zero. If it is zero, call GetLastError to get info on why it failed. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                  K Offline
                  K Offline
                  Kreatief
                  wrote on last edited by
                  #8

                  Oh perfect. It works now! BUT: While testing, I used a string that was 442 chars long, so not longer than those 500! And no, I couldnt easily convert the string to LPCSTR, cause the string is an own written class (not by me), but I found out that there is a c_str method in this class. And now, it works! Amazing. Thanks so much, this was a very clear answer, and I can learn alot from it, on how to code better! DKT

                  S 1 Reply Last reply
                  0
                  • K Kreatief

                    Oh perfect. It works now! BUT: While testing, I used a string that was 442 chars long, so not longer than those 500! And no, I couldnt easily convert the string to LPCSTR, cause the string is an own written class (not by me), but I found out that there is a c_str method in this class. And now, it works! Amazing. Thanks so much, this was a very clear answer, and I can learn alot from it, on how to code better! DKT

                    S Offline
                    S Offline
                    Steen Krogsgaard
                    wrote on last edited by
                    #9

                    Glad to help. Cheers Steen. "To claim that computer games influence children is ridiculous. If Pacman had influenced children born in the 80'ies we would see a lot of youngsters running around in dark rooms eating pills while listening to monotonous music"

                    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