LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

NewMenuItem() Event Callback Data

Solved!
Go to solution

Hi,

 

I am having trouble passing a string to the callback function using NewMenuItem(); 

I am able to receive the 'Menu Item Id' properly in the callback function but the 'Event Callback Data' is not being sent correctly to the callback function. I would like to pass a string as 'Event Callback Data'.   A little help please?

 

Thanks!

 

Sinnas

 

 

0 Kudos
Message 1 of 6
(4,428 Views)

You should use the SetCtrlAttribute function with the ATTR_CALLBACK_DATA attribute allowing to assign the callback data pointer to your user data.

Message 2 of 6
(4,418 Views)

...and don't forget that the value passed in callbackData parameter must be still valid when the function executes, so you must allocate string in the heap (calloc) and pass the string pointer.



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 3 of 6
(4,416 Views)

Hi Wolfgang/Roberto, thanks but i dont seem to be able to get it right. 

Is what im doing right? 

 

static char  *stringValue;   

Ini_GetPointerToRawString (g_myInifile, "CONF", itemName, &stringValue);
int MENU_PRINC_VER_TMP = NewMenuItem (DynaMenu, MENU_PRINC_VER, itemName, -1, 0, CallBack, stringValue);
SetCtrlAttribute (panprinci, MENU_PRINC_VER_TMP, ATTR_CALLBACK_DATA, stringValue);

 

??

 

Thanks!

0 Kudos
Message 4 of 6
(4,394 Views)
Solution
Accepted by topic author sinnas

Using pointers can be tricky as their content can be no more valid later when you want to use them.

In your case, Ini_GetPointerToRawString returns a pointer to an internal memory area which is not permanent: as you can read in the help for the function:

 

The address of the string will be valid until the next call to Ini_GetPointerToString, Ini_GetStringCopy, Ini_GetStringIntoBuffer, Ini_GetPointerToRawString, Ini_GetRawStringCopy, or Ini_GetRawStringIntoBuffer.

(highlight is mine)

 

That is, if you call any of the stated functions your pointer won't be valid any more.

I suppose this is even more true if you call Ini_Dispose when you have finished reading the .INI file.

 

What you should do is to obtain a copy of the string with Ini_GetRawStringCopy in a static or dynamic string and pass the address of that string to NewMenuItem.



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 5 of 6
(4,387 Views)

Got it! Thank you!!

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