Author Topic: CALLBACK function  (Read 368 times)

BugCatcher

  • Member
  • **
  • Posts: 68
CALLBACK function
« 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?

Vortex

  • Member
  • *****
  • Posts: 2372
Re: CALLBACK function
« Reply #1 on: August 26, 2020, 04:54:01 AM »
Hello,

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

Code: [Select]
BOOL EnumWindows(
  WNDENUMPROC lpEnumFunc,
  LPARAM      lParam
);

Code: [Select]
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 :
Code: [Select]
.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

BugCatcher

  • Member
  • **
  • Posts: 68
Re: CALLBACK function
« Reply #2 on: August 26, 2020, 05:38:13 AM »
Thx Vortex

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7607
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: CALLBACK function
« Reply #3 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".
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

BugCatcher

  • Member
  • **
  • Posts: 68
Re: CALLBACK function
« Reply #4 on: August 26, 2020, 09:44:08 PM »
Thx Hutch

TouEnMasm

  • Member
  • *****
  • Posts: 1391
    • EditMasm
Re: CALLBACK function
« Reply #5 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.
Fa is a musical note to play with CL

daydreamer

  • Member
  • *****
  • Posts: 1384
  • building nextdoor
Re: CALLBACK function
« Reply #6 on: August 28, 2020, 04:29:28 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
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D
I love assembly,because its legal to write
princess:lea eax,luke
:)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 7607
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: CALLBACK function
« Reply #7 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

TimoVJL

  • Member
  • ****
  • Posts: 562
Re: CALLBACK function
« Reply #8 on: August 28, 2020, 03:40:28 PM »
Are you after Small Window - DednDave, 4-2011


Code: [Select]
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
May the source be with you

Adamanteus

  • Member
  • **
  • Posts: 235
    • LLC "AMS"
Re: CALLBACK function
« Reply #9 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 :
« Last Edit: August 30, 2020, 07:02:32 AM by Adamanteus »

daydreamer

  • Member
  • *****
  • Posts: 1384
  • building nextdoor
Re: CALLBACK function
« Reply #10 on: August 28, 2020, 06:12:21 PM »
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
 
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D
I love assembly,because its legal to write
princess:lea eax,luke
:)