C#使用HOOK检测剪贴板粘贴 [英] C# Detect clipboard paste WITH HOOK

查看:542
本文介绍了C#使用HOOK检测剪贴板粘贴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用C#WinForms中的钩子在Windows上全局拦截Windows粘贴事件并根据目标进程进行阻止?

Is possible to globally intercept paste events on Windows and block depending on the target process, with a hook that works in C# WinForms Any CPU build?

我考虑过要在

I thought about putting a hook in GetClipboardData or wait for WM_PASTE messages.

我的第一次尝试是 SetWindowsHookEx ,但另一种外观令我沮丧.

My first attempt was SetWindowsHookEx, but another look discouraged me.

.NET Framework不支持全局挂钩 除了WH_KEYBOARD_LL低级挂钩和WH_MOUSE_LL低级挂钩,您无法在Microsoft .NET Framework中实现全局挂钩.若要安装全局挂钩,挂钩必须具有本机DLL导出,才能将其自身插入另一个需要有效,一致的函数才能调用的进程中.此行为需要DLL导出. .NET Framework不支持DLL导出.托管代码不具有用于函数指针的一致值的概念,因为这些函数指针是动态生成的代理.

Global hooks are not supported in the .NET Framework Except for the WH_KEYBOARD_LL low-level hook and the WH_MOUSE_LL low-level hook, you cannot implement global hooks in the Microsoft .NET Framework. To install a global hook, a hook must have a native DLL export to inject itself in another process that requires a valid, consistent function to call into. This behavior requires a DLL export. The .NET Framework does not support DLL exports. Managed code has no concept of a consistent value for a function pointer because these function pointers are proxies that are built dynamically.

推荐答案

没有检测粘贴事件的理想解决方案.就像RbMm所说的那样,仅当剪贴板中的内容更改时,才会发送WM_CLIPBOARDUPDATE. (复制或剪切). WM_DESTROYCLIPBOARD是在粘贴操作期间发送的,但前提是剪贴板中的内容是使用剪切操作添加的.

There is no ideal solution to detect paste event. Like RbMm said WM_CLIPBOARDUPDATE will be sent only when the content in the clipboard is changed. (copy or cut). WM_DESTROYCLIPBOARD is sent during a paste action but only if the content in the clipboard was added using a cut action.

但是您可以通过实现文件系统过滤器驱动程序来实现.

But you can achieve this by implementing a file system filter driver.

这篇关于C#使用HOOK检测剪贴板粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆