News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

ScanMap [Safe version]

Started by Tight_Coder_Ex, May 22, 2011, 08:57:27 PM

Previous topic - Next topic

Tight_Coder_Ex

There has been some discussion of the pecular method by which I handle windows messages, specifically not preserving registers as per M$ specs.  I wrestled with a problem for quite some time today and thought maybe there is a glitch, only to find out it was something different.  In any event, this version only allows EAX ECX & EDX to be modified, therefore the handler needent preserve anything.


                .586
                .model flat, stdcall
                 option casemap :none

        include         user32.inc
        includelib      user32.lib

                .code
;==========================================================================================================
; This is a safe version of its predecessor in that only EAX ECX EDX maybe altered.

;       ENTER   EAX = Pointer to windows message map

;       LEAVE   EAX = NULL or value returned from DefWindow or CallWindow APIs

; 2011-05-22                                                                                  3B = 59 Bytes
;----------------------------------------------------------------------------------------------------------

; NOTE: This version is 15 bytes larger than the original, so it is probably 25 to 30 cycles longer

ScanMap         proc

                push    esi                             ; Only ESI & EBX need be preserved if there isn't
                push    ebx                             ; and application defined handler for this message.

                mov     esi, eax                        ; Copy pointer to Message Map
                lodsd                                   ; Is not NULL if this window sub/super classed.
                xchg    ebx, eax
                lodsd                                   ; Get number of definitions in map
                mov     ecx, eax
                mov     edx, [ebp+16]                   ; Get local copy of Message being processed

        ; Cycle through each of the application handlers to see if there is a match

        @@:     lodsd                                   ; Message number from map
                cmp     eax, edx                        ; Same as passed from kernel
                lodsd                                   ; Get pointer to handler
                jz      @Hdl                            ; ZR = 1, then execute
                loopnz  @B                              ; Continue till ECX is NULL

        ; At this point either there wasn't a matching handler in list, or handler needs default
        ; processing such as from WM_CHAR

        @@:     or      ebx, ebx                        ; Is there a pointer to default procedure
                mov     ecx, ebx                        ; Copy as we need recover EBX & ESI
                pop     ebx
                pop     esi                             ; Restore prologue registgers
                jz     DefWindowProc                    ; Execute default process

        ; Window is sub/superclassed so an addition argument must be tacked on front of argument
        ; list

                pop     eax                             ; Get callers return address
                push    ecx                             ; Prepend lpPrevWndFunc
                push    eax                             ; So API will return to caller
                jmp     CallWindowProc                  ; Execute sub/super classed default proc.

        ; Execute application defined hanlder making sure nothing but EAX ECX EDX can/will be modified

        @Hdl:   lea     esi, [esp + 12]                 ; ESI points to hWnd, Msg, wParam & lParam
                push    ebp
                push    edi                             ; Now EDI EBP EBX ESI are on stack

                call    eax                             ; Execute handler

                pop     edi
                pop     ebp                             ; Restore
                jnc     @B                              ; CY=0 if procedure requires default processing

                pop     ebx
                pop     esi                             ; Restore two remaining registers

                xor     eax, eax                        ; Default value for app handled message
                ret     16                              ; Waste arguments and return to caller

ScanMap         endp
                end

hutch--

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php