C# Hook鼠标消息

在用C#做一个游戏的脚本,要控制鼠标。在脚本允许的时候,用户乱动鼠标会影响脚本的正常操作,故需要屏蔽掉用户对游戏的鼠标。

之前对hook也不是很了解,在网上搜了一圈,都是hook全局鼠标的,自己试验,全局也hook成功了,在对窗口hook时,怎么都不成功·····

再搜,了解到全局hook,不需要把代码放到线程内执行,对窗口hook,是把自己的代码放到窗口的线程内执行,所以,需要用C++写成DLL···

放代码:

 

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>

//-------------------------------------------------------------
// global variables (unshared!)
//

HHOOK g_hHook = 0;

 

//-------------------------------------------------------------
// HookProc
// Notice:
// - executed by the instance of "HookInjEx.dll" mapped into "explorer.exe";
//
// When called from InjectDll:
// - sublasses the start button;
// - removes the hook, but the DLL stays in the remote process
// though, because we increased its reference count via LoadLibray
// (this way we disturb the target process as litle as possible);
//
// When called from UnmapDll:
// - restores the old window procedure for the start button;
// - reduces the reference count of the DLL (via FreeLibrary);
// - removes the hook, so the DLL is unmapped;
//
// Also note, that the DLL isn't unmapped immediately after the
// call to UnhookWindowsHookEx, but in the near future
// (right after finishing with the current message).
// Actually it's obvious why: windows can NOT unmap the
// DLL in the middle of processing a meesage, because the code
// in the hook procedure is still required.
// That's why we can change the order LoadLibrary/FreeLibrary &
// UnhookWindowsHookEx are called.
//
// FreeLibrary, in contrast, unmapps the DLL imeditaley if the
// reference count reaches zero.
//
#define pCW ((CWPSTRUCT*)lParam)

LRESULT HookProc(
int code, // hook code
WPARAM wParam, // virtual-key code
LPARAM lParam // keystroke-message information
)
{
return 1;
return ::CallNextHookEx(g_hHook, code, wParam, lParam);

}


//-------------------------------------------------------------
// InjectDll
// Notice:
// - injects "HookInjEx.dll" into "explorer.exe" (via SetWindowsHookEx);
// - subclasses the START button (see HookProc for more details);
//
// Parameters: - hWnd = START button handle
//
// Return value: 1 - success;
// 0 - failure;
//
extern "C" __declspec(dllexport) int InjectDll(char *str)
{
HINSTANCE hDll = ::GetModuleHandle("Win32Project2.dll"); //获取dll的句柄

HWND g_hWnd = FindWindow(NULL, str); //查找窗口
if (g_hWnd > 0)
{
g_hHook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)HookProc,
hDll, GetWindowThreadProcessId(g_hWnd, NULL));

if (g_hHook == NULL) //失败
{
return 0;
}

}
else //未找到窗口
{
return 0;
}

return 1;
}


//-------------------------------------------------------------
// UnmapDll
// Notice:
// - restores the old window procedure for the START button;
// - unmapps the DLL from the remote process
// (see HookProc for more details);
//
// Return value: 1 - success;
// 0 - failure;
//
extern "C" __declspec(dllexport) int UnmapDll()
{

if (UnhookWindowsHookEx(g_hHook))
{
return 1;
}
else
{
return 0;
}
}

 

C#导入调用即可

转载于:https://www.cnblogs.com/blograin/p/6396621.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值