home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / WINBAK.ZIP / WINBACK.C next >
Text File  |  1991-10-25  |  6KB  |  182 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 ;
  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.      SWCNTRL swctl ;
  57.      HSWITCH hswitch ;
  58.      PID pid ;
  59.  
  60.      WinQueryWindowProcess ( hwndFrame, &pid, NULL ) ;
  61.      swctl.hwnd = hwndFrame ;
  62.      swctl.hwndIcon = NULL ;
  63.      swctl.hprog = NULL ;
  64.      swctl.idProcess = pid ;
  65.      swctl.idSession = 0 ;
  66.      swctl.uchVisibility = SWL_VISIBLE ;
  67.      swctl.fbJump = SWL_JUMPABLE ;
  68.      strcpy ( swctl.szSwtitle, "WinBack 1.2" ) ;
  69.  
  70.      hswitch = WinAddSwitchEntry ( &swctl ) ;
  71.    }
  72.  
  73.   while (WinGetMsg(hab,&qmsg,NULL,0,0))
  74.     WinDispatchMsg(hab,&qmsg);
  75.  
  76.   WinReleaseHook (hab,                       // Make sure hook is released
  77.             (HMQ) NULL,
  78.                   HK_INPUT,
  79.                   pfnInputHook,
  80.                   hmodule);
  81.  
  82.   WinDestroyWindow (hwndFrame);
  83.  
  84.   WinDestroyMsgQueue (hmq);
  85.  
  86.   WinTerminate (hab);
  87.  
  88.   return;
  89.   }
  90.  
  91. MRESULT CALLBACK WinbackWndProc (hwnd,msg,mp1,mp2)
  92. HWND         hwnd;
  93. USHORT       msg;
  94. MPARAM       mp1;
  95. MPARAM       mp2;
  96. {
  97.  
  98.    switch (msg)
  99.    {
  100.    case WM_CREATE:
  101.    {
  102.       USHORT rc;                             // return code
  103.       WinPostMsg(hwnd,WM_USER,0L,0L);
  104.  
  105.       rc = DosLoadModule( NULL,              // Load the DLL module
  106.                       0,                     // containing the Hook
  107.                       "Winhook",             // procedure (must use a DLL).
  108.                       &hmodule);             // Get module handle
  109.       if (rc)
  110.       {
  111.           WinMessageBox (HWND_DESKTOP,hwnd,
  112.                          "There was an error in loading WINHOOK.DLL",
  113.                          "DLL could not be loaded",
  114.                          0,
  115.                          MB_OK | MB_ICONEXCLAMATION);
  116.  
  117.           WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  118.       }
  119.       else
  120.       {
  121.           rc = DosGetProcAddr( hmodule,      // Get address of Hook Proc.
  122.                           "WINHOOKPROC",     // FOR THE WINSETHOOK AND
  123.                           &pfnInputHook );   // WinReleaseHook calls.
  124.           if(rc)
  125.           {
  126.              WinMessageBox (HWND_DESKTOP,
  127.                             hwnd,
  128.                             "There is an error in WINHOOK.DLL",
  129.                             "Error in DLL",
  130.                             0,
  131.                             MB_OK | MB_ICONEXCLAMATION);
  132.  
  133.              WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  134.           }
  135.           else
  136.           {
  137.              WinSetHook (hab,
  138.                    (HMQ) NULL,               // set hook to capture
  139.                          HK_INPUT,           // input messages for filtering.
  140.                          pfnInputHook,
  141.                          hmodule);
  142.          }
  143.       }
  144.       return FALSE;
  145.    }
  146.    case WM_USER:
  147.    {
  148.       WinDlgBox (HWND_DESKTOP,hwnd,InfoDlgProc,(HMODULE) NULL,InfoBox,NULL);
  149.       return (MRESULT) TRUE;
  150.    }
  151.    case WM_QUIT:
  152.    case WM_CLOSE:
  153.    case WM_DESTROY:
  154.    {
  155.       WinReleaseHook (hab,                   // Make sure hook is released
  156.                 (HMQ) NULL,
  157.                       HK_INPUT,
  158.                       pfnInputHook,
  159.                       hmodule);
  160.    }
  161.  
  162.    }
  163.   return WinDefWindowProc(hwnd,msg,mp1,mp2);
  164. }
  165.  
  166. MRESULT CALLBACK InfoDlgProc(HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2)
  167.  
  168. {
  169.    switch (msg)
  170.    {
  171.       case WM_INITDLG:
  172.          WinStartTimer (hab,hwnd,1,4000);
  173.          break;
  174.       case WM_TIMER:
  175.          WinStopTimer (hab,hwnd,1);
  176.          WinDismissDlg (hwnd,0);
  177.          break;
  178.    }
  179.    return WinDefDlgProc(hwnd,msg,mp1,mp2);
  180. }
  181.  
  182.