LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How I can Pass variables trought a CVICALLBACK function defined for a Control in a panel?

Hi dudes!!!
Well the problem is simple.
Im using a struct to handle almost all the variables of a big part of my program and i've always used the USer event driven programming, but now i need to use a CVICALLBACK function for a specefied control on a panel, but i need to process information contained in the struct I told.--> ?How I can have acces to that variable struct from a CVICALLBACK? -->?I need to pass to the CVICALLBACK function as an aditional parameter, or What?
I tried to declare the struct as a global variable, the callback functions looks like to not reach it.
Please, Help!!!!
Thanks for everithing
0 Kudos
Message 1 of 4
(4,712 Views)
You can connect user data to controls or panels by passing the adress of the
struct to their callback-data.
You can use one of the following functions, depending if you want to put the
data to a panel or to a control.

InstallPanelCallback (panel,&function,&data); // callbackfunction +
callbackdata
SetPanelAttribute (panel, ATTR_CALLBACK_DATA,&data); // only callbackdata
InstallCtrlCallback (panel,control,&function,&data);
SetCtrlAttribute (panel,control, ATTR_CALLBACK_DATA,&data );

In the callback function use GetPanelAttribute or SetPanelAttribute to
receive your data.

Regard,
Manfred

ikerugo77 schrieb in im Newsbeitrag:
506500000008000000E82B0000-1000509150000@exchange.ni.com...
> Hi dudes!!!
> Well the problem is simple.
> Im using a
struct to handle almost all the variables of a big part of
> my program and i've always used the USer event driven programming, but
> now i need to use a CVICALLBACK function for a specefied control on a
> panel, but i need to process information contained in the struct I
> told.--> ?How I can have acces to that variable struct from a
> CVICALLBACK? -->?I need to pass to the CVICALLBACK function as an
> aditional parameter, or What?
> I tried to declare the struct as a global variable, the callback
> functions looks like to not reach it.
> Please, Help!!!!
> Thanks for everithing
0 Kudos
Message 2 of 4
(4,712 Views)
Yes, you are exactly right in both cases, you can either use a global variable or pass the structure to the callback function if you call the function yourself in your code.

To declare it globally just put it outside and before any block of code in the program module it is to be used in. You can reference the global variable from within any part of the module, even callback functions. You can even reference the global variable from external modules (other .c files that make up your program) as long as you don't declare it as static.

Now, if you want to call a callback function yourself you simply pass the address of the structure as the void *callbackdata parameter that is a part of any callback function used in CVI.

The following code shows a global structure of type zstruct being both passed to a callback function and reference in other sections of code as a global variable:

#include
#include
#include

typedef struct {
int x;
double y;
} zstruct;

zstruct mystruct;
int panelHandle;

int CVICALLBACK mycallback (int, int, int, void *, int, int);

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

mystruct.x = 5;
mystruct.y = 7;

mycallback(NULL, NULL, NULL, &mystruct, NULL, NULL);

printf("Press enter to quit!");
getchar();

return 0;
}

int CVICALLBACK mycallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
printf("mystruct.x = %d\n", ((zstruct*)callbackData)->x);
printf("mystruct.y = %f\n\n", ((zstruct*)callbackData)->y);

switch (event)
{
case EVENT_COMMIT:
// not using event information in this example
break;
}
return 0;
}

As far as why doing it with a global variable in your case didn't work I'm not really sure about, but what you should do is debug the code you are claiming should be able to work with it by putting strategic breakpoints in your callback functions to see if the code you are describing is actually being executed at all.

Jason F.
Applications Engineer
National Instruments
www.ni.com/ask
0 Kudos
Message 3 of 4
(4,712 Views)
Very important points on globals: "You can even reference the global variable from external modules (other .c files that make up your program) as long as you don't declare it as static."

I just spent 2 hours trying to figure out why I couldn't use global variables for my panel handles. Jason's note explains it: CVI's code generator declares the handles static. I removed the static keyword, and now all my other modules can see the handle. Thanks!
0 Kudos
Message 4 of 4
(4,712 Views)