Date: 02-16-2022
Return to Index
created by gbSnippets
'There are times when a programmer wants to indirectly call a local sub/function.
'The PowerBASIC CALL DWord statement supplies that capability.
'Primary Code:
'Credit: PowerBASIC Help
'These 3 statements set up the sub/function calls:
Declare Sub MySub(x&, y&) 'declare the local procedure
Local PtrMySub AS DWord 'define pointer variable
PtrMySub= CodePTR(MySub) 'get address of the local procedure
'The next 2 statements are equivalent - both call the procedure MySub:
Call MySub(x&, y&) 'direct call of the local procedure
Call DWord PtrMySub USING MySub(x&, y&) 'indirect call of the local procedure
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Declare Sub MySubCall(Param1%, Param2%) 'the local Sub/Function
Global hDlg as DWord, i as Long
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Direct", 50,10,100,20
Control Add Button, hDlg, 110,"Indirect", 50,40,100,20
Control Add Label, hDlg, 120,"<msg>", 50,70,150,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
Incr i : MySub (i)
End If
If CB.Msg = %WM_Command AND CB.Ctl = 110 AND CB.Ctlmsg = %BN_Clicked Then
Local PtrMySub AS DWord
Incr i : PtrMySub= CodePTR(MySub) 'pointer to the local Sub/Function
Call DWord PtrMySub USING MySub(i) 'indirect call
End If
End Function
Sub MySub(i as long)
Control Set Text hDlg, 120, "I have been here" + Str$(i) + " time" + Switch$(i=1,".",i<>1,"s.")
End Sub
'gbs_00318
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm