Author Topic: callback function template  (Read 3014 times)

noname

  • Regular Member
  • *
  • Posts: 3
callback function template
« 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





dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: callback function template
« Reply #1 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 ?
Code: [Select]
    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
Code: [Select]
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
Code: [Select]
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

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: callback function template
« Reply #2 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

noname

  • Regular Member
  • *
  • Posts: 3
Re: callback function template
« Reply #3 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?

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: callback function template
« Reply #4 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

jj2007

  • Member
  • *****
  • Posts: 13283
  • Assembly is fun ;-)
    • MasmBasic
Re: callback function template
« Reply #5 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.

Code: [Select]
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
Masm32 Tips, Tricks and Traps

Some members can't live without insulting others. It tells us a lot about them.