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. Chump Is Stumped By VARIANT Problem

Chump Is Stumped By VARIANT Problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomdata-structures
7 Posts 3 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.
  • E Offline
    E Offline
    Eurosid
    wrote on last edited by
    #1

    So I have this COM object that's packing up an array floats for me. Here's the loop where the VARIANT* array is getting stuffed: for (i = 0; i < cElements; i++) {      VariantInit(&vDataValues[i]);      V_VT(&vDataValues[i]) = VT_R4;            //Works ok UNLESS float_data[i] == 0.0;      V_R4(&vDataValues[i]) = float_data[i];      if (!V_R4(&vDataValues[i]))      {           *pErrorCode = 1;           m_strMessage.Format("V_R4 failed!");           hr = E_FAIL;           goto Error;      } } The VT_R4() operation works unless the element in float_data happens to be 0.0. When that happens, VT_R4 fails. I know it fails only with 0.0, because I tried this: if( float_data[i] != 0.0 )      V_R4(&vDataValues[i]) = float_data[i]; else      V_R4(&vDataValues[i]) = float_data[i] + 0.1; Then, it never fails. I can't figure out why.

    L I 2 Replies Last reply
    0
    • E Eurosid

      So I have this COM object that's packing up an array floats for me. Here's the loop where the VARIANT* array is getting stuffed: for (i = 0; i < cElements; i++) {      VariantInit(&vDataValues[i]);      V_VT(&vDataValues[i]) = VT_R4;            //Works ok UNLESS float_data[i] == 0.0;      V_R4(&vDataValues[i]) = float_data[i];      if (!V_R4(&vDataValues[i]))      {           *pErrorCode = 1;           m_strMessage.Format("V_R4 failed!");           hr = E_FAIL;           goto Error;      } } The VT_R4() operation works unless the element in float_data happens to be 0.0. When that happens, VT_R4 fails. I know it fails only with 0.0, because I tried this: if( float_data[i] != 0.0 )      V_R4(&vDataValues[i]) = float_data[i]; else      V_R4(&vDataValues[i]) = float_data[i] + 0.1; Then, it never fails. I can't figure out why.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Well I have no idea what your float_data is or if it has been properly initialized but this works without error

      VARIANT DataValues[4];
          V_R4( &DataValues[0]) = 0.0;

      led mike

      E 1 Reply Last reply
      0
      • L led mike

        Well I have no idea what your float_data is or if it has been properly initialized but this works without error

        VARIANT DataValues[4];
            V_R4( &DataValues[0]) = 0.0;

        led mike

        E Offline
        E Offline
        Eurosid
        wrote on last edited by
        #3

        I'm new to using COM, so maybe I'm missing something obvious here. The data I can verify is OK.

        led mike wrote:

        VARIANT DataValues[4];
        V_R4( &DataValues[0]) = 0.0;

        Those lines work for me, too. The examples I'm looking show that you verify it's OK by doing this sort of thing: if (!V_R4(&vDataValues[i])) { //Do some error stuff } And !V_R4(&vDataValues[i]) evaluates to false if the data is 0.0. Why is that?:confused: If I comment out that error check, everything seems to work fine. Is it not needed?

        L 1 Reply Last reply
        0
        • E Eurosid

          I'm new to using COM, so maybe I'm missing something obvious here. The data I can verify is OK.

          led mike wrote:

          VARIANT DataValues[4];
          V_R4( &DataValues[0]) = 0.0;

          Those lines work for me, too. The examples I'm looking show that you verify it's OK by doing this sort of thing: if (!V_R4(&vDataValues[i])) { //Do some error stuff } And !V_R4(&vDataValues[i]) evaluates to false if the data is 0.0. Why is that?:confused: If I comment out that error check, everything seems to work fine. Is it not needed?

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Eurosid wrote:

          The examples I'm looking show that you verify it's OK by doing this sort of thing:

          I don't think that's correct. Where are you getting these examples? V_R4 is just a macro that evaluates to the specified VARIANT member during pre-compile. It does not return an status indication value.

          led mike

          E 1 Reply Last reply
          0
          • E Eurosid

            So I have this COM object that's packing up an array floats for me. Here's the loop where the VARIANT* array is getting stuffed: for (i = 0; i < cElements; i++) {      VariantInit(&vDataValues[i]);      V_VT(&vDataValues[i]) = VT_R4;            //Works ok UNLESS float_data[i] == 0.0;      V_R4(&vDataValues[i]) = float_data[i];      if (!V_R4(&vDataValues[i]))      {           *pErrorCode = 1;           m_strMessage.Format("V_R4 failed!");           hr = E_FAIL;           goto Error;      } } The VT_R4() operation works unless the element in float_data happens to be 0.0. When that happens, VT_R4 fails. I know it fails only with 0.0, because I tried this: if( float_data[i] != 0.0 )      V_R4(&vDataValues[i]) = float_data[i]; else      V_R4(&vDataValues[i]) = float_data[i] + 0.1; Then, it never fails. I can't figure out why.

            I Offline
            I Offline
            Iain Clarke Warrior Programmer
            wrote on last edited by
            #5

            I'm slightly curious what you mean by "fails". Do you put the variant in a watch window, and see wrong numbers? From your later post, I think you think that V_R4 is a function which passes success / failure. Looking in OLEAUTO.H,

            #define V_UNION(X, Y) ((X)->Y)
            #define V_VT(X) ((X)->vt)
            #define V_R4(X) V_UNION(X, fltVal)

            So...

            VARIANT var;
            VariantInit (&var);
            V_VT(&var) = VT_R4;
            V_R4(&var) = 0.0;

            becomes

            V_UNION(&var,ftlVal) = 0.0;

            becomes

            (&var)->ftlVal = 0.0;

            which looks fine to me. BUT: You are using this in an if statement. Imagine the following:

            if (1)
            {
            // pretty guaranteed to get here
            } else {
            // not likely...
            }

            and

            if (a=0)
            {
            // pretty unlikely to get here
            } else {
            //
            }

            The second if evaluates to 0, which will fail the if. Cutting to the chase:

            V_R4(&blah) = a number

            evaluates to whatever the number is - and 0 is FALSE. Here endeth the lesson. Iain.

            Iain Clarke appears because CPallini still cares.

            E 1 Reply Last reply
            0
            • L led mike

              Eurosid wrote:

              The examples I'm looking show that you verify it's OK by doing this sort of thing:

              I don't think that's correct. Where are you getting these examples? V_R4 is just a macro that evaluates to the specified VARIANT member during pre-compile. It does not return an status indication value.

              led mike

              E Offline
              E Offline
              Eurosid
              wrote on last edited by
              #6

              :doh: :doh: :doh: You are exactly correct. I misunderstood what I was looking at. The example was for packing up a BSTR, so they did such a check to make sure the pointer wasn't NULL. Doing the same thing on a float isn't needed, but will look OK unless the value happens to be 0, which will look like FALSE. I told you I was a Chump! Thank you for help!

              1 Reply Last reply
              0
              • I Iain Clarke Warrior Programmer

                I'm slightly curious what you mean by "fails". Do you put the variant in a watch window, and see wrong numbers? From your later post, I think you think that V_R4 is a function which passes success / failure. Looking in OLEAUTO.H,

                #define V_UNION(X, Y) ((X)->Y)
                #define V_VT(X) ((X)->vt)
                #define V_R4(X) V_UNION(X, fltVal)

                So...

                VARIANT var;
                VariantInit (&var);
                V_VT(&var) = VT_R4;
                V_R4(&var) = 0.0;

                becomes

                V_UNION(&var,ftlVal) = 0.0;

                becomes

                (&var)->ftlVal = 0.0;

                which looks fine to me. BUT: You are using this in an if statement. Imagine the following:

                if (1)
                {
                // pretty guaranteed to get here
                } else {
                // not likely...
                }

                and

                if (a=0)
                {
                // pretty unlikely to get here
                } else {
                //
                }

                The second if evaluates to 0, which will fail the if. Cutting to the chase:

                V_R4(&blah) = a number

                evaluates to whatever the number is - and 0 is FALSE. Here endeth the lesson. Iain.

                Iain Clarke appears because CPallini still cares.

                E Offline
                E Offline
                Eurosid
                wrote on last edited by
                #7

                Iain Clarke wrote:

                Here endeth the lesson. Iain.

                Yes. That was it. I was being a Chump. I knew it, but I just couldn't see why. :-O Thanks!

                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