注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

且行且记录

点滴记录,行的更远!

 
 
 

日志

 
 

远程消息钩子  

2013-03-28 14:19:35|  分类: 一些练习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

折腾了好多天,终于把远程消息钩子弄成功了!雏形已经弄出来了,原理也清楚了。

但还有些问题,比如多次运行时,结束掉其中一个就都没反应了。这大概要做同步处理。

以后再考虑了。我的目的只是想看到远程消息钩子的效果。

包括两个模块,一个dll模块,用于实现远程钩子;一个exe模块,用于安装和显示。

代码如下:

1、DLL模块

// remotehook.c

#include <windows.h>
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")

#pragma data_seg("MYDATA")
HHOOK g_hHook=NULL;
HWND g_hWnd=NULL;
HANDLE g_hDll=NULL;
#pragma data_seg()

#pragma comment(linker,"/SECTION:MYDATA,RWS")

__declspec(dllexport) LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    CHAR szBuf[128];
    int cch;
 HDC hDC;
 HWND hCurWnd;
 
    if (nCode < 0)
 {
        return CallNextHookEx(g_hHook, nCode, wParam, lParam);
 }

 hDC=GetDC(g_hWnd);

 hCurWnd = WindowFromPoint(((MOUSEHOOKSTRUCT*)lParam)->pt);

 cch = GetWindowText(hCurWnd,szBuf,120);
 TextOut(hDC,0,0,szBuf,cch);

    cch = wsprintf(
  szBuf,
  "MOUSE - nCode: %d, x: %d, y: %d",
  nCode,
  ((MOUSEHOOKSTRUCT*)lParam)->pt.x,
  ((MOUSEHOOKSTRUCT*)lParam)->pt.y
  );

 //OutputDebugString(szBuf);

 TextOut(hDC,0,20,szBuf,cch);
 ReleaseDC(g_hWnd,hDC);

    return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}

__declspec(dllexport) BOOL HookOn(HWND hWnd,HINSTANCE hinstDll)
{
 if (hinstDll==NULL) return FALSE;

 g_hDll=hinstDll;

 if (g_hWnd==NULL && hWnd !=NULL)
 {
  g_hWnd=hWnd;
 }
 else
 {
  return FALSE;
 }

 if (!g_hHook)
 {
  g_hHook = SetWindowsHookEx(WH_MOUSE,MouseProc,(HINSTANCE) g_hDll, 0);
  if (g_hHook==NULL)
  {
   return FALSE;
  }
 }
 //OutputDebugString("Hook OK!"); 
 return TRUE;
}

__declspec(dllexport) void HookOff(void)
{
 if (g_hHook)
 {
  UnhookWindowsHookEx(g_hHook);
  g_hHook=NULL;
  //OutputDebugString("UnHook OK!");
 }
 if (g_hWnd)
 {
  g_hWnd=NULL;
 }

}


BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    switch (ul_reason_for_call)
 {
  case DLL_PROCESS_ATTACH:
  case DLL_THREAD_ATTACH:
  case DLL_THREAD_DETACH:
  case DLL_PROCESS_DETACH:
   break;
    }
    return TRUE;
}

2、应用模块

// hookmouse.c

#include <windows.h>
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"gdi32.lib")

HHOOK hhook;
HWND hwndMain;
HINSTANCE hDll;
typedef BOOL (*MYON)(HWND,HINSTANCE);
MYON ProcHookOn;
typedef void (*MYOFF)(void);
MYOFF ProcHookOff;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
 static TCHAR szAppName[] = TEXT ("HOOKTEST") ;
 HWND   hwnd ;
 MSG    msg ;
 WNDCLASS wndclass ;
 wndclass.style        = CS_HREDRAW | CS_VREDRAW ;
 wndclass.lpfnWndProc  = WndProc ;
 wndclass.cbClsExtra   = 0 ;
 wndclass.cbWndExtra   = 0 ;
 wndclass.hInstance    = hInstance ;
 wndclass.hIcon        = LoadIcon (NULL, IDI_APPLICATION) ;
 wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW) ;
 wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
 wndclass.lpszMenuName  = NULL ;
 wndclass.lpszClassName= szAppName ;


 if (!RegisterClass (&wndclass))
 {
  MessageBox (NULL, TEXT ("This program requires Windows NT!"),
    szAppName, MB_ICONERROR);
  return 0;
 }
       
    hwnd = CreateWindow( szAppName,      // window class name
   TEXT ("HOOK TEST"),   // window caption
   WS_OVERLAPPEDWINDOW,  // window style
   CW_USEDEFAULT,// initial x position
   CW_USEDEFAULT,// initial y position
   CW_USEDEFAULT,// initial x size
   CW_USEDEFAULT,// initial y size
   NULL,                 // parent window handle
   NULL,            // window menu handle
   hInstance,   // program instance handle
   NULL);      // creation parameters


 ShowWindow (hwnd, iCmdShow);

 UpdateWindow (hwnd);

 while (GetMessage (&msg, NULL, 0, 0))
 {
  TranslateMessage (&msg);
  DispatchMessage (&msg);
 }

 return msg.wParam;
}
       

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;

    switch (message)
    {
    case WM_CREATE:
  hwndMain=hwnd;

  hDll=LoadLibrary(".\\remotehook.dll");
  if (hDll==NULL) return 0;
  OutputDebugString("Load DLL OK!");
  ProcHookOn = (MYON) GetProcAddress(hDll, "HookOn");
  ProcHookOff = (MYOFF) GetProcAddress(hDll, "HookOff");

  if (ProcHookOn==NULL || ProcHookOff==NULL)
  {
   OutputDebugString("Get Proc Failed!");
   return 0;
  }
  if (ProcHookOn(hwndMain,hDll))
  {
   OutputDebugString("HOOKON!");
  }
        return 0;

    case WM_PAINT:
  hdc = BeginPaint (hwnd, &ps);
  GetClientRect (hwnd, &rect);
  DrawText (hdc, TEXT ("HOOK TEST!"), -1, &rect,
   DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  EndPaint (hwnd, &ps);
  return 0 ;
       
 case WM_CHAR:
  break;

 case WM_DESTROY:
  if (ProcHookOff) ProcHookOff();

  if (hDll) FreeLibrary(hDll);
  PostQuitMessage (0);
  return 0;

    }

 return DefWindowProc (hwnd, message, wParam, lParam) ;

}

  评论这张
 
阅读(256)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017