gpt4 book ai didi

c# - CallWndProc 示例

转载 作者:太空宇宙 更新时间:2023-11-03 21:44:31 25 4
gpt4 key购买 nike

这是我第一次尝试使用hooks

我正在寻找一些很好的资源来实现 CallWndProc Hook 。 MSDN 的东西有点让人不知所措。

我发现使用这种类型的钩子(Hook)需要注入(inject)一个外部 dll。这主要是我被困的地方。

不确定 dll 中需要什么以及 .NET 应用程序中需要什么。

有dll例子吗?

最佳答案

您不能用 C# 等托管语言编写 WH_CALLWNDPROC Hook 。因此,您不仅需要一个外部 DLL,还需要一个用可编译为 native 代码的语言(如 C 或 C++)编写的外部 DLL。

The MSDN documentation其实还不错,尤其是overview . Using Hooks 上什至有一个例子页面。

我并不是要听起来令人沮丧,但如果您觉得难以抗拒,那么您将很难让它正常工作。钩子(Hook)是 Windows 编程中非常先进的技术。在开始像这样的项目之前,您需要了解窗口过程、消息循环和 Windows 应用程序的其他基础知识。熟悉 C 或 C++ 语言显然也有帮助,因为这就是您将要使用的语言!

反正我手边正好有一个用C写的hook DLL,所以我会试着把一些相关的代码拉出来。它实际上安装了一个 WH_CALLWNDRETPROC Hook ,但两者非常相似。 窗口过程处理完消息后调用这个钩子(Hook)过程;您正在谈论的那个在窗口过程处理消息之前被调用。

/* The handle to the hook is stored as a shared global variable and is the
* same for all hooked processes. We achieve that by placing it in the
* shared data segment of the DLL.
*
* Note that shared global variables must be explicitly initialized.
*
* And also note that this is really not the ideal way of doing this; it's just
* an easy way to get going. The better solution is to use a memory-mapped file.
* See Also: http://msdn.microsoft.com/en-us/library/h90dkhs0.aspx
*/
#pragma comment(linker, "/section:.SHARED,rws")
#pragma data_seg(".SHARED") /* begin the shared data segment */
HHOOK g_hhkCallWndProcRet = NULL;
#pragma data_seg() /* end the shared data segment and default back to normal behavior */


LRESULT CALLBACK CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
{
/* If nCode is greater than or equal to HC_ACTION,
* we should process the message. */
if (nCode >= HC_ACTION)
{
/* Retrieve a pointer to the structure that contains details about
* the message, and see if it is one that we want to handle. */
const LPCWPRETSTRUCT lpcwprs = (LPCWPRETSTRUCT)lParam;
switch (lpcwprs->message)
{
/* ...SNIP: process the messages we're interested in ... */
}
}

/* At this point, we are either not processing the message
* (because nCode is less than HC_ACTION),
* or we've already finished processing it.
* Either way, pass the message on. */
return CallNextHookEx(g_hhkCallWndProcRet, nCode, wParam, lParam);
}


BOOL __stdcall InstallHook(void)
{
/* Try to install the WH_CALLWNDPROCRET hook,
* if it is not already installed. */
if (!g_hhkCallWndProcRet)
{
g_hhkCallWndProcRet = SetWindowsHookEx(WH_CALLWNDPROCRET,
CallWndRetProc,
g_hinstDLL,
0);
if (!g_hhkCallWndProcRet)
{
/* ...SNIP: handle failure condition ... */
return FALSE;
}
}

return TRUE; /* return success */
}

BOOL __stdcall RemoveHook(void)
{
/* Try to remove the WH_CALLWNDPROCRET hook, if it is installed. */
if (g_hhkCallWndProcRet)
{
if (!UnhookWindowsHookEx(g_hhkCallWndProcRet))
{
/* ...SNIP: handle failure condition ... */
return FALSE;
}
g_hhkCallWndProcRet = NULL;
}

return TRUE; /* return success */
}

关于c# - CallWndProc 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17747345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com