home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / SelfSub__S2076517212007.psc / APIwindow.asm < prev    next >
Assembly Source File  |  2007-07-15  |  7KB  |  167 lines

  1. ;*****************************************************************************************
  2. ;** APIWindow.asm - subclassing thunk. Assemble with nasm.
  3. ;**
  4. ;** Paul_Caton@hotmail.com
  5. ;** Copyright free, use and abuse as you see fit.
  6. ;**
  7. ;** v1.0 LaVolpe: Created from Subclass ASM                        ... 20070630
  8. ;*****************************************************************************************
  9.  
  10. ;***************
  11. ;API definitions
  12. M_RELEASE    equ    8000h        ;VirtualFree memory release flag
  13.  
  14. ;******************************
  15. ;Stack frame access definitions
  16. %define lParam          [ebp + 48]  ;WndProc lParam
  17. %define wParam          [ebp + 44]  ;WndProc wParam
  18. %define uMsg            [ebp + 40]  ;WndProc uMsg
  19. %define hWnd            [ebp + 36]  ;WndProc hWnd
  20. %define lRetAddr        [ebp + 32]  ;Return address of the code that called us
  21. %define lReturn         [ebp + 28]  ;lReturn local, restored to eax after popad
  22. %define bHandled        [ebp + 20]  ;bHandled local, restored to edx after popad
  23.  
  24. ;***********************
  25. ;Data access definitions
  26. %define nCallStack      [ebx]       ;validation flag
  27. %define bShutdown       [ebx + 4]      ;Shutdown flag
  28. %define fnEbMode        [ebx + 8]      ;EbMode function address
  29. %define fnDefWinProc       [ebx + 12]  ;DefWindowProc function address
  30. %define fnDestroyWin    [ebx + 16]  ;DestroyWindow function address
  31. %define fnIsBadCodePtr  [ebx + 20]  ;IsBadCodePtr function address
  32. %define objOwner        [ebx + 24]  ;Owner object address
  33. %define addrCallback    [ebx + 28]  ;Callback address
  34. %define addrTableB      [ebx + 32]  ;Address of before original WndProc message table
  35. %define addrTableA      [ebx + 36]  ;Address of after original WndProc message table
  36. %define lParamUser      [ebx + 40]  ;User defined callback parameter
  37.  
  38. use32
  39. ;************
  40. ;Data storage
  41.     dd_nCallStack        dd 0        ;validation flag
  42.     dd_bShutdown        dd 0        ;Shutdown flag
  43.     dd_fnEbMode         dd 0        ;EbMode function address
  44.     dd_fnDefWinProc       dd 0        ;CallWindowProc function address
  45.     dd_fnDestroyWin        dd 0        ;SetWindowsLong function address
  46.     dd_fnIsBadCodePtr    dd 0        ;ISBadCodePtr function address
  47.     dd_objOwner         dd 0        ;Owner object address
  48.     dd_addrCallback    dd 0        ;Callback address
  49.     dd_addrTableB        dd 0        ;Address of before original WndProc message table
  50.     dd_addrTableA        dd 0        ;Address of after original WndProc message table
  51.     dd_lParamUser        dd 0        ;User defined callback parameter
  52.     
  53. ;***********
  54. ;Thunk start    
  55.     xor     eax, eax                ;Zero eax, lReturn in the ebp stack frame
  56.     xor     edx, edx                ;Zero edx, bHandled in the ebp stack frame
  57.     pushad                        ;Push all the cpu registers on to the stack
  58.     mov     ebp, esp                ;Setup the ebp stack frame
  59.     mov     ebx, 012345678h            ;dummy Address of the data, patched from VB
  60.  
  61.     xor     esi, esi                ;Zero esi
  62.  
  63.     cmp     fnEbMode, eax            ;Check if the EbMode address is set
  64.     jnz     _ide_state                ;Running in the VB IDE
  65.  
  66. Align 4
  67. _before:                        ;Before the original WndProc
  68.     dec     edx                     ;edx <> 0, bBefore callback parameter = True
  69.     mov     edi, addrTableB            ;Get the before message table
  70.     mov    lReturn, esi
  71.     call    _callback                ;Attempt the VB callback
  72.     
  73. _before_handled:
  74.     cmp     bHandled, esi            ;If bHandled <> False
  75.     jne     _return                ;The callback entirely handled the message
  76.  
  77. Align 4
  78. _original_wndproc:
  79.     call    _wndproc                ;Call the original WndProc
  80.     
  81. Align 4
  82. _after:                         ;After the original WndProc
  83.     xor     edx, edx                ;Zero edx, bBefore callback parameter = False
  84.     mov     edi, addrTableA            ;Get the after message table
  85.     call    _callback                ;Attempt the VB callback
  86.  
  87. Align 4
  88. _return:                        ;Clean up and return to caller
  89.     popad                        ;Pop all registers. lReturn is popped into eax
  90.     ret     16                    ;Return with a 16 byte stack release
  91.  
  92. Align 4
  93. _ide_state:                        ;Running under the VB IDE
  94.     cmp    bShutdown, esi
  95.     jz    _check_state
  96.  
  97.     call    _wndproc
  98.     jmp    _return
  99.  
  100. Align 4
  101. _check_state:
  102.     call    near fnEbMode            ;Determine the IDE state
  103.  
  104.     cmp     eax, dword 1            ;If EbMode = 1
  105.     je    _before                ;Running normally
  106.     
  107.     test    eax, eax                ;If EbMode = 0
  108.     jz    _shutdown            ;Ended, shutdown
  109.  
  110.     call    _wndproc                ;EbMode = 2, breakpoint... call original WndProc
  111.     jmp     _return                ;Return
  112.  
  113. Align 4
  114. _wndproc:                        
  115.     push    dword lParam            ;ByVal lParam
  116.     push    dword wParam            ;ByVal wParam
  117.     push    dword uMsg                ;ByVal uMsg
  118.     push    dword hWnd                ;ByVal hWnd
  119.     call    near fnDefWinProc       ;Call DefWindowProc
  120.     mov     lReturn, eax            ;Save the return value
  121.     jmp     _generic_ret            ;return
  122.  
  123. Align 4
  124. _shutdown:                        ;Destroy window
  125.     mov    bShutdown, dword 1
  126.     push    dword hWnd                ;Push the window handle
  127.     call    fnDestroyWin           ;Call DestroyWindow - only gets here when IDE has stopped
  128.     jmp    _return    
  129.  
  130. Align 4
  131. _callback:                        ;Validate the callback
  132.     mov     ecx, [edi]                ;ecx = table entry count
  133.     jecxz   _generic_ret            ;ecx = 0, table is empty
  134.  
  135.     test    ecx, ecx                ;Set the flags as per ecx
  136.     js    _call                    ;Table entry count is negative, all messages callback
  137.     add     edi, 4                ;Inc edi to point to the start of the callback table
  138.     mov     eax, uMsg                ;eax = the value to scan for
  139.     repne   scasd                    ;Scan the callback table for uMsg
  140.     jne     _generic_ret            ;uMsg not in the callback table
  141.  
  142. Align 4
  143. _call:                        ;Callback required, do it...
  144.     push    edx                     ;Preserve edx (bBefore)
  145.     push    dword addrCallback    ;Push the callback address
  146.     call    dword fnIsBadCodePtr    ;Check the code is live
  147.     pop     edx                     ;Preserve edx (bBefore)
  148.     jnz     _generic_ret            ;If not, skip callback
  149.  
  150.     lea     eax, lParamUser            ;Address of lParamUser
  151.     lea     ecx, bHandled            ;Address of bHandled
  152.     push    eax                     ;ByRef lParamUser
  153.     lea     eax, lReturn            ;Address of lReturn
  154.     push    dword lParam            ;ByVal lParam
  155.     push    dword wParam            ;ByVal wParam
  156.     push    dword uMsg                ;ByVal uMsg
  157.     push    dword hWnd                ;ByVal hWnd
  158.     push    eax                     ;ByRef lReturn
  159.     push    ecx                     ;ByRef bHandled
  160.     push    edx                     ;ByVal bBefore
  161.     push    dword objOwner            ;ByVal the owner object
  162.     call    near addrCallback            ;Call the zWndProc callback procedure
  163.  
  164. Align 4
  165. _generic_ret:                    ;Shared return (local only)
  166.     ret
  167.