1

您好,提前感谢任何愿意提供帮助的人。我正在尝试设置一个 CBT windows 挂钩,当我在全局设置它时效果很好,但每当我尝试将它附加到单个线程时都会失败。据我所知,我按书行事: - 我从非托管 dll 中暴露了钩子过程 - 我的应用程序、dll 和线程的进程都是 32 位的 - 我使用的线程 ID 是正确的(用 spy++ 确认)

当我试图只从 C++ 代码中挂钩一个线程时,我设法做到了……你可以只从非托管代码中挂钩一个线程吗?

无论如何,这是我的代码:

[DllImport( "user32.dll", SetLastError = true )]
    static extern IntPtr SetWindowsHookEx ( int hookType, UIntPtr lpfn, IntPtr hMod, uint dwThreadId );

[DllImport( "kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true )]
    public static extern UIntPtr GetProcAddress ( IntPtr hModule, string procName );

[DllImport( "kernel32", SetLastError = true, CharSet = CharSet.Unicode )]
    public static extern IntPtr LoadLibrary ( string libraryName );

const int WH_CBT = 5;

    void SetHook ()
    {
        IntPtr dll = LoadLibrary( LIBRARY );
        UIntPtr proc = GetProcAddress( dll, PROC );
        uint threadId = GetAppWindowThreadId();
         //assume that the threadId of the external window is correct, as I said I verified with spy++
         //and assume that dll and proc both get correct values
        IntPtr hookAddress = SetWindowsHookEx( WH_CBT , proc, dll, threadId );
         //hookAddress is 0
    }
4

1 回答 1

3
[DllImport( "user32.dll", SetLastError = true )]
static extern IntPtr SetWindowsHookEx ( int hookType, UIntPtr lpfn, 
                                        IntPtr hMod, ulong dwThreadId );

那个声明是错误的。最后一个参数 (dwThreadId) 的类型是 DWORD,在 C# 中是一个 uint。

奇怪的是,您没有收到 PInvokeStackImbalance 调试器警告。这表明您在 64 位操作系统上运行。这增加了几种额外的故障模式,您注入的 DLL 必须包含编译为您的进程和要挂钩的进程的正确位数的非托管代码。根据需要设置项目的平台目标。您的代码缺少所有错误检查,因此您不知道为什么它不起作用,请确保在收到失败返回代码时抛出 new Win32Exception()。

于 2011-08-28T12:06:16.807 回答