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 / Callback.asm < prev    next >
Assembly Source File  |  2007-07-18  |  3KB  |  100 lines

  1. ;*****************************************************************************************
  2. ;** Callback.asm - Generic Class/Form/UserControl callback thunk. Assemble with nasm.
  3. ;**
  4. ;** Paul_Caton@hotmail.com
  5. ;** Copyright free, use and abuse as you see fit.
  6. ;**
  7. ;** v1.0 The original............................................................ 20060408
  8. ;** v1.1 IDE safe................................................................ 20060409
  9. ;** v1.1 Validate that the callback address is live code......................... 20060413
  10. ;** By Ralph Eastwood (tcmreastwood@ntlworld.com) - Modified for FASM assembler.. 20060901
  11. ;*****************************************************************************************
  12.  
  13. ;***********************
  14. ;Definitions
  15. %define     objOwner        [ebx]            ;Owner object address
  16. %define    addrCallback    [ebx + 4]      ;Callback address
  17. %define     fnEbMode        [ebx + 8]      ;EbMode address
  18. %define    fnIsBadCodePtr    [ebx + 12]  ;EbMode address
  19. %define    fnKillTimer        [ebx + 16]
  20. %define    RetValue        [ebp - 4]      ;Callback/thunk return value
  21.  
  22. [Bits 32]
  23. ;************
  24. ;Data storage
  25.     dd_objOwner         dd 0        ;Owner object address
  26.     dd_addrCallback    dd 0        ;Callback address
  27.     dd_fnEbmode         dd 0        ;EbMode address
  28.     dd_fnIsBadCodePtr    dd 0        ;IsBadCodePtr address
  29.     dd_fnKillTimer    dd 0        ;
  30.     
  31. ;***********
  32. ;Thunk start    
  33.     mov     eax, esp                ;Get a copy of esp as it is now
  34.     pushad                        ;Push all the cpu registers on to the stack
  35.     mov     ebx, 012345678h            ;Address of the data, patched from VB
  36.     mov     ebp, eax                ;Set ebp to point to the return address
  37.     
  38.     push    dword addrCallback    ;Callback address
  39.     call    fnIsBadCodePtr        ;Call IsBadCodePtr
  40.     jz     _check_ide                ;If the callback code isn't live, return
  41.  
  42.     call     _kill_timer
  43.     jmp    _return
  44.  
  45. Align 4
  46. _check_ide:    
  47.     cmp     fnEbMode, dword 0        ;Are we running in the IDE?
  48.     jnz     _ide_state            ;Check the IDE state
  49.  
  50. Align 4
  51. _ide_running:     
  52.     mov     eax, ebp                ;Copy the stack frame pointer
  53.     sub     eax, 4                  ;Address of the callback/thunk return value
  54.     push    eax                     ;Push the return value address
  55. nop
  56. nop
  57. nop    
  58.     mov     ecx, 012345678h         ;Parameter count into ecx, patched from VB
  59.     jecxz   _callback               ;If parameter count = 0, skip _parameter_loop
  60.  
  61. Align 4
  62. _parameter_loop:
  63.     push    dword [ebp + ecx * 4]   ;Push parameter
  64.     loop    _parameter_loop        ;Decrement ecx, if <> 0 jump to _parameter_loop
  65.  
  66. Align 4
  67. _callback:    
  68.     push    dword objOwner        ;Owning object
  69.     call    dword addrCallback        ;Make the callback
  70.  
  71. Align 4
  72. _return:    
  73.  
  74.     popad                ;Restore registers
  75.     ret     01234h            ;Return, the number of esp stack bytes to release is patched from VB
  76.     
  77. Align 4
  78. _ide_state:                ;Running under the VB IDE
  79.     call    near fnEbMode        ;Determine the IDE state
  80.  
  81.     cmp     eax, dword 1           ;If EbMode = 1
  82.     je        _ide_running        ;Running normally
  83.  
  84.     cmp     eax, dword 2            ;If EbMode = 2
  85.     je      _breakpoint             ;Breakpoint
  86.  
  87.     call _kill_timer
  88.  
  89. Align 4
  90. _breakpoint:    
  91.     xor     eax, eax            ;Zero eax
  92.     mov     RetValue, eax        ;Set the return value
  93.     jmp     _return            ;Outta here
  94.  
  95. Align 4
  96. _kill_timer:
  97.     push    dword [ebp + 12]
  98.     push    dword [ebp + 4]
  99.     call    fnKillTimer
  100.     ret