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

    你必须做的第一件事是创建共有的数据段。 所以我们使用 # 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 /
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值