Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to pass variables to Callback Functions (without using Globals
#1
Hi All,
This is a general programming question but interested on any thoughts:
I try to use global variables (str, int, arrays) as much as possible and especially with COM objects to avoid the need for locking. So I try to pass things like child and window handles and arrays, etc in the function call and I construct the function to receive them as the defined parameters (top line).
However, when I use callbacks associated with a given COM object that I have used (e.g. MSFLEXGRID), I don't define the parameters of the available callback functions defined in the dll/ocx.
What is a good clean strategy for this. I have just been reluctantly globalizing more and more variables as needed. It worries me because I am also passing these variables in my custom functions (which I assume is the good behavior). But once I change this variable to global (and update all my declarations), then there is no point anymore in 'passing' the variable in my custom function definitions. But then this leads to sloppy dangerous code.
So any thoughts? I imagine this is a common problem.
Thanks,
Stuart

looking on StackOverflow and other forums, I am seeing some suggested solutions using set/get functions (which to me seem very similar to globals but just more inconvenient) and then stuff about wrapping/enclosing the callback in a more custom function that I can also pass my variables with. This sounds more promising. I just don't know how I would do this in QM.
Thanks for any advice.
S

I am sorry I didn't specify that these callback functions are Event handler callback functions eg. User clicked grid it selected row change.
S
#2
COM event handler functions can and in most cases should be #sub functions. Then use option v.

int k=5

#sub EventHandler v
out k

If this cannot be used, please give an example code.
#3
Yes, I think I understand. Similar to the messages3 area when using LV or grid control, these are in the main dialog box itself so can accept thread level variables and pass them out explicitly or by reference in custom-defined funcs. Will try to implement in this way and show examples if I can't.
Thx, S
#4
Yes, this worked!
When I created the event handler in a current version of QM, it created the callback as a subfunction in the parent dialog rather than a folder:
xGrid._setevents("FlexGrid_ICH_DMSFlexGridEvents") --> xGrid._setevents("sub.xGrid")

The only difference from your example was that I had to make the shared variables thread scope rather than local, else they wouldn't be recognized. Also I couldn't just pass hDlg like I often do in my other custom functions called out of my dialogs. I had to say something like: int- hMyDlg = hDlg

But this is truly great and allows me to avoid globals!
Thanks so much!,
Stuart
#5
Macro Macro284
Code:
Copy      Help
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "Dialog"
;3 Button 0x54032000 0x0 8 8 48 14 "Button"
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040800 "*" "" "" ""

int _hDlg
if(!ShowDialog(dd &sub.DlgProc 0)) ret


#sub DlgProc v
function# hDlg message wParam lParam

sel message
,case WM_INITDIALOG
,_hDlg=hDlg
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case 3
,sub.Sub2
,case IDOK
,case IDCANCEL
ret 1


#sub Sub2 v
outw _hDlg
#6
Cool! Thanks, S


Forum Jump:


Users browsing this thread: 1 Guest(s)