You can install hook functions by calling WinSetHook, specifying the type of hook that calls the function-whether the function is to be associated with the system-message queue or with the queue of a particular thread-and a pointer to a function entry point. The following sample code shows how to install a hook function into the message queue of a thread:

BOOL EXPENTRY MyInputHook(HAB, PQMSG, USHORT);
HAB  hab;
HMQ  hmq;

WinSetHook(hab,                   /* Anchor block handle            */
           hmq,                   /* Thread message queue           */
           HK_INPUT,              /* Called by the input hook       */
           (PFN) MyInputHook,     /* Address of input-hook function */
           (HMODULE)NULL);        /* Function is in appl. module    */

Place hook functions associated with the system-message queue in a dynamic link library (DLL) separate from the application that installs the hook function. The installing application needs the handle of the DLL module before it can install the hook function. DosLoadModule, given the name of the DLL, returns the handle of the DLL module. Once you have the handle, you can call DosQueryProcAddr to obtain the address of the hook function. Finally, use WinSetHook to install the hook-function address in the appropriate hook list. WinSetHook passes the module handle, a pointer to the hook-function entry point, and NULL for the message-queue argument, indicating that the hook function should be associated with the system queue. The following sample code shows functions, called from the application's main routine, that initialize a DLL and install the hook function:

HAB     habDLL;
HMODULE hMod;
PFN     pfnInput;

/***********************************************************************/
/*  InitDLL: This function sets up the DLL and sets all variables.     */
/***********************************************************************/
void EXPENTRY InitDLL(HAB hab)
{
    habDLL = hab;

/***********************************************************************/
/*  Load the dll - actually, just get our module handle.               */
/***********************************************************************/
    DosLoadModule(NULL, 0, "HOOKDLL", &hMod);

/***********************************************************************/
/*  Find the address of the input hook procedure.                      */
/***********************************************************************/
    DosQueryProcAddr(hMod, 0, "InputProc", &pfnInput);
}

/***********************************************************************/
/*  StartInputHook: This function starts the hook filtering.           */
/***********************************************************************/
void EXPENTRY StartInputHook(void)
{

/***********************************************************************/
/*  Set a hook to our input filter routine.                            */
/***********************************************************************/
    WinSetHook(habDLL, NULLHANDLE, HK_INPUT, pfnInput, hMod);
}


[Back] [Next]