Global Hooks and .NET dll / Assembly

Hello,

I want to write a program which detects and monitors other running/starting/stopping programs. To do so I know I need a global windows hook. To install a global hook I need to load a dll, which is mentioned as a normal dll and not a .NET dll (I've read there's a difference, but I don't know what exactly). So I'm wondering is whether this will work when writing a .NET dll / assembly and loading that one and, if possible, how to do it.

I'm new to .NET and C#, so any help would be appreciated on this matter.

Alexander



Answer this question

Global Hooks and .NET dll / Assembly

  • Mark Wetterau

    Is there another way to detect realtime application start and stop in C#

  • Sarahli

    For (most) global hooks, you need to use unmanaged code because you need a C-style DLL export that can be pointed to, which is not supported by the .NET Framework. Theoretically you could write an unmanaged thunk in Managed C++ that translated between a C-style DLL export and a delegate implemented in C#. I've never tried this. So I might be missing something obvious why this wouldn't work.
     
    This Microsoft KB article has this to say about the subject:

    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.


  • Global Hooks and .NET dll / Assembly