473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

my hook can't work well,help me!

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)Keyboar dProc,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)HookP roc,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;

//这是钩子的实现代码
Jul 22 '05 #1
1 3329

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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Hans Kiehn | last post by:
Hi *, I'm debugging a crash in a very big and complex server application. This server run's fine for days and hours and suddenly crashes. I've spent 2 weeks of time now, but cannot find the...
1
by: Michael Roper | last post by:
Well, actually I'm not sure that's what I need. What I'd like to do is write an app for the tray that can monitor any app that gets launched and do a 'window size and position' on it based on...
3
by: Omar | last post by:
Hi!! How can i make a global keyboard hook in c#?? thanks...
5
by: Arnold | last post by:
Hi, I want to make a Global CBT Hook, but the CallBack Method is never raised. SetWindowsHookEx return me a Handle to the Hook, but when i try to uninstall the hook the UnhookWindowsHookEx...
1
by: murlbrown | last post by:
Basicly im trying to determine when a new line has been appended to the textbox of another application. Using spy++ I see that the msg (wm_user + 97) is being sent, which is the EM_SETTEXTEX...
3
by: june | last post by:
Hi, I am coding for an application with dialog window. I need intercept mouse input. I need catch raw input, pretty much everything for WM_INPUT: such as Left/Middle/Right button down/up,...
7
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
0
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form...
22
by: schneider | last post by:
I need to hook the system mouse down event. I'm trying to replicate how a context menu hides when the mouse clicks outside of the control. Thanks, Schneider
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.