Solved

Howto fill a variant structure

Posted on 1998-10-27
2
333 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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