The MASM Forum

General => The Campus => Topic started by: BugCatcher on August 26, 2020, 04:38:55 AM

Title: CALLBACK function
Post by: BugCatcher on August 26, 2020, 04:38:55 AM
I know I've seen an example of a call back function some where in the forum a while ago, but I can't find it. Any help?
Title: Re: CALLBACK function
Post by: Vortex on August 26, 2020, 04:54:01 AM
Hello,

A classical example is the API function enumerating all the top-level windows :

BOOL EnumWindows(
  WNDENUMPROC lpEnumFunc,
  LPARAM      lParam
);


lpEnumFunc

Type: WNDENUMPROC

A pointer to an application-defined callback function. For more information, see EnumWindowsProc.


https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows

A quick example :
.386
.model flat,stdcall
option casemap:none

include     \masm32\include\windows.inc
include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc
include     \masm32\include\masm32.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\masm32.lib


EnumWndProc PROTO :DWORD,:DWORD


.data

f1          db 'Window handle = %X , window title = %s',13,10,0

.data?

buffer      db 64 dup(?)
buffer2     db 512 dup(?)

.code

start:

    invoke  EnumWindows,ADDR EnumWndProc,0

    invoke  ExitProcess,0
   

EnumWndProc PROC hwnd:DWORD,lParam:DWORD

    invoke  GetWindowText,hwnd,ADDR buffer,64

    invoke  wsprintf,ADDR buffer2,ADDR f1,\
            hwnd,ADDR buffer
           
    invoke  StdOut,ADDR buffer2

    mov     eax,1

    ret

EnumWndProc ENDP


END start
Title: Re: CALLBACK function
Post by: BugCatcher on August 26, 2020, 05:38:13 AM
Thx Vortex
Title: Re: CALLBACK function
Post by: hutch-- on August 26, 2020, 10:22:26 AM
If the type of callback is a subclass of a control, the MASM32 SDK has a tool for that task called surprisingly enough, "subclass.exe".
Title: Re: CALLBACK function
Post by: BugCatcher on August 26, 2020, 09:44:08 PM
Thx Hutch
Title: Re: CALLBACK function
Post by: TouEnMasm on August 26, 2020, 11:36:00 PM
There is three kind of Callback functions.
    * a simple proc  called  by a function (richedit streamread,streamwrite..)
    * an event loop installed by a subclass procedure (common controls)
    * an implemented interface (openfile...)

For each,you need to know what is the need of the function,msdn give you the needed information.
Title: Re: CALLBACK function
Post by: daydreamer on August 28, 2020, 04:29:28 AM
Quote from: hutch-- on August 26, 2020, 10:22:26 AM
If the type of callback is a subclass of a control, the MASM32 SDK has a tool for that task called surprisingly enough, "subclass.exe".
thanks I try that,need reorganize big wndproc,to several smaller parts
Title: Re: CALLBACK function
Post by: hutch-- on August 28, 2020, 09:50:28 AM
magnus,

You need to know the correct form for different callback types. A WndProc() is a procedure that you never call directly, it calling address is done in the WNDCLASSEX structure that is used by a call to CreateWindowEx. A "subclass" is a specialised access to the message handling procedure of a control like a button or any of the others. It get access to the message data BEFORE the control does which allows you to modify the actions of the control.

This is not a creative area where you experiment, it is built into the structure of the operating system and you need to do them correctly to get them to work. WIN32.HLP is your friend here, the basic architecture has not changed since the first versions of NT.
Title: Re: CALLBACK function
Post by: TimoVJL on August 28, 2020, 03:40:28 PM
Are you after Small Window - DednDave, 4-2011


WndProc PROC    hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
    mov     eax,uMsg
    .if eax==WM_CREATE
INVOKE  WMCreate, hWnd
    .elseif eax==WM_CLOSE
        INVOKE  WMClose, hWnd
jnz     @F
    .elseif eax==WM_DESTROY
        INVOKE  WMDestroy 
    .else
@@:     INVOKE  DefWindowProc,hWnd,uMsg,wParam,lParam
    .endif
    ret
WndProc ENDP

WMCreate PROC hWnd:HWND
    mov     eax,hWnd
    mov     hwndMain,eax
    xor     eax,eax
    ret
WMCreate ENDP

WMDestroy PROC
    INVOKE  PostQuitMessage,NULL
    xor     eax,eax
    ret
WMDestroy ENDP

WMClose PROC hWnd:HWND
    INVOKE  DestroyWindow,hWnd
    xor     eax,eax
    ret
WMClose ENDP
Title: Re: CALLBACK function
Post by: Adamanteus on August 28, 2020, 05:24:38 PM
  :eusa_boohoo: Good styled interface controls skeleton module, sure in my minimalistic style by luxury moderui button :
Title: Re: CALLBACK function
Post by: daydreamer on August 28, 2020, 06:12:21 PM
Quote from: hutch-- on August 28, 2020, 09:50:28 AM
magnus,

You need to know the correct form for different callback types. A WndProc() is a procedure that you never call directly, it calling address is done in the WNDCLASSEX structure that is used by a call to CreateWindowEx. A "subclass" is a specialised access to the message handling procedure of a control like a button or any of the others. It get access to the message data BEFORE the control does which allows you to modify the actions of the control.

This is not a creative area where you experiment, it is built into the structure of the operating system and you need to do them correctly to get them to work. WIN32.HLP is your friend here, the basic architecture has not changed since the first versions of NT.
I guess magnus.asm you made earlier with button subclassing is good example