钩子程序的VC++版

主程序

_______________________________________________________________________________________________________________

* .CPP文件

#include <windows.h>
#include "resource.h"
#include "cpp1.h"

#define WM_MOUSEHOOK WM_USER+6

void InstallHook(HWND);
void UninstallHook();

HINSTANCE hInstance;
BOOL Hook=FALSE;
HHOOK myhook;

int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)DialogProc,NULL);
ExitProcess(0);
}
BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
 char mybuffer1[MAX_PATH];
 char mybuffer2[MAX_PATH];
 char mybuffer3[MAX_PATH];
 LPRECT myrect;
 int wmesgID,wmesgEVENT;
 switch(uMsg)
 {
 case WM_INITDIALOG:
  myrect=new RECT;
  GetWindowRect(hwndDlg,myrect);
  SetWindowPos(hwndDlg,HWND_TOPMOST,myrect->left,myrect->top,myrect->right,myrect->bottom,SWP_SHOWWINDOW);
  return(TRUE);
 case WM_MOUSEHOOK:
  wsprintf(mybuffer1,"%lx",wParam);
  SetDlgItemText(hwndDlg,IDC_EDIT1,mybuffer1);
        GetClassName((HWND)wParam,mybuffer2,MAX_PATH);
        SetDlgItemText(hwndDlg,IDC_EDIT2,mybuffer2);
  wsprintf(mybuffer3,"%lx",GetClassLong((HWND)wParam,GCL_WNDPROC));
  SetDlgItemText(hwndDlg,IDC_EDIT3,mybuffer3);
  return(TRUE);
 case WM_COMMAND:
  wmesgID=LOWORD(wParam);
  wmesgEVENT=HIWORD(lParam);
  switch(wmesgID)
  {
    case IDOK:    
      return (TRUE);
    case IDC_MYHOOK:
      if(Hook==FALSE)
      {
      InstallHook(hwndDlg);
         Hook=TRUE;
      SetDlgItemText(hwndDlg,IDC_MYHOOK,"unhook");
      }
      else
      {
       UninstallHook();
                   SetDlgItemText(hwndDlg,IDC_MYHOOK,"hook");
       Hook=FALSE;
                   SetDlgItemText(hwndDlg,IDC_EDIT1,"NULL");
       SetDlgItemText(hwndDlg,IDC_EDIT2,"NULL");
       SetDlgItemText(hwndDlg,IDC_EDIT3,"NULL");
      }
               return (TRUE);
    case IDCANCEL:
      if(Hook==TRUE) UninstallHook();
         EndDialog(hwndDlg, TRUE);
               return (TRUE); // TRUE 表示处理过信息
  }
          break;
 }
 return (FALSE);
}

 

*.H 文件

BOOL CALLBACK DialogProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam);

_______________________________________________________________________________________________________________

主DLL文件

_______________________________________________________________________________________________________________

*.CPP文件

 

DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved)
{
 // Remove this if you use lpReserved
 UNREFERENCED_PARAMETER(lpReserved);

 if (dwReason == DLL_PROCESS_ATTACH)
 {
  TRACE0("TEMPDLL1.DLL Initializing!/n");
        hInstance=hInst;
  // Extension DLL one-time initialization
  if (!AfxInitExtensionModule(Tempdll1DLL, hInstance))
   return 0;

  // Insert this DLL into the resource chain
  // NOTE: If this Extension DLL is being implicitly linked to by
  //  an MFC Regular DLL (such as an ActiveX Control)
  //  instead of an MFC application, then you will want to
  //  remove this line from DllMain and put it in a separate
  //  function exported from this Extension DLL.  The Regular DLL
  //  that uses this Extension DLL should then explicitly call that
  //  function to initialize this Extension DLL.  Otherwise,
  //  the CDynLinkLibrary object will not be attached to the
  //  Regular DLL's resource chain, and serious problems will
  //  result.

  new CDynLinkLibrary(Tempdll1DLL);
 }
 else if (dwReason == DLL_PROCESS_DETACH)
 {
  TRACE0("TEMPDLL1.DLL Terminating!/n");
  // Terminate the library before destructors are called
  AfxTermExtensionModule(Tempdll1DLL);
 }
 return 1;   // ok
}
LRESULT CALLBACK MouseProc(int nCode,WPARAM wParam,LPARAM lParam)
{
nexthook=CallNextHookEx(myhook,nCode,wParam,lParam);
mymouse=(MOUSEHOOKSTRUCT*)lParam;
hWND=WindowFromPoint(mymouse->pt);
PostMessage(hWnd,WM_MOUSEHOOK,(DWORD)hWND,0);
return nexthook;
}

void InstallHook(HWND hwnd)
{
hWnd=hwnd;
myhook=SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseProc,hInstance,NULL);
}

void UninstallHook()
{
UnhookWindowsHookEx(myhook);
}

 

*.DEF 文件

; tempdll1.def : Declares the module parameters for the DLL.

LIBRARY      "tempdll1"
DESCRIPTION  'tempdll1 Windows Dynamic Link Library'

EXPORTS
    ; Explicit exports can go here
InstallHook @1
UninstallHook @2

______________________________________________________________________________________________________________

其中的资源句柄中的资源自己编写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值