LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

passing variable number of arguments to function

He,
I would like to create a function like this:

//prototypes
void out_print(char * format,...);
void start(void);

//function with variable number of arguments
void out_print(char * format, ...)
{
char buffer[512];
Fmt(buffer,format, ...); //this doesn't work!!!
SetCtrlVal(panelHandle,STRING,buffer);
}

//some function calling out_print
void start(void)
{
int size;

//do some stuff
//get the size needed, for example 65000
size=65000;
out_print("file : %s, size: %i", "myfile.bin",size);

//do more stuff
}

How can I pass the variable number of arguments from the out_print function to the Fmt function? I have read it should be something with the macros va_list, va_start and va_end but I couldnt figure out how exactly to do it. A problem is also that the arguments of print_out may be int, double, char *, ... in any order.

Thanks for the help!

Kristrio
0 Kudos
Message 1 of 5
(4,190 Views)

Here some hint to design and use a function vith variable number of arguments (by means of va_xxx macros you already mentioned). I used this function to create a history of operation in a plant controller: depending on the type of event to record, there were different parameters to store.

// Types of parameters passed to the function (see "stdarg.h")
#define INTPRM  1
#define DBLPRM  2
#define CHRPRM  3

int parms[10];   // Array of arguments for History ()

 

// Function definition
void History (int op, int stn, int *parms, ...);

 

// USAGE: Record and event in program history
// HINT: always add a parms[x] = 0 at the end of the list!

parms[0] = INTPRM; parms[1] = DBLPRM; parms[2] = 0;
History (id, op, parms, data1, data2);

 

// History () function
// This function writes resord in program activity list
// This function accepts and handles a variable number of arguments
void History (int recordType, int station, int *parms, ...)
{
 int  fH, error = 0;
 char type, msg[256];
 SYSTEMTIME lt;
 va_list ap;

 // Start variable parms handling
 va_start (ap, parms);

 // Open history file (handle file I/O error)
....

 // Date / time of record
 GetLocalTime (&lt);
 sprintf (msg, "%02d/%02d/%04d  %02d:%02d", lt.wDay, lt.wMonth, lt.wYear,
  lt.wHour, lt.wMinute);
 sprintf (msg, "%s,%s,%d,%d", msg, operator, station, recordType);

 // Variable list of parameters
 while ((type = *parms++) != 0) {
  switch (type) {
   case INTPRM: sprintf (msg, "%s,%d", msg, va_arg (ap, int)); break;
   case DBLPRM: sprintf (msg, "%s,%.1f", msg, va_arg (ap, double)); break;
   case CHRPRM: sprintf (msg, "%s,%s", msg, va_arg (ap, char *)); break;
  }
 }

 // Write to file and close it (handle file I/O error)
strcat (msg, "\n");
...

Error:
 CloseFile (fH);
 // Stop variable parms handling
 va_end (ap);

 if (error) {
 // Error handling
}
 return;
}

Hope this helps you a little
Roberto



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 5
(4,184 Views)
Thank you Roberto.
I managed to do what I wanted to. In attachment my code for the ones interested.
Cheers,
Kristrio
0 Kudos
Message 3 of 5
(4,174 Views)

On my side I use this kind of simple function. this allow to add text in the text box.

I have also a variant with rich text box.

 

Bext Regards,

 

int UI_Printf(char *form_str, ...)   /* a printf emulation  ! */

 va_list list_arg;        
  char    strbox[500];
  int  iTraceFileHandle = 0;
  
 strbox[0]='\0';
//  translate form_str with list_arg into str   
 va_start(list_arg, form_str);
 vsprintf(strbox, form_str, list_arg);
 va_end(list_arg);

SetCtrlVal (uiPanel, UI_TEXT_BOX_INFO, strbox);
 
ProcessSystemEvents();
 return 0;
}

0 Kudos
Message 4 of 5
(4,114 Views)
This code is indeed much easier than mine. Thank you!
0 Kudos
Message 5 of 5
(3,963 Views)