LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I use (void *)callbackData ?

Hi !
I would like to pass a integer variable to the callback function through the (void *)callbackData but I have got an 'void *' : unknown size error.
Could you give me an example?
Thank in advance.

Heedone Jang
0 Kudos
Message 1 of 6
(6,019 Views)
In order to use correctly the callbackData parameter you must first set its value for the control with:

int integer_val;

SetCtrlAttribute (panel, control, ATTR_CALLBACK_DATA, (void *)integer_val);

This must be done OUTSIDE the control callback and BEFORE using it, and you have to do it programmatically (that s you cannot assiign a callbackData with the UIR editor).

After that you can use it in the control's callback casting it to the appropriate type:

int cv;

cv = (int)callbackData; // Read the callabckData and cast to an integer

If you haven't set the callbackData before using it, it hase the value of NULL.

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?
Message 2 of 6
(6,019 Views)
Step1:
SetCtrlAttribute (panel, control, ATTR_CALLBACK_DATA, &integer_val);

Step2:
cv = *(int *)callbackData; // Read the callabckData and cast to an integer
0 Kudos
Message 3 of 6
(6,019 Views)
Thank you very much!
0 Kudos
Message 4 of 6
(6,019 Views)
Hy there,

> Step1:
> SetCtrlAttribute (panel, control, ATTR_CALLBACK_DATA, &integer_val);

Be careful with this version.
In this case, the address of the variable integer_val is used as callback
data.
If this variable is a local variable, this will be somewhere in the stack
and won't contain the desired value any more.
Better is the way as described in earlier postings to type cast the value as
(void*).

If you need more than one value as callback data, you could use the
following method:

/* define */
int DoubleShort;
short value1, value2;

DoubleShort = (value1&0xFFFF) << 16 | (value2&0xFFFF);
SetCtrlAttribute (panel, control, ATTR_CALLBACK_DATA, (void*)DoubleShort);

/* use */
short value1, value2;

value1 = (short)((int)callbackData&0xFFFF);
value2 = (s
hort)(((int)callbackData&0xFFFF0000) >> 16);

Cheerio
Harald
0 Kudos
Message 5 of 6
(6,019 Views)

old thread but still useful!

can you explain better how the type cast (void*) metod works?

i usually declare the variable as static to avoid the volatile variable problem but actually i dont't like it.

another question, sometimes i use structure as variable to pass more than one value, is there any better way?

Davide Vittorio G. - TLGB S.R.L.
Italian SW Developer
0 Kudos
Message 6 of 6
(3,444 Views)