C# Detect clipboard paste WITH HOOK

核能气质少年 提交于 2019-12-12 05:26:21

问题


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.

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

MSDN says:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/44996734/c-sharp-detect-clipboard-paste-with-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!