how do i define a callback function with assembly language?
is there a simple sample for me?
Posted on 2007-04-14 05:14:23 by zoncpp
There's lot of different callbacks - which one do you need? :)

Here's one of the most common examples, a wndproc, in MASM syntax:


WndProc proc STDCALL hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
mov eax,

cmp eax, WM_CREATE
je @@create
cmp eax, WM_DESTROY
je @@destroy
cmp eax, WM_DRAW
je @@draw

invoke DefWindowProc, , , ,
jmp @@exit

@@create:
; do stuff here
jmp @@exit
@@destroy:
; do stuff here
jmp @@exit
@@paint:
; do stuff here
jmp @@exit

@@exit:
ret
WndProc endp

Posted on 2007-04-14 05:33:47 by f0dder
thank you, but i work with Visual C++ and don't know asm language very well.

i add an section to my exe file, then my cods should be asm language.
in code of this section i call myFunc. one of parameters of this function is address of a callback function.
callback function is defined end of asm code (before end of asm block), and i calculate address of this function to send to myFunc function.(correctly)
but my exe encounter "Don't send" error.
how do i return of callback function to main address of code?
for example to call "ExitThread" (api function) i return of thread to main address of code


_asm
{
....
// push other parameters
push addressOfCallbackFunc
call myFunc
....
jmp End

CallbackFunc:
....
ret // ?????

End:
}
Posted on 2007-04-15 01:59:46 by zoncpp
A "callback function" is a general name for functions called by the OS (when OS wants your program to perform some task, or when it wants to notify you about some events). Please tell us WHICH callback function you need? You declare/define them just like any other function, but you must know which function you need, to correctly declare its parameters and return value(s). The most common example of a callback function is the "Window Callback Function" shown in f0dder's post.
Posted on 2007-04-15 07:14:38 by ti_mo_n

The term 'callback function' describes a particular way of using a function, it's not an os-specific thing, they can be found in all kinds of applications. In fact, if we loosen our thinking a little, we can describe C++ class interfaces as sets of callback functions, and the same for COM interfaces, like VB uses.

Just in Windows OS, we find a number of callback function prototypes for various 'agents', for example, Direct3D has a callback prototype that is specific to animated skinmesh event handling.

Custom callbacks can be of whatever form you choose.
Who is doing the calling? That's what defines your callback proto.

Posted on 2007-04-16 00:29:56 by Homer
A "callback function" is a general name for functions called by the OS (when OS wants your program to perform some task, or when it wants to notify you about some events). Please tell us WHICH callback function you need?


i need first type of callback function (when OS wants my program to perform some task).

i create Thread in the same code and i define function for thread and i send address of this function to CreateThread (api function). this code works well. in function of thread, i call ExitThread (api function) before return :
_asm
{
....
// push other parameters for CreateThread function
push addressOfThreadFunc
// push other parameters for CreateThread function
call CreateThread  // address of this api function
....
jmp End



myThreadFunc:
....

push 1
call ExitThread // address of this api function
ret

End:
}


now for callback function i don't know what i do !!! (for return of callback function)???
Posted on 2007-04-16 00:47:34 by zoncpp
The code snippet you posted is either severely incomplete, or severely flawed. Also, I don't really understand what you're trying to do (or perhaps more, WHY you're trying to do it, since there seems to be no advantage compared to writing it in regular C.)

Also, you're passing wrong amount of parameters to CreateThread. And you don't need ExitThread, you can simply return from the function instead.

What you really want is probably more along the lines of


DWORD WINAPI myThread(LPVOID param)
{
  __asm {
  // do stuff here
  }
}

void foobar()
{
  DWORD tid; // not really used, but needed for 9x compatibility
  HANDLE hthread;
  htread = CreateThread(0, 0, myThread, 0, 0, &tid);
  CloseHandle(htread); // avoid leaks
}
Posted on 2007-04-16 01:31:31 by f0dder
What you really want is probably more along the lines of

no! i don't have C or VC++ compiler, when my exe file is executing.


i want to add  my code, in first of code section of an exe file, accordingly, this code can not be wrote with c language. when my exe file execute, my addition code runs, in first of execution, then other codes of exe run, to continue.

i create a VC project what it reads an exe file then adds my asm code in first of code section of this file.

accordingly, my code should be wrote with asm language to able to run in exe file
Posted on 2007-04-16 03:29:59 by zoncpp
It doesn't matter much whether you write it in C/C++ or assembly, as long as you use API calls things will not work as-is. Same goes for pushing the address of your callback, that address will also be wrong when you write it to the exe. And when you do broken API code, well, that doesn't help either.

What's the purpose of your code adding anyway?

Posted on 2007-04-16 06:17:28 by f0dder
thank you for your pursuit

this code works correctly (when it added to exe file).
_asm
{
....
// push other parameters for CreateThread function
push addressOfThreadFunc
// push other parameters for CreateThread function
call CreateThread  // address of this api function
....
jmp End



myThreadFunc:
....

push 1
call ExitThread // address of this api function
ret

End:
}


my problem is: to return of callback function. because, all of the codes, in callback function, run correctly, until "ret"  :mad: :roll:
_asm
{
....
// push other parameters
push addressOfCallbackFunc
call myFunc
....
jmp End

CallbackFunc:
....
ret // ?????

End:
}

how do i return of callback function?
Posted on 2007-04-16 07:37:02 by zoncpp
I wonder how that code can actually work. It's ugly and the snippet you show is incomplete. And you still haven't answered what you're trying to accomplish.

Anyway, you'll need a "retn 4" to balance the stack, but that might not be enough.
Posted on 2007-04-16 10:13:01 by f0dder
i detect problem of my code :
i change "ebp" value into code of CallbackFunc function, accordingly, the CallbackFunc encounters whit problem, to return.
then i call "pushad" and "popad" in first and last of this function, and my problem resolves.
_asm
{
....
// push other parameters
push addressOfCallbackFunc
call myFunc
....
jmp End

CallbackFunc:
pushad
....
popad
ret

End:
}


thank you a lot :P
Posted on 2007-04-18 03:01:24 by zoncpp