How to hook an application?

后端 未结 5 1166
情书的邮戳
情书的邮戳 2020-12-18 16:20

I\'m trying to hook the creation of a windows in my C# app.

static IntPtr hhook = IntPtr.Zero;
static NativeMethods.HookProc hhookProc;

static void Main(str         


        
5条回答
  •  温柔的废话
    2020-12-18 17:05

    One issue with your code is that hhookProc can be garbage collected while your native code still needs it. Use GC.KeepAlive or put in in a static variable.

    The hMod param should probably be null, since you specified a thread in your own process:

    hMod [in]

    Type: HINSTANCE

    A handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process.


    But I think this only works for windows on the thread you specify.

    In theory you can specify threads in other applications or even a global hook. The specified callback then gets called on the corresponding thread, even if that thread is in another process, in which case your dll gets injected into that process(that's the reason you need to specify the module handle in the first place).

    But I believe that's not possible with .net code, because the mechanism for injecting into other processes and calling the hook method over there doesn't work with JIT compiled code.

提交回复
热议问题