Solved

Howto fill a variant structure

Posted on 1998-10-27
2
332 Views
Last Modified: 2013-11-20
I have a function that use a variant for parameter. I need to fill it with a buffer of char. How do I create, fill and destroy correcly this type of structure ?
I never used variant structure, so please be precise.
Thank you very much
0
Comment
Question by:kharandir
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
Norbert earned 50 total points
ID: 1323886
Answer is comming
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1323887
You are using MFC so the simplest way to use variants is to use
COleVariant
You say You will fill it with a buffer of char.
Is it a string or is it simple an array of byte data
if it is a string I would recommend to use - it is the simplest-
CString Str=YourBuffer;
COleVariant OleVar=Str.AllocSysString();
The class COlevariant has an assignment operator that does all for you
if it is an array you can use the class COleArray or directly fill the variant
For using COleArray have a look to the online help
Direct filling the Variant do the following:
VARIANT Var;
VariantInit(Var)
      
SAFEARRAY* SafeArray=NULL;
SAFEARRAYBOUND ArrayBoundaries[1]; //one dimensional array
ArrayBoundaries[0].lLbound = 0; /* First index of limits array is always 0 */
ArrayBoundaries[0].cElements=SizeOfYourArray;
SafeArray=SafeArrayCreate(VT_UI1,1,ArrayBoundaries)
if(SafeArray==NULL)
     ErrorHandling
else
{
     char* ArrayData=NULL;
     HRESULT hr=SafeArrayAccessData(SafeArray,(void**)&ArrayData);
     if(FAILED(hr)
       ErrorHandling
     else
     {
         memcpy(ArrayData,YourArray,YourArraySize);
         SafeArrayUnaccessData(SafeArray);
         Var.vt=VT_ARRAY|VT_UI1
         Var.parray=SafeArray;
       }
}
now the variant is filled and can be passed.
To clear the variant:
If you use COleVariant or COleArray it is cleared automatically
if the COleVariant/COleArray variable goes out of scope.
If you use  the Variant directly
simple call VariantClear(&Var).
Attention Note:
If Using something like
VARIANT Func()
{
       VARIANT Var;
       VariantInit(&Var);
       SAFEARRAY* SafeArray=NULL;
       SAFEARRAYBOUND ArrayBoundaries[1]; //one dimensional array
       ArrayBoundaries[0].lLbound = 0; /* First index of limits array is always 0 */
      ArrayBoundaries[0].cElements=SizeOfYourArray;
      SafeArray=SafeArrayCreate(VT_UI1,1,ArrayBoundaries)
      if(SafeArray==NULL)
           ErrorHandling
      else
      {
          char* ArrayData=NULL;
          HRESULT hr=SafeArrayAccessData(SafeArray,(void**)&ArrayData);
           if(FAILED(hr)
             ErrorHandling
           else
          {
               memcpy(ArrayData,YourArray,YourArraySize);
               SafeArrayUnaccessData(SafeArray);
               Var.vt=VT_ARRAY|VT_UI1
                Var.parray=SafeArray;
          }
       }
       return Var;
}
It is nice to use it like
COleVariant OleVar=Func();
_BUT_ this will produce a memory leak because
COleVariant has an assignmen operator that copies the array
and so the array created inside Func is lost.
in the case you want to use the functionality of COleVariant beter use
     COleVariant *OleVar=(COleVariant*)&(Func());
     and after done
     OleVar->Clear();
To access the array inside the variant
first check the datatype inside the variant:
      switch(Var.vt)
      {
       case VT_ARRAY|VT_I1:
              DoYorArrayHandlingStuff using SafeArrayAccessData(Var.parray,(void**)&YourPointerToData)
               Do with *pointer what ever you want
               SafeArrayUnaccessData(Var.parray);
        CASE other supported VT_TYPES
        default:
              Handle error for unsupported types
       
     }

I hope now something about variants is more clear.
Norbert
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now