The MASM Forum

General => The Campus => Topic started by: noname on August 07, 2015, 07:35:54 PM

Title: callback function template
Post by: noname on August 07, 2015, 07:35:54 PM
hi,

could anyone share some piece of code that shows how to create a callback function and a caller function?

my_Callback proc var1:DWORD, var2:DWORD
       ...
       ret
my_Callback endp


say, var5 refers to my_Callback.

caller_func proc var3:DWORD, var4:DWORD, var5:DWORD
      ...   ;   how do we use and call var5(=my_Callback) with its parameters inside this procedure?
      ret
caller_func endp




Title: Re: callback function template
Post by: dedndave on August 07, 2015, 07:50:45 PM
as far as the assembler syntax goes, caller and callback functions are treated the same
i.e., the assembler does not know "who" is going to call the function

WndProc is one example of a callback
there are many examples to look at

maybe you want an example like this ?
    INVOKE  caller_func,val3,val4,my_Callback

now, if you want to use the routine, inside the caller_func PROC....
there are bascially 2 ways to do it
one way is to simply PUSH arguments and CALL the function
not as pretty, but is simple to write
caller_func proc var3:DWORD, var4:DWORD, var5:DWORD
    push    val2
    push    val1
    call dword ptr var5


the other way is to create a TYPEDEF and PROTO at the beginning of the program
then, the function can be INVOKE'd
MYCALLBACK  TYPEDEF PROTO :DWORD,:DWORD
PMYCALLBACK TYPEDEF Ptr MYCALLBACK
.
.
.
caller_func proc var3:DWORD, var4:DWORD, var5:PMYCALLBACK

    INVOKE  var5,val1,val2


didn't test this, but it looks right   :P
Title: Re: callback function template
Post by: dedndave on August 07, 2015, 07:59:25 PM
error reporting will be different for the 2 methods

if you use PUSH/CALL, the assembler will not verify that you passed the proper number and size arguments
if you use INVOKE, the assember should throw an error if you don't have the proper arguments
Title: Re: callback function template
Post by: noname on August 07, 2015, 08:06:45 PM
thanks for the explanation dedndave.

i know what PROTO is. but i haven't come across "TYPEDEF PROTO" and "TYPEDEF PTR" before. What are they?
Title: Re: callback function template
Post by: dedndave on August 07, 2015, 08:10:50 PM
TYPEDEF allows to you to define a new data type
this is just an extreme example of it

the first one creates a type that is a proto
the second one creates a type that is a pointer to the proto
it is how we allow invoke to be used on a variable address
Title: Re: callback function template
Post by: jj2007 on August 07, 2015, 10:16:22 PM
Dave's example is perfectly correct, as usual, but maybe you want a complete one?

Note that there is nothing mysterious about a callback: You simply pass the address of a second proc to the first proc. The documentation will tell you how many arguments the CB wants - one in the example below.

include \masm32\include\masm32rt.inc

.code
TheTest proc pTxt, pTitle, pCB
  MsgBox 0, pTxt, pTitle, MB_YESNO
  .if eax==IDYES
push pTxt
call pCB
  .endif
  ret
TheTest endp

SomeCB proc arg
  MsgBox 0, arg, "Callback:", MB_OK
  ret
SomeCB endp

start:
  invoke TheTest, chr$("A text"), chr$("A title"), SomeCB
  exit
end start