Link to home
Start Free TrialLog in
Avatar of barrett
barrett

asked on

sizeof VARIANT data in C++

Is there a convenient way to get the size of the encapsulated data in a VARIANT? I need a general method that works for all the types: VT_BSTR, VT_I4, etc...
Avatar of rayb
rayb

Perhaps another expert will offer another opinion, but I believe MS left this type of function out of the VariantXXX methods because the OS is responsible for the memory of the various members...  By that I mean, to allocate a bstr, your supposed to use the SysAllocString, to allocate a safe array, you must use the SafeArrayCreate and so on.

You may get a break and have somebody know of an existing method for doing this.
Avatar of barrett

ASKER

This sounds pretty plausible, unfortunately.
ASKER CERTIFIED SOLUTION
Avatar of AlexVirochovsky
AlexVirochovsky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of barrett

ASKER

Ok, let me see if I understand - essentially I'll need to write a method that determines the type from VARTYPE vt, then explicitly calls sizeof for the appropriate type. I've already done this, and it looks something like:

int SizeofVariantData( const _variant_t&  var )
{
    switch( var.vt )
    {
      case VT_EMPTY:
      {
            return 0;
            break;
      }
      case VT_I2:
      {
            return( sizeof( V_I2( &var ) ) );
            break;
      }
      case VT_I4:
      {
            return( sizeof( V_I4( &var ) ) );
            break;
      }
      case VT_R4:
      {
            return( sizeof( (double)V_R4( &var ) ) );
            break;
      }
      case VT_R8:
      {
            return( sizeof( V_R8(&var) ) );
            break;
      }
      case VT_CY:
      {
            return( sizeof( double ) );
            break;
      }
      case VT_DATE:
      {
            return 8;
            break;
      }
      case VT_BSTR:
      {
            return( _bstr_t( V_BSTR( &var ) ).length() );
            break;
      }
      case VT_BOOL:
      {
            return( sizeof( V_BOOL( &var ) ) );
            break;
      }
      case VT_UI1:
      {
            return( sizeof( (unsigned short)V_UI1( &var ) ) );
            break;
      }
      case VT_DECIMAL:
      {
            return( sizeof( double ) );
            break;
      }
      default:
      {
            ATLTRACE( _T("TYPE NOT RECOGNIZED") );
            ATLASSERT( FALSE );
            return -1;
      }
    }
}

I really wish I could find a more general approach - this relies on my knowledge of data type sizes, which I don't necessarily have.

Or am I way off?

1. sorry, i don't read you Q attentionally, and lost
word "encapsulated data".
2. you solution is good: in some places can write shorter:
case VT_ERROR: return sizeof(SCODE); but it is not
  important.
3. No in C++(or i don't know) general metod to find
  sizeof of struct's member in such case.
  I test in all my books and find nothing!
Best regards, Alex

Avatar of barrett

ASKER

OK, thanks for all the effort!