Pure C# Hook implementation
To implement system hooks, it's actually very simple, just call the three Win32 APIs. SetWindowsHookEx is used to set up hooks. (Set up a card to check the information you need)
CallNextHookEx is used to pass hooks (messages are important, so where they come from, they should go back to where you want to block them)
UnhookWindowsHookEx Uninstall hooks (uninstalling is very important, too many stuck settings will cause congestion)
The article "HOW TO: Setting Window Hooks in Visual C# .NET" describes it as follows: Global hooks are not supported in the .NET framework You can't implement global hooks in the Microsoft .NET framework. To install a global hook, the hook must have a native dynamic link library (DLL) export so that it can be inserted into another process that needs to be called into a valid and consistent function. This requires a DLL export, which is not supported by the .NET framework. The managed code doesn't have the concept of having a uniform value for function pointers, because these functions are dynamically constructed proxies. I found a lot of code on the Internet, and most of them contain a C++ DLL to identify the DLL containing the subroutine referred to by lpfn, which seems to validate this claim.
But in reality, this is not the case, and the global hook can be implemented using the following code: IntPtr pInstance = Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly(). ManifestModule ); Win32API.SetWindowsHookEx( WH_MOUSE_LL,m_MouseHookProcedure, pInstance, 0 ); Note: The ManifestModule property is a new addition in the .Net Framework 2.0, so when you still use . In Net Framework 1.x, you can use the GetModules method to get all the modules of the current assembly, and then use one of them as a parameter to the GetHINSTAN method to get the appropriate handle pointer.
|