home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / WBK20.ZIP / WINBACK.C < prev    next >
C/C++ Source or Header  |  1993-03-03  |  6KB  |  177 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 = NULLHANDLE;
  17. PFN pfnInputHook;
  18.  
  19. INT main()
  20. {
  21.    HMQ hmq;
  22.    HWND hwndFrame,hwndClient;
  23.    QMSG qmsg;
  24.    ULONG frameflags ;
  25.    HMTX hmtx;                                    /* handle to semaphore */
  26.    if (DosCreateMutexSem("\\SEM32\\WINBACK.SEM", /* name of semaphore   */
  27.                     &hmtx,                       /* address of handle   */
  28.                     0,                           /* system-wide known   */
  29.                     TRUE))                       /* initially owned     */
  30.    {
  31.       DosBeep(600,200);                          /* Already running     */
  32.       DosExit(EXIT_PROCESS,1);                   /* Terminate           */
  33.    }
  34.  
  35.    hab = WinInitialize (0);
  36.    hmq = WinCreateMsgQueue (hab,0);
  37.  
  38.    WinRegisterClass (hab,                    /* Anchor block handle            */
  39.                      "WinBackClass",         /* Name of class being registered */
  40.                       WinbackWndProc,        /* Window procedure for class     */
  41.                       0,                     /* Class style                    */
  42.                       0);                    /* Extra bytes to reserve         */
  43.  
  44.    hwndFrame = WinCreateStdWindow (HWND_DESKTOP,  /* parent */
  45.                                    0,             /* Invisible */
  46.                                    &frameflags,   /* Pointer to control data */
  47.                                    "WinBackClass",/* Client window class name */
  48.                                    NULL,          /* titlebar text */
  49.                                    0L,            /* Style of client window*/
  50.                                    NULLHANDLE,    /* Module handle for resources */
  51.                                    0,             /* resource id */
  52.                                    &hwndClient);  /* Pointer to client window handle */
  53.  
  54.    {
  55.      SWCNTRL swctl ;
  56.      PID pid ;
  57.  
  58.      WinQueryWindowProcess ( hwndFrame, &pid, NULL ) ;
  59.      swctl.hwnd = hwndFrame ;
  60.      swctl.hwndIcon = NULLHANDLE ;
  61.      swctl.hprog = NULLHANDLE ;
  62.      swctl.idProcess = pid ;
  63.      swctl.idSession = 0 ;
  64.      swctl.uchVisibility = SWL_VISIBLE ;
  65.      swctl.fbJump = SWL_JUMPABLE ;
  66.      strcpy ( swctl.szSwtitle, "WinBack 2.0" ) ;
  67.  
  68.      WinAddSwitchEntry ( &swctl ) ;
  69.    }
  70.  
  71.   while (WinGetMsg(hab,&qmsg,NULLHANDLE,0,0))
  72.     WinDispatchMsg(hab,&qmsg);
  73.  
  74.   WinReleaseHook (hab,                       /* Make sure hook is released */
  75.                   NULLHANDLE,
  76.                   HK_INPUT,
  77.                   pfnInputHook,
  78.                   hmodule);
  79.  
  80.   WinDestroyWindow (hwndFrame);
  81.  
  82.   WinDestroyMsgQueue (hmq);
  83.  
  84.   WinTerminate (hab);
  85.  
  86.   return 0;
  87.   }
  88.  
  89. MRESULT EXPENTRY WinbackWndProc (HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
  90. {
  91.  
  92.    switch (msg)
  93.    {
  94.    case WM_CREATE:
  95.    {
  96.       ULONG rc;                              /* return code */
  97.       WinPostMsg(hwnd,WM_USER,0L,0L);
  98.  
  99.       rc = DosLoadModule( NULL,              /* Load the DLL module */
  100.                       0,                     /* containing the Hook */
  101.                       "Winhook",             /* procedure (must use a DLL). */
  102.                       &hmodule);             /* Get module handle */
  103.       if (rc)
  104.       {
  105.           WinMessageBox (HWND_DESKTOP,hwnd,
  106.                          "There was an error in loading WINHOOK.DLL",
  107.                          "DLL could not be loaded",
  108.                          0,
  109.                          MB_OK | MB_ICONEXCLAMATION);
  110.  
  111.           WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  112.       }
  113.       else
  114.       {
  115.           rc = DosQueryProcAddr( hmodule,      /* Get address of Hook Proc. */
  116.                                  0,
  117.                                  "WINHOOKPROC",     /* FOR THE WINSETHOOK AND */
  118.                                  &pfnInputHook );   /* WinReleaseHook calls.     */
  119.           if(rc)
  120.           {
  121.              WinMessageBox (HWND_DESKTOP,
  122.                             hwnd,
  123.                             "There is an error in WINHOOK.DLL",
  124.                             "Error in DLL",
  125.                             0,
  126.                             MB_OK | MB_ICONEXCLAMATION);
  127.  
  128.              WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  129.           }
  130.           else
  131.           {
  132.              WinSetHook (hab,
  133.                          NULLHANDLE,         /* set hook to capture */
  134.                          HK_INPUT,           /* input messages for filtering. */
  135.                          pfnInputHook,
  136.                          hmodule);
  137.          }
  138.       }
  139.       return FALSE;
  140.    }
  141.    case WM_USER:
  142.    {
  143.       WinDlgBox (HWND_DESKTOP,hwnd,InfoDlgProc,NULLHANDLE,InfoBox,NULL);
  144.       return (MRESULT) TRUE;
  145.    }
  146.    case WM_QUIT:
  147.    case WM_CLOSE:
  148.    case WM_DESTROY:
  149.    {
  150.       WinReleaseHook (hab,                   /* Make sure hook is released */
  151.                       NULLHANDLE,
  152.                       HK_INPUT,
  153.                       pfnInputHook,
  154.                       hmodule);
  155.    }
  156.  
  157.    }
  158.   return WinDefWindowProc(hwnd,msg,mp1,mp2);
  159. }
  160.  
  161. MRESULT EXPENTRY InfoDlgProc(HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
  162.  
  163. {
  164.    switch (msg)
  165.    {
  166.       case WM_INITDLG:
  167.          WinStartTimer (hab,hwnd,1,4000);
  168.          break;
  169.       case WM_TIMER:
  170.          WinStopTimer (hab,hwnd,1);
  171.          WinDismissDlg (hwnd,0);
  172.          break;
  173.    }
  174.    return WinDefDlgProc(hwnd,msg,mp1,mp2);
  175. }
  176.  
  177.