02-12-2014 08:56 AM
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
Solved! Go to Solution.
02-12-2014 09:35 AM
You should use the SetCtrlAttribute function with the ATTR_CALLBACK_DATA attribute allowing to assign the callback data pointer to your user data.
02-12-2014 09:37 AM
...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.
02-13-2014 02:23 AM
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!
02-13-2014 03:01 AM - edited 02-13-2014 03:07 AM
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.
02-13-2014 04:11 AM
Got it! Thank you!!