flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Alternate windows procedure

Thread Post new topic Reply to topic

Joined: 14 Feb 2013
Posts: 58
Location: Alberta
I devised this algorithm to facilitate runtime modification of event handlers in windows procedure. It also illuminates the busyness of excessive conditionals and jumps. ScanMap also deals with sub/super classed windows and post default processing.

  MMap          dd       0                              ; Pointer to subclass procedure
                dd      (MainWnd - MMap - 8) / 8        ; Number of handlers in map
                dd      WM_COMMAND, CWndCmd             ; Event # & Procedure pointer combos
                dd      WM_DESTROY, MDest    
Entry point to window procedure
        mov     esi, MMap
        jmp     ScanMap    
Code to handle qualifying events

        lodsd                           ; Load pointer to subclass procedure if applicable
        xchg    ebx, eax                ; Move it into EBX
        lodsd                           ; Load number of handlers in map
        xchg    ecx, eax                ; Move it into ECX

    @@: lodsd                           ; Load corresponding handlers message number
        cmp      ax, [esp + 8]          ; Is it same as kernel message
        lodsd                           ; Load pointer to procedure
        jz      @Hd1                    ; If it's the same, then handle message
        loopnz  @B                      ; Keep going until end of map

    @@: or      ebx, ebx                ; Is there a subclass procedure
        jnz     @Sub
        jmp     [DefWindowProc]         ; Do default processing

  @Sub: pop     eax
        push    ebx
        push    eax
        jmp     [CallWindowProc]

  @Hd1: lea     esi, [esp + 4]          ; ESI points to hwnd, msg, wParam, lParam
        mov     edx, eax                ; So EAX is NULL at the beginning of all handlers
        xor     eax, eax
        call    edx                     ; Execute handler
        jnc     @B                      ; Branch if default handling required.

        xor     eax, eax                ; Handled messages return zero
        ret     16                      ; Waste parameters    
This may look a bit bizarre, but it does work, primarily because kernel code looks after cleaning up stack.

The handler simply looks like this and the only thing ScanMap needs is CF cleared if post default processing is required.

        push    0
        call    [PostQuitMessage]
        stc                             ; Don't require default processing
Post 29 Sep 2015, 00:04
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.