Hook消息的问题。我为什么只能钩到进程内的消息。别的进程的消息我收不到。

pp616 2003-02-12 03:13:59
我需要截取别的进程的消息。
写了个Dll。代码如下:
#pragma option -zRSHSEG
#pragma option -zTSHCLASS
#include <windows.h>

#define WM_MYHOOK WM_USER + 1234

#pragma argsused

HHOOK NewHook=NULL;
int LoadCount=0;
HINSTANCE DllHinst=NULL;
HANDLE m_hwnd=NULL;

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
DllHinst=hinst;
if(reason==DLL_PROCESS_ATTACH)
LoadCount+=1;
else if(reason==DLL_PROCESS_DETACH)
LoadCount-=1;
return 1;
}


LRESULT WINAPI MsgHook(int nCode,WPARAM wParam,LPARAM lParam)
{
MSG *msg;
if (nCode==HC_ACTION)
{
msg=(MSG *)lParam;
switch(msg->message)
{
case(...)
......
}

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

extern "C" __declspec(dllexport)
void EnableHook(HANDLE mHandle)
{
if (NewHook==NULL)
{
m_hwnd=mHandle;
NewHook=SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)MsgHook,DllHinst,NULL);
}
}

extern "C" __declspec(dllexport)
void DisableHook()
{
if (NewHook!=NULL)
{
UnhookWindowsHookEx(NewHook);
NewHook=NULL;
}
}

为什么我调用这个DLL。只能收到本进程内的消息。别的就收不到呢。是不是我的hook写的有问题。请各位指点一二
...全文
186 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
NowCan 2003-02-14
  • 打赏
  • 举报
回复
用SendMessage发的消息截获不到。
pp616 2003-02-14
  • 打赏
  • 举报
回复
我看过你的参考了。做shell的hook是可以的。但是message还是截不到啊。
penu 2003-02-13
  • 打赏
  • 举报
回复
可能要注入。
  • 打赏
  • 举报
回复
可以参考这个程序:
利用全局钩子得到在你的电脑中运行的程序历史记录,并且可拒绝运行你不想让运行的程序。

http://www.cppfans.com/articles/system/whshellhook.asp
Billy_Chen28 2003-02-13
  • 打赏
  • 举报
回复
好像生成的不是全局勾子
mustang_zr 2003-02-13
  • 打赏
  • 举报
回复
估计你是要拦截别的进程发给本进程的消息

WH_GETMESSAGE HOOK 的 hook procedure 触发的时机是当
GetMessage 和 PeekMessage 从消息队列中取得一个消息时

如果是别的进程SendMessage 这样消息不进消息队列 故拦截不到

用WH_CALLWNDPROC 或 WH_CALLWNDPROCRET
zzhong2 2003-02-13
  • 打赏
  • 举报
回复
mark
pp616 2003-02-13
  • 打赏
  • 举报
回复
不是啊。我写的鼠标和键盘都可以截获别的进程的鼠标和键盘动作。
但是我用WH_GETMESSAGE想截取别的进程的消息时。比如说截获别的进程的鼠标双击消息。就截获不到了。只能截获本进程内消息。
tccsdn 2003-02-13
  • 打赏
  • 举报
回复
帮你顶一下,学习
NowCan 2003-02-13
  • 打赏
  • 举报
回复
所以后来,我写hook都是用的FileMapping了。
NowCan 2003-02-13
  • 打赏
  • 举报
回复
你这个是BCB写hook的经典做法。
但这个程序的问题是hook停止后就不能再启动。
原因我曾经研究过。见下页
http://vip.6to23.com/NowCan1/tech/bcbhook.htm
Behard 2003-02-13
  • 打赏
  • 举报
回复
mark
chinayang 2003-02-12
  • 打赏
  • 举报
回复
看看SetWindowsHookEx這個API的用法吧.如果想收到別的進程的消息..這個Hook必須是全局.那麼SetWindowsHookEx中的最後一個參數就要為0.
***MSDN..
SetWindowsHookEx
The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the system.

HHOOK SetWindowsHookEx(
int idHook, // type of hook to install
HOOKPROC lpfn, // address of hook procedure
HINSTANCE hMod, // handle to application instance
DWORD dwThreadId // identity of thread to install hook for
);

Parameters
idHook
Specifies the type of hook procedure to be installed. This parameter can be one of the following values: Value Description
WH_CALLWNDPROC Installs a hook procedure that monitors messages before the system sends them to the destination window procedure. For more information, see the CallWndProc hook procedure.
WH_CALLWNDPROCRET Installs a hook procedure that monitors messages after they have been processed by the destination window procedure. For more information, see the CallWndRetProc hook procedure.
WH_CBT Installs a hook procedure that receives notifications useful to a computer-based training (CBT) application. For more information, see the CBTProc hook procedure.
WH_DEBUG Installs a hook procedure useful for debugging other hook procedures. For more information, see the DebugProc hook procedure.
WH_FOREGROUNDIDLE Installs a hook procedure that will be called when the application's foreground thread is about to become idle. This hook is useful for performing low priority tasks during idle time. For more information, see the ForegroundIdleProc hook procedure.
WH_GETMESSAGE Installs a hook procedure that monitors messages posted to a message queue. For more information, see the GetMsgProc hook procedure.
WH_JOURNALPLAYBACK Installs a hook procedure that posts messages previously recorded by a WH_JOURNALRECORD hook procedure. For more information, see the JournalPlaybackProc hook procedure.
WH_JOURNALRECORD Installs a hook procedure that records input messages posted to the system message queue. This hook is useful for recording macros. For more information, see the JournalRecordProc hook procedure.
WH_KEYBOARD Installs a hook procedure that monitors keystroke messages. For more information, see the KeyboardProc hook procedure.
WH_KEYBOARD_LL Windows NT: Installs a hook procedure that monitors low-level keyboard input events. For more information, see the LowLevelKeyboardProc hook procedure.
WH_MOUSE Installs a hook procedure that monitors mouse messages. For more information, see the MouseProc hook procedure.
WH_MOUSE_LL Windows NT: Installs a hook procedure that monitors low-level mouse input events. For more information, see the LowLevelMouseProc hook procedure.
WH_MSGFILTER Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. For more information, see the MessageProc hook procedure.
WH_SHELL Installs a hook procedure that receives notifications useful to shell applications. For more information, see the ShellProc hook procedure.
WH_SYSMSGFILTER Installs a hook procedure that monitors messages generated as a result of an input event in a dialog box, message box, menu, or scroll bar. The hook procedure monitors these messages for all applications in the system. For more information, see the SysMsgProc hook procedure.


lpfn
Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link library (DLL). Otherwise, lpfn can point to a hook procedure in the code associated with the current process.
hMod
Handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.
dwThreadId
Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads.
Return Values
If the function succeeds, the return value is the handle to the hook procedure.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks
An error may occur if the hMod parameter is NULL and the dwThreadId parameter is zero or specifies the identifier of a thread created by another process.

Calling the CallNextHookEx function to chain to the next hook procedure is optional, but it is highly recommended; otherwise, other applications that have installed hooks will not receive hook notifications and may behave incorrectly as a result. You should call CallNextHookEx unless you absolutely need to prevent the notification from being seen by other applications.

Before terminating, an application must call the UnhookWindowsHookEx function to free system resources associated with the hook.

The scope of a hook depends on the hook type. Some hooks can be set only with system scope; others can also be set for only a specific thread, as shown in the following list:

Hook Scope
WH_CALLWNDPROC Thread or system
WH_CALLWNDPROCRET Thread or system
WH_CBT Thread or system
WH_DEBUG Thread or system
WH_FOREGROUNDIDLE Thread or system
WH_GETMESSAGE Thread or system
WH_JOURNALPLAYBACK System only
WH_JOURNALRECORD System only
WH_KEYBOARD Thread or system
WH_KEYBOARD_LL Thread or system
WH_MOUSE Thread or system
WH_MOUSE_LL Thread or system
WH_MSGFILTER Thread or system
WH_SHELL Thread or system
WH_SYSMSGFILTER System only


For a specified hook type, thread hooks are called first, then system hooks.

The system hooks are a shared resource, and installing one affects all applications. All system hook functions must be in libraries. System hooks should be restricted to special-purpose applications or to use as a development aid during application debugging. Libraries that no longer need a hook should remove the hook procedure.

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧