こんにちは、助けようとする人に前もって感謝します。CBT Windows フックを設定しようとしています。これは、グローバルに設定するとうまく機能しますが、単一のスレッドにアタッチしようとすると失敗します。私が知る限り、私は本ですべてをやっています: - アンマネージ dll からフック プロシージャを公開しました - 私のアプリケーション、dll、およびスレッドのプロセスはすべて 32 ビットです - 私が使用するスレッド ID は正しいです (spy++ で確認)
C++ コードからスレッドを 1 つだけフックしようとしたところ、なんとか成功しました...アンマネージ コードからスレッドを 1 つだけフックできますか?
とにかくここに私のコードがあります:
[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
}