home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / WINBCK11.ZIP / WINBACK.C next >
C/C++ Source or Header  |  1991-10-21  |  5KB  |  165 lines

  1. /*-----------------------------------------------------------------
  2.   This module loads and releases the System Hook DLL, WINHOOK.DLL.
  3. ------------------------------------------------------------------*/
  4.  
  5. #define INCL_DOS
  6. #define INCL_ERRORS
  7. #define INCL_PM
  8.  
  9. #include <os2.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #include "winback.h"
  14.  
  15. HAB hab;
  16. HMODULE hmodule = (HMODULE)NULL;
  17. PFN pfnInputHook;
  18.  
  19.  
  20. VOID cdecl main()
  21. {
  22.    HMQ hmq;
  23.    HWND hwndFrame,hwndClient;
  24.    QMSG qmsg;
  25.    ULONG frameflags = 0;
  26.    HSYSSEM hssm;                             // handle to semaphore
  27.  
  28.    if (DosCreateSem(CSEM_PUBLIC,             // specifies ownership
  29.                     &hssm,                   // address of handle
  30.                     "\\sem\\winback.sem"))   // name of semaphore
  31.    {
  32.       DosBeep(600,200);                      // Already running
  33.       DosExit(EXIT_PROCESS,1);               // Terminate
  34.    }
  35.  
  36.    hab = WinInitialize (0);
  37.    hmq = WinCreateMsgQueue (hab,0);
  38.  
  39.    WinRegisterClass (hab,                    // Anchor block handle
  40.                      "WinBackClass",         // Name of class being registered
  41.                       WinbackWndProc,        // Window procedure for class
  42.                       0,                     // Class style
  43.                       0);                    // Extra bytes to reserve
  44.  
  45.    hwndFrame = WinCreateStdWindow (HWND_DESKTOP, // parent
  46.                                    0,        // Invisible
  47.                                    &frameflags, // Pointer to control data
  48.                                    "WinBackClass", // Client window class name
  49.                                    NULL,     // titlebar text
  50.                                    0L,       // Style of client window
  51.                          (HMODULE) NULL,     // Module handle for resources
  52.                                    0,        // resource id
  53.                                    &hwndClient); // Pointer to client window handle
  54.  
  55.  
  56.    while (WinGetMsg(hab,&qmsg,NULL,0,0))
  57.       WinDispatchMsg(hab,&qmsg);
  58.  
  59.   WinReleaseHook (hab,                       // Make sure hook is released
  60.             (HMQ) NULL,
  61.                   HK_INPUT,
  62.                   pfnInputHook,
  63.                   hmodule);
  64.  
  65.   WinDestroyWindow (hwndFrame);
  66.  
  67.   WinDestroyMsgQueue (hmq);
  68.  
  69.   WinTerminate (hab);
  70.  
  71.   return;
  72.   }
  73.  
  74. MRESULT CALLBACK WinbackWndProc (hwnd,msg,mp1,mp2)
  75. HWND         hwnd;
  76. USHORT       msg;
  77. MPARAM       mp1;
  78. MPARAM       mp2;
  79. {
  80.  
  81.    switch (msg)
  82.    {
  83.    case WM_CREATE:
  84.    {
  85.       USHORT rc;                             // return code
  86.       WinPostMsg(hwnd,WM_USER,0L,0L);
  87.  
  88.       rc = DosLoadModule( NULL,              // Load the DLL module
  89.                       0,                     // containing the Hook
  90.                       "Winhook",             // procedure (must use a DLL).
  91.                       &hmodule);             // Get module handle
  92.       if (rc)
  93.       {
  94.           WinMessageBox (HWND_DESKTOP,hwnd,
  95.                          "There was an error in loading WINHOOK.DLL",
  96.                          "DLL could not be loaded",
  97.                          0,
  98.                          MB_OK | MB_ICONEXCLAMATION);
  99.  
  100.           WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  101.       }
  102.       else
  103.       {
  104.           rc = DosGetProcAddr( hmodule,      // Get address of Hook Proc.
  105.                           "WINHOOKPROC",     // FOR THE WINSETHOOK AND
  106.                           &pfnInputHook );   // WinReleaseHook calls.
  107.           if(rc)
  108.           {
  109.              WinMessageBox (HWND_DESKTOP,
  110.                             hwnd,
  111.                             "There is an error in WINHOOK.DLL",
  112.                             "Error in DLL",
  113.                             0,
  114.                             MB_OK | MB_ICONEXCLAMATION);
  115.  
  116.              WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  117.           }
  118.           else
  119.           {
  120.              WinSetHook (hab,
  121.                    (HMQ) NULL,               // set hook to capture
  122.                          HK_INPUT,           // input messages for filtering.
  123.                          pfnInputHook,
  124.                          hmodule);
  125.          }
  126.       }
  127.       return FALSE;
  128.    }
  129.    case WM_USER:
  130.    {
  131.       WinDlgBox (HWND_DESKTOP,hwnd,InfoDlgProc,(HMODULE) NULL,InfoBox,NULL);
  132.       return (MRESULT) TRUE;
  133.    }
  134.    case WM_QUIT:
  135.    case WM_CLOSE:
  136.    case WM_DESTROY:
  137.    {
  138.       WinReleaseHook (hab,                   // Make sure hook is released
  139.                 (HMQ) NULL,
  140.                       HK_INPUT,
  141.                       pfnInputHook,
  142.                       hmodule);
  143.    }
  144.  
  145.    }
  146.   return WinDefWindowProc(hwnd,msg,mp1,mp2);
  147. }
  148.  
  149. MRESULT CALLBACK InfoDlgProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
  150.  
  151. {
  152.    switch (msg)
  153.    {
  154.       case WM_INITDLG:
  155.          WinStartTimer (hab,hwnd,1,4000);
  156.          break;
  157.       case WM_TIMER:
  158.          WinStopTimer (hab,hwnd,1);
  159.          WinDismissDlg (hwnd,0);
  160.          break;
  161.    }
  162.    return WinDefDlgProc(hwnd,msg,mp1,mp2);
  163. }
  164.  
  165.