Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sizeof VARIANT data in C++

Posted on 1999-07-29
6
Medium Priority
?
1,540 Views
Last Modified: 2013-12-03
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...
0
Comment
Question by:barrett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:rayb
ID: 1404124
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.
0
 

Author Comment

by:barrett
ID: 1404125
This sounds pretty plausible, unfortunately.
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 300 total points
ID: 1404126
Next it is VARIANT struct description and  you
can easy see , that sizeof(VARIANT) = 12b
But you can use function sizeof()!

 VARIANT and VARIANTARG

Use VARIANTARG to describe arguments passed within DISPPARAMS. Use VARIANT to specify variant data that can't be passed by reference; the VARIANT type can't have the VT_BYREF bit set. Note that VARIANTs can be passed by value even if VARIANTARGs cannot.

typedef struct FARSTRUCT tagVARIANT VARIANT;
typedef struct FARSTRUCT tagVARIANT VARIANTARG;

typedef struct tagVARIANT  {
      VARTYPE vt;      //unsigned short 2b
      unsigned short wReserved1;//2b
      unsigned short wReserved2;//2b
      unsigned short wReserved3;//2b
      union {                       //max 4b
            unsigned char      bVal;                  /* VT_UI1                        */
            short                  iVal;                  /* VT_I2                              */
            long                  lVal;                  /* VT_I4                              */
            float                  fltVal;                  /* VT_R4                              */
            double                  dblVal;                  /* VT_R8                              */

            VARIANT_BOOL            bool;                  /* VT_BOOL                        */
            SCODE                  scode;                  /* VT_ERROR                        */
            CY                        cyVal;                  /* VT_CY                              */
            DATE                  date;                  /* VT_DATE                        */
            BSTR                  bstrVal;                  /* VT_BSTR                        */
            Iunknown                  FAR* punkVal;      /* VT_UNKNOWN                  */
            Idispatch            FAR* pdispVal;      /* VT_DISPATCH                  */
            SAFEARRAY            FAR* parray;            /* VT_ARRAY|*                  */
            unsigned char      FAR *pbVal;            /* VT_BYREF|VT_UI1            */

            short                  FAR* piVal;            /* VT_BYREF|VT_I2            */
            long                  FAR* plVal;            /* VT_BYREF|VT_I4            */
            float                  FAR* pfltVal;      /* VT_BYREF|VT_R4            */
            double                  FAR* pdblVal;      /* VT_BYREF|VT_R8            */
            VARIANT_BOOL            FAR* pbool;            /* VT_BYREF|VT_BOOL            */
            SCODE                  FAR* pscode;            /* VT_BYREF|VT_ERROR            */
            CY                        FAR* pcyVal;            /* VT_BYREF|VT_CY            */
            DATE                  FAR* pdate;            /* VT_BYREF|VT_DATE            */
            BSTR                  FAR* pbstrVal;      /* VT_BYREF|VT_BSTR            */

            IUnknown FAR*      FAR* ppunkVal;      /* VT_BYREF|VT_UNKNOWN      */
            IDispatch FAR*      FAR* ppdispVal;      /* VT_BYREF|VT_DISPATCH      */
            SAFEARRAY FAR*      FAR* parray;            /* VT_ARRAY|*                  */
            VARIANT                  FAR* pvarVal;      /* VT_BYREF|VT_VARIANT      */
            void                  FAR* byref;            /* Generic ByRef                  */
      };
};
I hope, this helps. Alex

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:barrett
ID: 1404127
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?

0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1404128
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

0
 

Author Comment

by:barrett
ID: 1404129
OK, thanks for all the effort!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question