News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

callback function template

Started by noname, August 07, 2015, 07:35:54 PM

Previous topic - Next topic

noname

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





dedndave

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

dedndave

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

noname

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?

dedndave

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

jj2007

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