my hook can't work well,help me!

A

ayiiq180

my hook already in a dll and the handle is shared,but the hook cant
work well,when i run the application,My mouse click the application's
view,the hook work well,but when i click the other place(like
taskbar),it cant work,why?help me.
this is the hook:
// HookDll.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "HookDll.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WM_MAP_OPEN WM_USER+101
#define WM_DATA_READY WM_USER+102
#pragma data_seg("Shared")
static HHOOK hms=NULL;//系统钩子
HINSTANCE hins=NULL;
#pragma data_seg()
#pragma comment(linker, "/section:Shared, rws")

HANDLE hMapFile;//内存数据共享区句柄
LPVOID lpData;
HWND Phwnd;//父窗口句柄


/////////////////////////////////////////////////////////////////////////////
// CHookDllApp

BEGIN_MESSAGE_MAP(CHookDll, CWinApp)
//{{AFX_MSG_MAP(CHookDllApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHookDllApp construction
//回调函数
LRESULT __declspec(dllexport)__stdcall CALLBACK HookProc(int
nCode,WPARAM wParam,LPARAM lParam)
{
PMSG pmsg=(PMSG)lParam;
if (nCode==HC_ACTION) {

switch(pmsg->message)
{
case WM_KEYDOWN:
//处理键盘按键按下消息,拷贝数据到内存共享区中
{
char KeyName[50];
ZeroMemory(KeyName,50);
GetKeyNameText(pmsg->lParam,KeyName,50);
CString str;
str="KeyPress(" + CString(KeyName) + ");";
// 数据复制到共享内存
//首先清空内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据

::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
case WM_KEYUP:
//处理键盘按键释放消息
{
char KeyName[50];
ZeroMemory(KeyName,50);
GetKeyNameText(pmsg->lParam,KeyName,50);
CString str;
str="KeyUp(" + CString(KeyName)+");";
// 数据复制到共享内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据

::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
case WM_SYSKEYDOWN:
//处理用户按下alt的同时按下其他的键消息
{
char KeyName[50];
ZeroMemory(KeyName,50);
GetKeyNameText(pmsg->lParam,KeyName,50);
CString str;
str="SysKeyDown(" + CString(KeyName)+");";
// 数据复制到共享内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据

::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
case WM_SYSKEYUP:
//处理用户按下alt的同时按下其他的键消息
{
char KeyName[50];
ZeroMemory(KeyName,50);
GetKeyNameText(pmsg->lParam,KeyName,50);
CString str;
str="SysKeyUp(" + CString(KeyName)+");";
// 数据复制到共享内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据

::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
// case WM_CHAR:
//处理键盘按键消息
// break;
/* case WM_COMMAND:
//处理菜单选择消息
break;
case WM_MOUSEMOVE:
//处理鼠标移动消息
break;*/
case WM_LBUTTONDOWN:
//处理鼠标左键按下消息
{
double x,y;
x=LOWORD(pmsg->lParam);
y=HIWORD(pmsg->lParam);
CString strx,stry;
strx.Format("%f",x);
stry.Format("%f",y);
CString str;
str="MouseLbuttonDown("+strx+","+stry+");";
// 数据复制到共享内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据

::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
case WM_LBUTTONUP:
//处理鼠标左键释放消息
{
double x,y;
x=LOWORD(pmsg->lParam);
y=HIWORD(pmsg->lParam);
CString strx,stry;
strx.Format("%f",x);
stry.Format("%f",y);
CString str;
str="MouseLbuttonUp("+strx+","+stry+");";
// 数据复制到共享内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据
/*char* str1[100];
memcpy(str1, lpData, 100);*/
::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
case WM_LBUTTONDBLCLK:
//处理鼠标双击消息
{
double x,y;
x=LOWORD(pmsg->lParam);
y=HIWORD(pmsg->lParam);
CString strx,stry;
strx.Format("%f",x);
stry.Format("%f",y);
CString str;
str="MouseLbuttonDbClick("+strx+","+stry+");";
// 数据复制到共享内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据

::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
case WM_RBUTTONDOWN:
//处理鼠标右键按下消息
{
double x,y;
x=LOWORD(pmsg->lParam);
y=HIWORD(pmsg->lParam);
CString strx,stry;
strx.Format("%f",x);
stry.Format("%f",y);
CString str;
str="MouseRbuttonDown("+strx+","+stry+");";
// 数据复制到共享内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据

::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
case WM_RBUTTONUP:
//处理鼠标右键释放消息
{
double x,y;
x=LOWORD(pmsg->lParam);
y=HIWORD(pmsg->lParam);
CString strx,stry;
strx.Format("%f",x);
stry.Format("%f",y);
CString str;
str="MouseRbuttonUp("+strx+","+stry+");";
// 数据复制到共享内存
memset(lpData,NULL,100);
memcpy(lpData, str, str.GetLength());
// 通知接收方接收数据

::postMessage(Phwnd, WM_DATA_READY, (WPARAM)0,
(LPARAM)str.GetLength());
}
break;
/* case WM_SYSCOMMAND:
//处理选择系统菜单或者选择最大化最小化按钮消息
break;
*/
}

}

LRESULT RetVal = CallNextHookEx( hms, nCode, wParam, lParam );
return RetVal;
}
BOOL __declspec(dllexport)__stdcall installhook(HWND hwnd)
{
// hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);
Phwnd=hwnd;
//建立内存共享区

hMapFile=CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE | SEC_COMMIT, 0, 100, "DataMap");
if(hMapFile!=NULL)
{
lpData = (LPBYTE)MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, 0);
if (lpData == NULL)
{
CloseHandle(hMapFile);
hMapFile = NULL;
}
// 通知接收程序内存文件映射对象的视图已经打开
::postMessage(Phwnd, WM_MAP_OPEN, 0, 0);

}
hms=SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)HookProc,hins,0);
return TRUE;
}
BOOL __declspec(dllexport) Uninstallhook()
{
BOOL unhooked=TRUE;
if(hms!=NULL)
{
unhooked = UnhookWindowsHookEx(hms);
hms=NULL;
}
//释放内存共享区资源
if (lpData != NULL)
{
UnmapViewOfFile(lpData);
lpData = NULL;
}
if (hMapFile != NULL)
{
CloseHandle(hMapFile);
}

return unhooked;
}
CHookDll::CHookDll()
{
// TODO: add construction code here,

}
BOOL CHookDll::InitInstance()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
hins=AfxGetInstanceHandle();

return TRUE;
}
BOOL CHookDll::ExitInstance()
{
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CHookDllApp object

CHookDll theDll;

//这是钩子的实现代码
 
S

Sharad Kala

This question is off-topic on this ng. Try ngs on MS news server -
msnews.microsoft.com.
There is a lot of activity specially on microsoft.public.vc.language.

Best wishes,
Sharad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top