Solved

Howto fill a variant structure

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.

839 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