Solved

Howto fill a variant structure

Posted on 1998-10-27
2
337 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
[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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

738 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