home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / HANDLER / HANDLER.AS_ / HANDLER.AS
Encoding:
Text File  |  1993-02-08  |  7.1 KB  |  230 lines

  1. TITLE   HANDLER.ASM -- Sample Windows DLL ISR
  2. ;****************************************************************************
  3. ;
  4. ;   PROGRAM: handler.asm
  5. ;
  6. ;   PURPOSE: Demonstrates installing an interrupt handler under Windows 3.x
  7. ;            This ISR (Interrupt service routine) will simply count the
  8. ;            number of interrupts it receives.
  9. ;
  10. ;   FUNCTIONS:
  11. ;       SetISRWindow: Called by the application to pass window handle
  12. ;       GetISRCount:  Function to retrieve the current count
  13. ;       DoInt:        Performs PostMessage() to app upon interrupt
  14. ;       InstallHandler: Installs the interrupt handler
  15. ;       DeInstallHandler: Removes the interrupt handler
  16. ;
  17. ;****************************************************************************   
  18.  
  19.         .286
  20. memM    EQU 1                   ;Medium memory model
  21.  
  22.         .xlist
  23.         include windows.inc
  24.         include cmacros.inc
  25.         include handler.inc
  26.         .list
  27.  
  28.  
  29. nHookVector   EQU 09h           ;hook vect9, IRQ1, keyboard interrupt
  30. DOS_SetVector EQU 2500h
  31. DOS_GetVector EQU 3500h
  32.  
  33. sBegin  Data
  34. staticD dOldVector,0
  35. staticW nCount,0
  36. staticW hWndApp,0
  37. sEnd
  38.  
  39. sBegin  Code
  40.         assumes cs,Code
  41.         assumes ds,Data
  42. ;****************************************************************************
  43. ;  FUNCTION: LibMain(HANDLE, WORD, WORD, LPSTR)
  44. ;
  45. ;  PURPOSE:  Is called by LibEntry.  LibEntry is called by Windows when
  46. ;            the DLL is loaded.  The LibEntry routine is provided in
  47. ;            the LIBENTRY.OBJ in the SDK Link Libraries disk.  (The
  48. ;            source LIBENTRY.ASM is also provided.)  
  49. ;
  50. ;            LibEntry initializes the DLL's heap, if a HEAPSIZE value is
  51. ;            specified in the DLL's DEF file.  Then LibEntry calls
  52. ;            LibMain.  The LibMain function below satisfies that call.
  53. ;            
  54. ;            The LibMain function should perform additional initialization
  55. ;            tasks required by the DLL. LibMain should return a value of 1
  56. ;            if the initialization is successful.
  57. ;          
  58. ;****************************************************************************   
  59. cProc LibMain, <FAR,PUBLIC>, <si,di>
  60. parmW    hModule;
  61. parmW   wDataSeg;
  62. parmW   cbHeapSize;
  63. parmD   lpszCmdLine;
  64.         
  65. cBegin  LibMain
  66.         cCall   InstallHandler,<>
  67.         mov     ax,1
  68. cEnd    LibMain
  69.  
  70.  
  71. ;****************************************************************************
  72. ;   FUNCTION:  WEP(int)
  73. ;
  74. ;   PURPOSE:  Performs cleanup tasks when the DLL is unloaded.  WEP() is
  75. ;             called automatically by Windows when the DLL is unloaded (no
  76. ;             remaining tasks still have the DLL loaded). 
  77. ;
  78. ;******************************************************************************/
  79. cProc WEP, <FAR,PUBLIC>, <si,di>
  80. parmW   bSystemExit
  81.  
  82. cBegin  WEP
  83.         cCall   DeInstallHandler,<>
  84.         mov     ax,1
  85. cEnd    WEP
  86.  
  87.  
  88. ;****************************************************************************
  89. ;   FUNCTION:  SetISRWindow(HWND)
  90. ;
  91. ;   PURPOSE:  This routine receives the handle to the window that should
  92. ;             receive ISRM_RUPT messages.
  93. ;
  94. ;****************************************************************************   
  95. cProc SetISRWindow, <FAR,PUBLIC>, <si,di>
  96. parmW   hWnd
  97. cBegin  SetISRWindow
  98.         mov     ax,hWnd
  99.         mov     hWndApp,ax
  100. cEnd    SetISRWindow
  101.  
  102.  
  103.  
  104. ;****************************************************************************
  105. ;   FUNCTION:  GetISRCount()
  106. ;
  107. ;   PURPOSE:  This function simply returns the value of nCount to the
  108. ;             caller.
  109. ;
  110. ;****************************************************************************   
  111. cProc GetISRCount, <FAR,PUBLIC>, <si,di>
  112. cBegin  GetISRCount 
  113.         mov     ax,nCount
  114. cEnd    GetISRCount 
  115.  
  116.  
  117. ;****************************************************************************
  118. ;   FUNCTION:  DoInt()
  119. ;
  120. ;   PURPOSE:  This routine is called by the ISR in the InstallHandler
  121. ;             routine. Note that this routine is EXPORTED, so when it
  122. ;             is called, DS is loaded with the default data segment of
  123. ;             this DLL. Thus, the routine has access to dOldVector, so
  124. ;             it can call the next interrupt handler in the chain.
  125. ;
  126. ;
  127. ;****************************************************************************   
  128. cProc DoInt, <FAR,PUBLIC>, <si,di>
  129. cBegin  DoInt
  130.  
  131.         pushf
  132.         call    DWORD PTR dOldVector ; call previous handler
  133.  
  134.         inc     nCount
  135.         cmp     hWndApp,0
  136.         jz      doiexit
  137.  
  138.         mov     bx,ISRM_RUPT
  139.         sub     ax,ax
  140.         cCall   PostMessage,<hWndApp,bx,nCount,ax,ax>
  141.  
  142. doiexit:
  143.  
  144. cEnd    DoInt
  145.  
  146.  
  147. ;****************************************************************************
  148. ;   FUNCTION:  InstallHandler()
  149. ;
  150. ;   PURPOSE:  This routine saves the interrupt vector "nHookVector" in
  151. ;             the global variable "dOldVector". Then, it installs a small
  152. ;             ISR at that vector which calls the routine "DoInt()" when
  153. ;             the interrupt occurs.
  154. ;          
  155. ;
  156. ;****************************************************************************   
  157. cProc InstallHandler, NEAR, <si,di>
  158. cBegin  InstallHandler
  159.  
  160.         push    bx                      ;Save previous vector
  161.         push    es
  162.         mov     ax,DOS_GetVector + nHookVector
  163.         int     21h
  164.         mov     WORD PTR dOldVector,bx
  165.         mov     WORD PTR dOldVector+2,es
  166.         pop     es
  167.         pop     bx
  168.  
  169.  
  170.         push    ds                      ;Install handler
  171.         push    dx                      ;
  172.         push    cs
  173.         pop     ds
  174.         mov     dx,OFFSET MyISR
  175.         mov     ax,DOS_SetVector + nHookVector
  176.         int     21h
  177.         pop     dx      ;
  178.         pop     ds      ;
  179.  
  180.         jmp     set_exit
  181.  
  182.         ; ****  Entry point of ISR  ****
  183. MyISR:                                  ;Our ISR
  184.             pusha
  185.             push    ds
  186.             push    es
  187.  
  188.             cCall   DoInt               ;Do Interrupt Handling
  189.  
  190.             pop     es
  191.             pop     ds
  192.             popa
  193.             iret                        ;exit MyISR
  194.                 
  195. set_exit:                               ;exit InstallHandler                    
  196.  
  197. cEnd    InstallHandler
  198.  
  199.  
  200. ;****************************************************************************
  201. ;   FUNCTION:  DeInstallHandler()
  202. ;
  203. ;   PURPOSE:  Restores the interrupt vector "nHookVector" with the address
  204. ;             at "dOldVector".
  205. ;
  206. ;****************************************************************************   
  207. cProc DeInstallHandler, NEAR, <si,di>
  208. cBegin  DeInstallHandler
  209.  
  210.         push    ds                      ;
  211.         push    dx                      ;
  212.         mov     dx,WORD PTR dOldVector
  213.         mov     ax,WORD PTR dOldVector+2
  214.         cmp     dx,0                    ;were we installed?
  215.         jnz     dih_go
  216.         cmp     ax,0
  217.         jz      dih_skip
  218. dih_go:
  219.         mov     ds,ax
  220.         mov     ax,DOS_SetVector + nHookVector
  221.         int     21h
  222. dih_skip:
  223.         pop     dx      ;
  224.         pop     ds      ;
  225.  
  226. cEnd    DeInstallHandler
  227.  
  228. sEnd
  229.         end
  230.