flat assembler
Message board for the users of flat assembler.

Index > Windows > Window Proc Assistant

Author
Thread Post new topic Reply to topic
Torrey



Joined: 12 Oct 2003
Posts: 78
Torrey 01 Jun 2005, 09:01
I created an include file to help with the handling of windows procs. Can some of you guys test it out if you get the time?

To setup the handling, you need to issue commands like the ones below before trying to create your window.

Code:
push wmcommand
push WM_COMMAND
call SetMsgHandler
push wmclose ;this is the handle to your routine
push WM_CLOSE ;actual window message value
call SetMsgHandler
;....and whatever else you'd like

;Your handler would be like this:
wmcommand:
        cmp [wParam],4
        je something
        ret
something:
        ret
;Get the idea?    



The Window Proc you set in your window creation parms is WndProc. Source code is pasted below, and attached.
Code:
hWnd                            dd      ?
wMsg                            dd      ?
wParam                          dd      ?
lParam                          dd      ?
WM_SETTINGCHANGE                equ     WM_WININICHANGE

_WM_ACTIVATE                    dd      ?
_WM_ACTIVATEAPP                 dd      ?
_WM_CANCELMODE                  dd      ?
_WM_CHILDACTIVATE               dd      ?
_WM_CLOSE                       dd      ?
_WM_COMMAND                     dd      ?
_WM_COMPACTING                  dd      ?
_WM_COPYDATA                    dd      ?
_WM_CREATE                      dd      ?
_WM_DESTROY                     dd      ?
_WM_ENABLE                      dd      ?
_WM_ENTERSIZEMOVE               dd      ?
_WM_EXITSIZEMOVE                dd      ?
_WM_GETICON                     dd      ?
_WM_GETMINMAXINFO               dd      ?
_WM_GETTEXT                     dd      ?
_WM_GETTEXTLENGTH               dd      ?
_WM_INITDIALOG                  dd      ?
_WM_INPUTLANGCHANGE             dd      ?
_WM_INPUTLANGCHANGEREQUEST      dd      ?
_WM_MOVE                        dd      ?
_WM_MOVING                      dd      ?
_WM_NCACTIVATE                  dd      ?
_WM_NCCALCSIZE                  dd      ?
_WM_NCCREATE                    dd      ?
_WM_NCDESTROY                   dd      ?
_WM_PARENTNOTIFY                dd      ?
_WM_POWER                       dd      ?
_WM_QUERYDRAGICON               dd      ?
_WM_QUERYOPEN                   dd      ?
_WM_QUIT                        dd      ?
_WM_SETICON                     dd      ?
_WM_SETTEXT                     dd      ?
_WM_SETTINGCHANGE               dd      ?
_WM_SHOWWINDOW                  dd      ?
_WM_SIZE                        dd      ?
_WM_SIZING                      dd      ?
_WM_STYLECHANGED                dd      ?
_WM_STYLECHANGING               dd      ?
_WM_USERCHANGED                 dd      ?
_WM_WINDOWPOSCHANGED            dd      ?
_WM_WINDOWPOSCHANGING           dd      ?
_WM_WININICHANGE                dd      ?
_END                            dd      0xFFFFFFFF

proc SetMsgHandler, WndMsg, Handler
     enter
     cmp [WndMsg],WM_ACTIVATE
     je Set_WM_ACTIVATE
     cmp [WndMsg],WM_ACTIVATEAPP
     je Set_WM_ACTIVATEAPP
     cmp [WndMsg],WM_CANCELMODE
     je Set_WM_CANCELMODE
     cmp [WndMsg],WM_CHILDACTIVATE
     je Set_WM_CHILDACTIVATE
     cmp [WndMsg],WM_CLOSE
     je Set_WM_CLOSE
     cmp [WndMsg],WM_COMMAND
     je Set_WM_COMMAND
     cmp [WndMsg],WM_COMPACTING
     je Set_WM_COMPACTING
     cmp [WndMsg],WM_COPYDATA
     je Set_WM_COPYDATA
     cmp [WndMsg],WM_CREATE
     je Set_WM_CREATE
     cmp [WndMsg],WM_DESTROY
     je Set_WM_DESTROY
     cmp [WndMsg],WM_ENABLE
     je Set_WM_ENABLE
     cmp [WndMsg],WM_ENTERSIZEMOVE
     je Set_WM_ENTERSIZEMOVE
     cmp [WndMsg],WM_EXITSIZEMOVE
     je Set_WM_EXITSIZEMOVE
     cmp [WndMsg],WM_GETICON
     je Set_WM_GETICON
     cmp [WndMsg],WM_GETMINMAXINFO
     je Set_WM_GETMINMAXINFO
     cmp [WndMsg],WM_GETTEXT
     je Set_WM_GETTEXT
     cmp [WndMsg],WM_GETTEXTLENGTH
     je Set_WM_GETTEXTLENGTH
     cmp [WndMsg],WM_INITDIALOG
     je Set_WM_INITDIALOG
     cmp [WndMsg],WM_INPUTLANGCHANGE
     je Set_WM_INPUTLANGCHANGE
     cmp [WndMsg],WM_INPUTLANGCHANGEREQUEST
     je Set_WM_INPUTLANGCHANGEREQUEST
     cmp [WndMsg],WM_MOVE
     je Set_WM_MOVE
     cmp [WndMsg],WM_MOVING
     je Set_WM_MOVING
     cmp [WndMsg],WM_NCACTIVATE
     je Set_WM_NCACTIVATE
     cmp [WndMsg],WM_NCCALCSIZE
     je Set_WM_NCCALCSIZE
     cmp [WndMsg],WM_NCCREATE
     je Set_WM_NCCREATE
     cmp [WndMsg],WM_NCDESTROY
     je Set_WM_NCDESTROY
     cmp [WndMsg],WM_PARENTNOTIFY
     je Set_WM_PARENTNOTIFY
     cmp [WndMsg],WM_POWER
     je Set_WM_POWER
     cmp [WndMsg],WM_QUERYDRAGICON
     je Set_WM_QUERYDRAGICON
     cmp [WndMsg],WM_QUERYOPEN
     je Set_WM_QUERYOPEN
     cmp [WndMsg],WM_QUIT
     je Set_WM_QUIT
     cmp [WndMsg],WM_SETICON
     je Set_WM_SETICON
     cmp [WndMsg],WM_SETTEXT
     je Set_WM_SETTEXT
     cmp [WndMsg],WM_SETTINGCHANGE
     je Set_WM_SETTINGCHANGE
     cmp [WndMsg],WM_SHOWWINDOW
     je Set_WM_SHOWWINDOW
     cmp [WndMsg],WM_SIZE
     je Set_WM_SIZE
     cmp [WndMsg],WM_SIZING
     je Set_WM_SIZING
     cmp [WndMsg],WM_STYLECHANGED
     je Set_WM_STYLECHANGED
     cmp [WndMsg],WM_STYLECHANGING
     je Set_WM_STYLECHANGING
     cmp [WndMsg],WM_USERCHANGED
     je Set_WM_USERCHANGED
     cmp [WndMsg],WM_WINDOWPOSCHANGED
     je Set_WM_WINDOWPOSCHANGED
     cmp [WndMsg],WM_WINDOWPOSCHANGING
     je Set_WM_WINDOWPOSCHANGING
     cmp [WndMsg],WM_WININICHANGE
     je Set_WM_WININICHANGE
     mov eax,-1
     return

Set_WM_ACTIVATE:
        mov eax,[Handler]
        mov [_WM_ACTIVATE],eax
        return
Set_WM_ACTIVATEAPP:
        mov eax,[Handler]
        mov [_WM_ACTIVATEAPP],eax
        return
Set_WM_CANCELMODE:
        mov eax,[Handler]
        mov [_WM_CANCELMODE],eax
        return
Set_WM_CHILDACTIVATE:
        mov eax,[Handler]
        mov [_WM_CHILDACTIVATE],eax
        return
Set_WM_CLOSE:
        mov eax,[Handler]
        mov [_WM_CLOSE],eax
        return
Set_WM_COMMAND:
        mov eax,[Handler]
        mov [_WM_COMMAND],eax
        return
Set_WM_COMPACTING:
        mov eax,[Handler]
        mov [_WM_COMPACTING],eax
        return
Set_WM_COPYDATA:
        mov eax,[Handler]
        mov [_WM_COPYDATA],eax
        return
Set_WM_CREATE:
        mov eax,[Handler]
        mov [_WM_CREATE],eax
        return
Set_WM_DESTROY:
        mov eax,[Handler]
        mov [_WM_DESTROY],eax
        return
Set_WM_ENABLE:
        mov eax,[Handler]
        mov [_WM_ENABLE],eax
        return
Set_WM_ENTERSIZEMOVE:
        mov eax,[Handler]
        mov [_WM_ENTERSIZEMOVE],eax
        return
Set_WM_EXITSIZEMOVE:
        mov eax,[Handler]
        mov [_WM_EXITSIZEMOVE],eax
        return
Set_WM_GETICON:
        mov eax,[Handler]
        mov [_WM_GETICON],eax
        return
Set_WM_GETMINMAXINFO:
        mov eax,[Handler]
        mov [_WM_GETMINMAXINFO],eax
        return
Set_WM_GETTEXT:
        mov eax,[Handler]
        mov [_WM_GETTEXT],eax
        return
Set_WM_GETTEXTLENGTH:
        mov eax,[Handler]
        mov [_WM_GETTEXTLENGTH],eax
        return
Set_WM_INITDIALOG:
        mov eax,[Handler]
        mov [_WM_INITDIALOG],eax
        return
Set_WM_INPUTLANGCHANGE:
        mov eax,[Handler]
        mov [_WM_INPUTLANGCHANGE],eax
        return
Set_WM_INPUTLANGCHANGEREQUEST:
        mov eax,[Handler]
        mov [_WM_INPUTLANGCHANGEREQUEST],eax
        return
Set_WM_MOVE:
        mov eax,[Handler]
        mov [_WM_MOVE],eax
        return
Set_WM_MOVING:
        mov eax,[Handler]
        mov [_WM_MOVING],eax
        return
Set_WM_NCACTIVATE:
        mov eax,[Handler]
        mov [_WM_NCACTIVATE],eax
        return
Set_WM_NCCALCSIZE:
        mov eax,[Handler]
        mov [_WM_NCCALCSIZE],eax
        return
Set_WM_NCCREATE:
        mov eax,[Handler]
        mov [_WM_NCCREATE],eax
        return
Set_WM_NCDESTROY:
        mov eax,[Handler]
        mov [_WM_NCDESTROY],eax
        return
Set_WM_PARENTNOTIFY:
        mov eax,[Handler]
        mov [_WM_PARENTNOTIFY],eax
        return
Set_WM_POWER:
        mov eax,[Handler]
        mov [_WM_POWER],eax
        return
Set_WM_QUERYDRAGICON:
        mov eax,[Handler]
        mov [_WM_QUERYDRAGICON],eax
        return
Set_WM_QUERYOPEN:
        mov eax,[Handler]
        mov [_WM_QUERYOPEN],eax
        return
Set_WM_QUIT:
        mov eax,[Handler]
        mov [_WM_QUIT],eax
        return
Set_WM_SETICON:
        mov eax,[Handler]
        mov [_WM_SETICON],eax
        return
Set_WM_SETTEXT:
        mov eax,[Handler]
        mov [_WM_SETTEXT],eax
        return
Set_WM_SETTINGCHANGE:
        mov eax,[Handler]
        mov [_WM_SETTINGCHANGE],eax
        return
Set_WM_SHOWWINDOW:
        mov eax,[Handler]
        mov [_WM_SHOWWINDOW],eax
        return
Set_WM_SIZE:
        mov eax,[Handler]
        mov [_WM_SIZE],eax
        return
Set_WM_SIZING:
        mov eax,[Handler]
        mov [_WM_SIZING],eax
        return
Set_WM_STYLECHANGED:
        mov eax,[Handler]
        mov [_WM_STYLECHANGED],eax
        return
Set_WM_STYLECHANGING:
        mov eax,[Handler]
        mov [_WM_STYLECHANGING],eax
        return
Set_WM_USERCHANGED:
        mov eax,[Handler]
        mov [_WM_USERCHANGED],eax
        return
Set_WM_WINDOWPOSCHANGED:
        mov eax,[Handler]
        mov [_WM_WINDOWPOSCHANGED],eax
        return
Set_WM_WINDOWPOSCHANGING:
        mov eax,[Handler]
        mov [_WM_WINDOWPOSCHANGING],eax
        return
Set_WM_WININICHANGE:
        mov eax,[Handler]
        mov [_WM_WININICHANGE],eax
        return
endp

proc WndProc, hwnd, wmsg, wparam, lparam
     enter
     push    ebx esi edi

     mov     eax,[hwnd]
     mov     [hWnd],eax
     mov     eax,[wmsg]
     mov     [wMsg],eax
     mov     eax,[wparam]
     mov     [wParam],eax
     mov     eax,[lparam]
     mov     [lParam],eax

     mov     esi,_WM_ACTIVATE
@@:
     lodsd
     cmp eax,0x0
     je @B
     cmp eax,0xFFFFFFFF
     je wmBYE
     call eax
wmBYE:
     xor     eax,eax
     pop     edi esi ebx
     return
endp    


Description:
Download
Filename: wproc_assistant.inc
Filesize: 7.31 KB
Downloaded: 282 Time(s)

Post 01 Jun 2005, 09:01
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 01 Jun 2005, 09:23
IMHO, it would be a lot better to do this with macros, and without using pointer indirection; a macro of the form HANDLE_MSG {WM_xxx, handle_wm_xxx}. I would start by mov eax, [wmsg], and then generate a sequence of cmp eax, WM_xxx / je handle_wm_xxx.

It's more compact than your method, and (while it doesn't really matter in the case of wndprocs), faster.
Post 01 Jun 2005, 09:23
View user's profile Send private message Visit poster's website Reply with quote
Torrey



Joined: 12 Oct 2003
Posts: 78
Torrey 01 Jun 2005, 09:30
Good idea! Which did cross my mind, but I'm not really a macro wizard. It took me a little while sitting here to type out all that stuff, but I'm sure a macro would have taken a lot less time.
Post 01 Jun 2005, 09:30
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 01 Jun 2005, 13:08
I'm certainly not a macro wizard either, but it would be a very simple thing to write, and shouldn't take more than a few minutes of research in the fasm documentation Smile
Post 01 Jun 2005, 13:08
View user's profile Send private message Visit poster's website Reply with quote
Torrey



Joined: 12 Oct 2003
Posts: 78
Torrey 01 Jun 2005, 18:07
Macro or not, if anyone gets the chance to test this include file, please let me know the results you have.

I simplified the code down. Now to use your window message routine just:

mov [_WM_xxx],wmYourRoutine

And remember to use WndProc.
Code:
hWnd                            dd      ?
wMsg                            dd      ?
wParam                          dd      ?
lParam                          dd      ?

_WM_ACTIVATE                    dd      ?
_WM_ACTIVATEAPP                 dd      ?
_WM_CANCELMODE                  dd      ?
_WM_CHILDACTIVATE               dd      ?
_WM_CLOSE                       dd      ?
_WM_COMMAND                     dd      ?
_WM_COMPACTING                  dd      ?
_WM_COPYDATA                    dd      ?
_WM_CREATE                      dd      ?
_WM_DESTROY                     dd      ?
_WM_ENABLE                      dd      ?
_WM_ENTERSIZEMOVE               dd      ?
_WM_EXITSIZEMOVE                dd      ?
_WM_GETICON                     dd      ?
_WM_GETMINMAXINFO               dd      ?
_WM_GETTEXT                     dd      ?
_WM_GETTEXTLENGTH               dd      ?
_WM_INITDIALOG                  dd      ?
_WM_INPUTLANGCHANGE             dd      ?
_WM_INPUTLANGCHANGEREQUEST      dd      ?
_WM_MOVE                        dd      ?
_WM_MOVING                      dd      ?
_WM_NCACTIVATE                  dd      ?
_WM_NCCALCSIZE                  dd      ?
_WM_NCCREATE                    dd      ?
_WM_NCDESTROY                   dd      ?
_WM_PARENTNOTIFY                dd      ?
_WM_POWER                       dd      ?
_WM_QUERYDRAGICON               dd      ?
_WM_QUERYOPEN                   dd      ?
_WM_QUIT                        dd      ?
_WM_SETICON                     dd      ?
_WM_SETTEXT                     dd      ?
_WM_SETTINGCHANGE               dd      ?
_WM_SHOWWINDOW                  dd      ?
_WM_SIZE                        dd      ?
_WM_SIZING                      dd      ?
_WM_STYLECHANGED                dd      ?
_WM_STYLECHANGING               dd      ?
_WM_USERCHANGED                 dd      ?
_WM_WINDOWPOSCHANGED            dd      ?
_WM_WINDOWPOSCHANGING           dd      ?
_WM_WININICHANGE                dd      ?
_END                            dd      0xFFFFFFFF

proc WndProc, hwnd, wmsg, wparam, lparam
     enter
     push    ebx esi edi

     mov     eax,[hwnd]
     mov     [hWnd],eax
     mov     eax,[wmsg]
     mov     [wMsg],eax
     mov     eax,[wparam]
     mov     [wParam],eax
     mov     eax,[lparam]
     mov     [lParam],eax

     mov     esi,_WM_ACTIVATE
@@:
     lodsd
     cmp eax,0x0
     je @B
     cmp eax,0xFFFFFFFF
     je wmBYE
     call eax
wmBYE:
     xor     eax,eax
     pop     edi esi ebx
     return
endp    


Description:
Download
Filename: wproc_assistant.inc
Filesize: 1.51 KB
Downloaded: 288 Time(s)

Post 01 Jun 2005, 18:07
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 01 Jun 2005, 18:49
Here's a very quick example of macro method:

Code:
macro handle_msg arg_msg, arg_handler
{
        cmp             eax, arg_msg
        je              arg_handler
}

        mov                     eax, [hwnd]
        handle_msg      WM_CREATE,      .create
        handle_msg      WM_DESTROY,     .destroy
        ; call default wndproc
        ret

.create:        ret
.destroy:       ret
    
Post 01 Jun 2005, 18:49
View user's profile Send private message Visit poster's website Reply with quote
Torrey



Joined: 12 Oct 2003
Posts: 78
Torrey 01 Jun 2005, 19:08
There's a major problem with this thing that I failed to realize before trying it all. We've all experienced problems with creating windows and dialogs at some time because of the touchy api calls for creating them.

The placement of the variables, and WndProc routine seem to be a deciding factor whether or not the include would work. It's almost confusing and not that surprising that MS hasn't fixed or at least improved the window creation api's.

I will probably end up going back to the drawing board on this one to redesign it. Thanks for the macro example.
Post 01 Jun 2005, 19:08
View user's profile Send private message Visit poster's website Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 05 Jun 2005, 13:34
I think I found an easier way:
Code:
          MSG_HANDLER_ARRAY     = $
          aMsg                  rd 400h
...
macro msg [cmd, handler]
 { common
    if ~ esi eq MSG_HANDLER_ARRAY
     mov esi, MSG_HANDLER_ARRAY
    end if
   forward
    mov eax, cmd
    mov dword [esi + eax * 4], handler }

macro msg.prepare
 { push edi
   mov eax, [DefWindowProc]
   mov ecx, 400h
   mov edi, MSG_HANDLER_ARRAY
   rep stosd
   pop edi }  
...
        msg.prepare

        msg     WM_CREATE, WndProc.Create,\
                WM_DESTROY, WndProc.Destroy
...
proc    WndProc, hWnd, uMsg, wParam, lParam
        mov     eax, [.uMsg]
        stdcall [aMsg + eax * 4], [.hWnd], eax, [.wParam], [.lParam]
        return

    .Create:
        xor     eax,eax
        ret     16

    .Destroy:
        invoke  PostQuitMessage, 0
        xor     eax, eax
        ret     16
endp    

Such solution is faster (slightly), but it has one bad side. Every 'ret' that's ending some message handler, must be followed by 16 like here 'ret 16'. But it's not so bad, one can easily got used to it Smile
Post 05 Jun 2005, 13:34
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.