深入解析钩子和动态链接库(中)

来源:互联网 发布:cad排版软件 编辑:程序博客网 时间:2024/04/29 23:08
    你必须做的第一件事是创建共有的数据段。所以我们使用# pragma data_seg声明。使用某一好记的数据段名字(它必须是没有比8 个字符长) 。我想强调名字是任意的,这里使用了我自己的名字。我发现如果我使用好的名字象.SHARE.SHR.SHRDATA,别人会认为名字有特殊的意义。但是,我要说NO
# pragma data_seg(".JOE")
HANDLE hWnd = NULL;
# pragma dta_seg()
# pragma comment(linker "/section:.JOE,rws ")
    # pragma声明一个数据段,在此范围内声明的变量在初始化后将被指派到该数据段,假设他们初始化. 如未初始化,变量将被分配到缺省数据段,而# pragma  不起作用。
      初看起来, 这将阻止你在共有的数据段使用一些C++ 对象,因为你无法初始化C++中用户定义的对象。这看来是一个根本局限。
      # pragma comment使连接器有命令行开关被显示增加到链接步骤。你可以进入VC++ 项目| 设置并且改变连接器命令行。
      你可以预定某一机制设置窗口句柄,例如
void SetWindow(HWND w) {hWnd = w; }
      但更经常的是如下所示的与钩子结合。
Sample: A Mouse Hook
header file (myhook.h)
      函数setMyHook并且clearMyHook必须在此被声明。这在我的另一文章中有详细论述。“The Ultimate DLL Header File.”
             _T("UMW_MOUSEHOOK-" /
            "{B30856F0-D3DD-11d4-A00B-006067718D04}")
source file (myhook.cpp)
#include "stdafx.h"
#include "myhook.h"
#pragma data_seg(".JOE")
HWND hWndServer = NULL;
#pragma data_seg()
#pragma comment("linker, /section:.JOE,rws")
HINSTANCE hInstance;
UINT HWM_MOUSEHOOK;
HHOOK hook;
// Forward declaration
static LRESULT CALLBACK msghook(int nCode, WPARAM wParam, LPARAM lParam);
/****************************************************************
*        DllMain
*        Inputs:
*       HINSTANCE hInst: Instance handle for the DLL
*       DWORD Reason: Reason for call
*       LPVOID reserved: ignored
*        Result: BOOL
*       TRUE if successful
*       FALSE if there was an error (never returned)
*       Effect:
*       Initializes the DLL.
****************************************************************/
BOOL DllMain(HINSTANCE hInst, DWORD Reason, LPVOID reserved)
{
  switch(Reason)
   {   /* reason */
      //**********************************************
      // PROCESS_ATTACH
      //**********************************************
    case DLL_PROCESS_ATTACH:
          // Save the instance handle because we need it to set the hook later
          hInstance = hInst;
          // This code initializes the hook notification message
          UWM_MOUSEHOOK = RegisterWindowMessage(UWM_MOUSEHOOK_MSG);
          return TRUE;
    //**********************************************
    // PROCESS_DETACH
    //**********************************************
   case DLL_PROCESS_DETACH:
          // If the server has not unhooked the hook, unhook it as we unload
         if(hWndServer != NULL)
                clearMyHook(hWndServer);
         return TRUE;
  } /* reason */
/****************************************************************
*       setMyHook
*        Inputs:
*       HWND hWnd: Window whose hook is to be set
*       Result: BOOL
*       TRUE if the hook is properly set
*       FALSE if there was an error, such as the hook already
*       being set
*       Effect:
*       Sets the hook for the specified window.
*       This sets a message-intercept hook (WH_GETMESSAGE)
*       If the setting is successful, the hWnd is set as the
*       server window.
****************************************************************/
__declspec(dllexport) BOOL WINAPI setMyHook(HWND hWnd)
 {
    if(hWndServer != NULL)
        return FALSE;
    hook = SetWindowsHookEx(
                          WH_GETMESSAGE,
                           (HOOKPROC)msghook,
                           hInstance,
                           0 );
   if(hook != NULL)
     {        /* success */
             hWndServer = hWnd;
              return TRUE;
     }       /* success */
   return FALSE;
 }    // SetMyHook
#define   UWM_MOUSEHOOK_MSG /