home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / WINBACK.ZIP / WINBACK.C next >
Text File  |  1991-09-19  |  9KB  |  280 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. #define NUL '\0'
  9.  
  10. #include <os2.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #include "winback.h"
  15.  
  16. HAB              hab;
  17. HMODULE          hmodule = (HMODULE)NULL;
  18. PFN              pfnInputHook;
  19.  
  20. MRESULT EXPENTRY WinbackWndProc( HWND, USHORT, MPARAM, MPARAM );
  21.  
  22. VOID cdecl main()
  23.   {
  24.   HMQ              hmq;
  25.   HPOINTER         hptrIcon;
  26.   HWND             hwndDlg;
  27.  
  28.   hab = WinInitialize( NUL );
  29.   hmq = WinCreateMsgQueue( hab, 0 );
  30.  
  31.   hwndDlg = WinLoadDlg( HWND_DESKTOP,        /* Load the dialog window     */
  32.                         HWND_DESKTOP,        /* which allows turning the   */
  33.                         WinbackDlgProc,      /* hook on and off.  This     */
  34.                         NUL,                 /* window also provides a     */
  35.                         IDD_WINBACK,         /* visual notification that a */
  36.                         NUL );               /* hook may be active (if set)*/
  37.  
  38.   hptrIcon = WinLoadPointer( HWND_DESKTOP, NUL, IDD_WINBACK );
  39.  
  40.   WinSendMsg( hwndDlg,                       /* Attach the icon to the     */
  41.               WM_SETICON,                    /* frame window.              */
  42.               (MPARAM)hptrIcon,
  43.               (MPARAM)NULL );
  44.  
  45.   WinProcessDlg( hwndDlg );
  46.  
  47.   WinReleaseHook( hab,            /* Make sure hook is released */
  48.                   (HMQ)NULL,
  49.                   HK_INPUT,
  50.                   pfnInputHook,
  51.                   hmodule );
  52.  
  53.   WinDestroyWindow( hwndDlg );
  54.  
  55.   WinDestroyMsgQueue( hmq );
  56.  
  57.   WinTerminate( hab );
  58.  
  59.   return;
  60.   }
  61.  
  62.  
  63. /***************************************************************************|
  64. | Change Pointer Window Procedure                                             |
  65. |***************************************************************************/
  66.  
  67. MRESULT EXPENTRY WinbackDlgProc( hwnd, msg, mp1, mp2 )
  68. HWND         hwnd;
  69. USHORT       msg;
  70. MPARAM       mp1;
  71. MPARAM       mp2;
  72. {
  73.   USHORT      usCheckedState;
  74.   HWND        hwndSysMenu;          /* system menu handle       */
  75.  
  76.   switch(msg)
  77.   {
  78.  
  79.     case WM_INITDLG:
  80.     {
  81.       USHORT      idSysMenu;            /* system menu id           */
  82.       MENUITEM    miSysMenu;            /* system menu item info    */
  83.       MENUITEM    miMenuItem;           /* About menu item info     */
  84.       USHORT           rc = 0;           /* return code */
  85.  
  86.         /* add About item to system menu */
  87.         if ( hwndSysMenu = WinWindowFromID( hwnd, FID_SYSMENU ) )
  88.         {
  89.           /* get handle of system submenu */
  90.           idSysMenu = SHORT1FROMMR( WinSendMsg( hwndSysMenu, MM_ITEMIDFROMPOSITION, MPFROMSHORT( 0 ), 0L ) );
  91.           WinSendMsg( hwndSysMenu, MM_QUERYITEM, MPFROM2SHORT( idSysMenu, FALSE ), MPFROMP( &miSysMenu ) );
  92.           hwndSysMenu = miSysMenu.hwndSubMenu;
  93.  
  94.           /* add menu separator */
  95.           miMenuItem.iPosition = MIT_END;
  96.           miMenuItem.afStyle = MIS_SEPARATOR;
  97.           miMenuItem.afAttribute = 0;
  98.           miMenuItem.id = 0;
  99.           miMenuItem.hwndSubMenu = NUL;
  100.           miMenuItem.hItem = NUL;
  101.           WinSendMsg( hwndSysMenu, MM_INSERTITEM, MPFROMP( &miMenuItem ), NUL );
  102.  
  103.           /* add About item */
  104.           miMenuItem.afStyle = MIS_TEXT;
  105.           miMenuItem.id = IDM_ABOUT;
  106.           WinSendMsg( hwndSysMenu, MM_INSERTITEM, MPFROMP( &miMenuItem ), MPFROMP( "A~bout..." ) );
  107.  
  108.        }
  109.  
  110.       WinSetDlgItemText( hwnd,            /* Set title bar text        */
  111.                           FID_TITLEBAR,
  112.                         "WinBack Utility" );
  113.  
  114.       WinSendMsg( WinWindowFromID( hwnd,IDCB_WINBACKSW ),
  115.                   BM_SETCHECK,                /* set check box */
  116.                   MPFROMSHORT(1),
  117.                   (MPARAM)NULL );
  118.  
  119.  
  120.       rc = 0;
  121.       rc = DosLoadModule( NULL,                /* Load the DLL module        */
  122.                       0,                        /* containing the Hook        */
  123.                       "Winhook",               /* procedure (must use a DLL).*/
  124.                       &hmodule );               /* Get module handle          */
  125.       if(rc)
  126.       {
  127.           WinMessageBox (HWND_DESKTOP,HWND_DESKTOP,
  128.                         "There was an error in loading WINHOOK.DLL",
  129.                         "DLL could not be loaded",
  130.                          0,MB_OK | MB_ERROR);
  131.           WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  132.       }
  133.       else
  134.       {
  135.           rc = DosGetProcAddr( hmodule,            /* Get address of Hook Proc.  */
  136.                           "WINHOOKPROC",          /* FOR THE WINSETHOOK AND     */
  137.                           &pfnInputHook );         /* WinReleaseHook calls.      */
  138.           if(rc)
  139.           {
  140.              WinMessageBox (HWND_DESKTOP,HWND_DESKTOP,
  141.                            "There is an error in WINHOOK.DLL",
  142.                            "Error in DLL",
  143.                             0,MB_OK | MB_ERROR);
  144.              WinPostMsg(hwnd,WM_CLOSE,0L,0L);
  145.           }
  146.           else
  147.           {
  148.              WinSetHook( hab,
  149.                          (HMQ)NULL,   /* set hook to capture*/
  150.                          HK_INPUT,    /* input messages for filtering.*/
  151.                          pfnInputHook,
  152.                          hmodule );
  153.          }
  154.       }
  155.       break;
  156.     }
  157.  
  158.     case WM_COMMAND:
  159.       switch ( COMMANDMSG( &msg )->cmd )
  160.       {
  161.         case IDM_ABOUT:
  162.           WinDlgBox( HWND_DESKTOP, hwnd, AboutDlgProc, NUL, IDD_ABOUT, NUL );
  163.           return( (MRESULT)TRUE );
  164.           break;
  165.       break;
  166.       }
  167.  
  168.     case WM_CONTROL:
  169.       if(    (SHORT1FROMMP(mp1) != IDCB_WINBACKSW)   /* Only care about CLICK */
  170.           || (SHORT2FROMMP(mp1) != BN_CLICKED) )  /* Check Box messages.   */
  171.         break;
  172.       usCheckedState                               /* Query if check box is*/
  173.         = SHORT1FROMMR( WinSendDlgItemMsg( hwnd,  /* set or not after     */
  174.                                     IDCB_WINBACKSW,
  175.                                     BM_QUERYCHECK, /* activity in the check*/
  176.                                     (MPARAM)NULL,  /* box.                 */
  177.                                     (MPARAM)NULL ) );
  178.       if( usCheckedState == 1 )
  179.       {
  180.         WinSetHook( hab,                   /* Check box is checked so make */
  181.                     (HMQ)NULL,             /* sure hook is "set" to capture*/
  182.                     HK_INPUT,              /* input messages for filtering.*/
  183.                     pfnInputHook,
  184.                     hmodule );
  185.       }
  186.       else
  187.       {
  188.         WinReleaseHook( hab,               /* Check box is not checked so  */
  189.                         (HMQ)NULL,         /* make sure hook is not "set"  */
  190.                         HK_INPUT,          /* so input messages are not    */
  191.                         pfnInputHook,      /* intercepted by the hook      */
  192.                         hmodule );         /* procedure.                   */
  193.       }
  194.       break;
  195.  
  196.     case WM_ERASEBACKGROUND:
  197.       return( (MRESULT)TRUE );
  198.       break;
  199.  
  200.     case WM_MINMAXFRAME:
  201.       {
  202.         PSWP        pswp;                     /* pos change structure */
  203.  
  204.         /* hide check box when minimized so it doesn't overwrite icon */
  205.         pswp = PVOIDFROMMP( mp1 );
  206.         WinShowWindow(
  207.           WinWindowFromID( hwnd, IDCB_WINBACKSW ),
  208.           !(pswp->fs & SWP_MINIMIZE)
  209.         );
  210.       }
  211.       break;
  212.  
  213.  
  214.   }
  215.  
  216.   return( WinDefDlgProc( hwnd, msg, mp1, mp2 ) );
  217. }
  218.  
  219. /*
  220.  * AboutDlgProc( hwndDlg, usMsg, mp1, mp2 ) : MRESULT;
  221.  *
  222.  *    hwndDlg        handle to dialog box
  223.  *    usMsg          message number
  224.  *    mp1            message parameter 1
  225.  *    mp2            message parameter 2
  226.  *
  227.  * This is the dialog procedure for the About dialog box.
  228.  *
  229.  */
  230.  
  231. MRESULT EXPENTRY AboutDlgProc( HWND hwndDlg, USHORT usMsg, MPARAM mp1, MPARAM mp2 )
  232. {
  233.   MRESULT       mresRtnVal;                 /* function return value  */
  234.   BOOL          fPassToDef;                 /* pass to def dlg proc?  */
  235.  
  236.   mresRtnVal = FALSE;
  237.   fPassToDef = FALSE;
  238.  
  239.   switch ( usMsg ) {
  240.  
  241.     case WM_INITDLG:
  242.       break;
  243.  
  244.     case WM_COMMAND:
  245.       switch ( COMMANDMSG( &usMsg )->cmd ) {
  246.         case DID_OK:
  247.           WinDismissDlg( hwndDlg, TRUE );
  248.           break;
  249.         default:
  250.           fPassToDef = TRUE;
  251.           break;
  252.       }
  253.       break;
  254.  
  255.     default:
  256.       fPassToDef = TRUE;
  257.       break;
  258.  
  259.   }
  260.  
  261.   if ( fPassToDef )
  262.     mresRtnVal = WinDefDlgProc( hwndDlg, usMsg, mp1, mp2 );
  263.  
  264.   return mresRtnVal;
  265. }
  266.  
  267. /* Exit List processing at program termination */
  268.  
  269. VOID APIENTRY ProgramTermination( )
  270.   {
  271.  
  272.   if( hmodule != (HMODULE)NULL )       /* Unfortunately does not free the  */
  273.     DosFreeModule( hmodule );          /* DLL because System Input Queue   */
  274.                                        /* hooks cannot be freed.           */
  275.  
  276.  
  277.   DosExitList( EXLST_EXIT,             /* Required to end ExitList process.*/
  278.                (PFNEXITLIST)ProgramTermination );
  279.   }
  280.