プログラムのWindowsフックを外部EXEに設定しようとしています。これは、ウィンドウのサイズ変更/最小化を監視するために使用されるため、ウィンドウにドッキングして、プログラムのサイズを同様に変更できます。
以下のエラーコード1428および126を回避するにはどうすればよいですか?
nullのhModを使用してSetWindowsHookExを呼び出すと、このエラーが発生していました。(IntPtr.Zeroの代わりに)現在のモジュールを渡すと同じエラーが発生しますが、これは正しく取得されているようです。
IntPtr module = PInvoke.GetModuleHandle(null);
[...]
SetWindowsHookEx(...,...,module,...);
int error = PInvoke.GetLastError();
1428 =モジュールハンドルなしで非ローカルフックを設定することはできません
また、 GetModuleHandleを使用して、モジュールとしてフックしている外部プログラムを取得しようとしました。
IntPtr module = PInvoke.GetModuleHandle("communicator.exe");
int error = PInvoke.GetLastError();
ただし、エラーは次のように設定されます。
126 =指定されたモジュールが見つかりませんでした。
次のPInvokeステートメントを使用しています。
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId);
これが問題を抱えている手順です:
public void Install(IntPtr hWnd)
{
uint threadId;
uint processId;
if (hWnd == IntPtr.Zero)
{
threadId = (uint)AppDomain.GetCurrentThreadId();
throw new Exception("Lync thread not found!");
}
else
{
threadId = PInvoke.GetWindowThreadProcessId(hWnd, out processId);
}
//IntPtr module = PInvoke.GetModuleHandle(null);
//IntPtr module = PInvoke.GetModuleHandle(GetType().Module.FullyQualifiedName);
IntPtr module = PInvoke.GetModuleHandle("communicator.exe");
int error = PInvoke.GetLastError();
m_hhook = PInvoke.SetWindowsHookEx(
m_hookType,
m_filterFunc,
//Process.GetCurrentProcess().Handle,
//threadId);
//IntPtr.Zero,
//module,
//Marshal.GetHINSTANCE(
// System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]
// ).ToInt32()
module,
threadId);
//IntPtr hinst = Marshal.GetHINSTANCE(Process.GetCurrentProcess().Handle);
// http://msdn.microsoft.com/en-us/library/ms681385
// ERROR_HOOK_NEEDS_HMOD - 1428 = Cannot set nonlocal hook without a module handle
error = PInvoke.GetLastError();
}