LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Need help on passing reference VARIANT or void * type

I would like to retrieve unknown type array from my function, I did read about VARIANT help (and Any Array type in page 2-7 of LabWindows/CVI Instrument Driver Developers Guide) ... but could not do any thing about this matter. Here how I would like to have:
 
I call a function with passing a reference variable (might be a VARIANT type, or void *?), I expect there is an array wiil be retunned as reference, that array will contain char*, int, double, short ... values that I do not know in-advance
 
How I create this function? And can I use library library ActiveX, Variant Related functions to retrieve their proper types?
 
Many thanks for any help or guidances
0 Kudos
Message 1 of 6
(4,817 Views)

I can see two ways to develop your function:

  1. Use void * type adding a field that describes data type passed
  2. Use a variant relying on this datatype intrinsic ability to show associated data type

In the first case you can take as an example ConvertArrayType function in AdvancedAnalisys Library, whose definition is as follows:

void            CVIFUNC ConvertArrayType(const void *sourceArray, int sourceDataType, void *targetArray,
                                        int targetDataType, int numberOfPoints);

As you can see, a void pointer is passed to the function together with a data type field that describes which type is passed array. The function can then properly handle the array passed to it.

In the second case, CA_VariantGetType () function can be used to retrieve the data type associated to the variant and correctly retrieve data in a properly allocated array. A sample on using this function can be found in DataTypesServer and DataTypesClient samples located in <cvidir>\samples\activex\servers\DataTypes folder.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 6
(4,813 Views)

Hi Roberto,

I tried the 2nd option & it worked, however I need a bit more help on the retrive information: Here my codes:

#include <cviauto.h>

void GetVariantValues (VARIANT *var_ptr)
{

       unsigned int  i_val = 12;
       double db_val = 34.56;
       char *str = "ABC";
 
      CA_VariantSetInt (var_ptr, i_val);
      var_ptr++;
      CA_VariantSetDouble (var_ptr, db_val);
       var_ptr++;
       CA_VariantSetCString (var_ptr, "ABC");
}

int main (int argc, char *argv[])
{
       VARIANT     var_ptr[3];
 
       int i_type;
 
       if (InitCVIRTE (0, argv, 0) == 0)
       return -1;    /* out of memory */
 

       // Just for clarify the CAVT type numbers
       i_type =  CAVT_UINT;      // = 103
       i_type =  CAVT_DOUBLE;   // = 105
       i_type =  CAVT_CSTRING;   // = 115
 
       GetVariantValues (var_ptr);                                // var_ptr returns with 3 different types successfully
 
       i_type = CA_VariantGetType (var_ptr);              // first value return is  i_type  = 103   (CAVT_UINT)


       var_ptr++;                                                           // **********   If add this line ---> ERROR COMPILE  *****************
 
 return 0;
}

I saw var_ptr return with all values I need to recieve but I can not retrieve the 2nd & 3rd values ... can you help here? I tried to increase the pointer or use var_ptr[1] or var_ptr[2] ---> compile error!

Many thanks

0 Kudos
Message 3 of 6
(4,786 Views)

Hi again,

I did find the way to increase the pointer as the following:

 i_type = CA_VariantGetType (var_ptr);
 i_type = CA_VariantGetType (var_ptr+1);
 i_type = CA_VariantGetType (var_ptr+2);

And it works, thanks for help Smiley Happy

 

0 Kudos
Message 4 of 6
(4,780 Views)

Yes, it's equivalent to the notation  &var_ptr[x] I am using in this sample code, that can be run in the interactive window:

#include <cviauto.h>
#include <utility.h>

static VARIANT     var_ptr[3];

static int i_type;
static unsigned int  i_val = 12;
static double db_val = 34.56;
static char *str = "ABC";

static int  xi, i, type[3];
static double xd;
static char  *msg;

// Just for clarify the CAVT type numbers
i_type =  CAVT_INT;      // = 103
i_type =  CAVT_DOUBLE;   // = 105
i_type =  CAVT_CSTRING;   // = 115

CA_VariantSetInt     (&var_ptr[0], i_val);
CA_VariantSetDouble  (&var_ptr[1], db_val);
CA_VariantSetCString (&var_ptr[2], "ABC");

msg = malloc (32); memset (msg, 0, 32);
DebugPrintf ("Received types\n");
for (i = 0; i < 3; i++) {
 type[i] = CA_VariantGetType (&var_ptr[i]);              // first value return is  i_type  = 103   (CAVT_UINT)
 switch (type[i]) {
  case CAVT_INT:
   CA_VariantGetInt (&var_ptr[i], &xi);
   DebugPrintf ("%d: Type %d - Value %d\n", i, type[i], xi);
   break;
  case CAVT_DOUBLE:
   CA_VariantGetDouble (&var_ptr[i], &xd);
   DebugPrintf ("%d: Type %d - Value %f\n", i, type[i], xd);
   break;
  case CAVT_CSTRING:
   CA_VariantGetCString (&var_ptr[i], &msg);
   DebugPrintf ("%d: Type %d - Value %s\n", i, type[i], msg);
   break;
 }
}

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 5 of 6
(4,777 Views)
I did very well this issue (VARIANT) , thanks again Smiley Happy
0 Kudos
Message 6 of 6
(4,755 Views)