Solved

Howto fill a variant structure

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

20 Experts available now in Live!

Get 1:1 Help Now