Event Handler

Tight_Coder_Ex Tight_Coder_Ex is offline Offline Feb 10th, 2005, 1:29 pm |
0
I prefer to use this method as it doesn't clutter a windows procedure with dozens of conditionals and it address my primary objective of coding as tight as possible [28 bytes]. If you think my method can be improved upon in size and/or speed please post your solution.

Note: I have not tested this algorithym yet
Quick reply to this message  
Assembly Syntax
  1. ; *** MESSAGE HANDLER ***
  2.  
  3. ; This procedure determines if application has a handler for a particular event. All windows
  4. ; procedures are passed to this subroutine except those that have been sub or super classed.
  5.  
  6. ; ENTRY: ESI = Pointer to message map
  7. ; ECX = Number of sets in map.
  8.  
  9. ; Each set consists of a 16 bit ID and 32 bit pointer to proceedure.
  10.  
  11. ; LEAVE: EAX = 0 if application handled event, or result of DefWindowProc is not.
  12. ; ____________________________________________________________________________________________
  13.  
  14. ; ESP + 0 = Return address in kernel module
  15. ; ESP + 4 = hWnd, this windows 32 bit handle
  16. ; ESP + 8 = Msg, Message ID passed by OS.
  17. ; ESP + 12 = wParam
  18. ; ESP + 16 = lParam
  19.  
  20. MsgHandler mov edx, [esp + 8] ; Get message ID 0 - 1024 or user defined.
  21.  
  22. ; In the unlikely event a null map is passed to this routine, this method of
  23. ; testing ECX first will prevent a fatal crash.
  24.  
  25. .NextMsg and ecx, ecx ; Are we at the end yet
  26. jz .Default ; ZR = 1, If we are to do default proceesing.
  27.  
  28. dec ecx ; Decrement counter
  29. lodsw ; Get 16 bit ID from message map
  30. cmp ax, dx ; Is there a match
  31. lodsd ; Load pointer
  32. jnz .NextMsg
  33.  
  34. ; At this point we've found a match and as many events need wParam & lParam,
  35. ; I'm going to establish a pointer to those in EBX. hWnd can be simply addressed
  36. ; by EBX - 8 or lParam by EBX + 4.
  37.  
  38. lea ebx, [esp + 12] ; EBX points to wParam
  39. call eax ; Execute event
  40. jnc .Default ; CY = 0, if event requires default proc.
  41. xor eax, eax ; Handled must return 0
  42. ret 16 ; Stack needs to be re-aligned.
  43.  
  44. ; As hWnd, Msg, wParam & lParam are already on the stack I use this method
  45. ; as a trace into kernel showed OS handles this appropriately and there is no
  46. ; point pushing onto stack what is already there. I've tested this on 98/ME/XP.
  47.  
  48. .Default pop ebx
  49. call _DefWindowProcA@16
  50. jmp ebx
  51.  
  52. ; You'll notice at this point we don't need RET 16 as the call to DefWindowProc has already
  53. ; done that for us.
  54.  
  55.  

Message:


Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC