我正在构建一个应用程序使用钩子,我想启动另一个应用程序,在我构建我的应用程序之后,应用程序将获得另一个应用程序和 setwindowhook 的自动化窗口句柄。但我做不到。请帮助我:(。我的英语
当我得到所有应用程序时,变量窗口是 hwnd 是 windowform 正在运行
private bool AddWnd(int hwnd, int lparam)
{
if (IsWindowVisible(hwnd))
{
StringBuilder sb = new StringBuilder(255);
string className = GetClassName((IntPtr)hwnd).ToString();
if (className.Length > 10)
{
string getSubStringClassName = className.Substring(0, 11);
if (getSubStringClassName.Equals("WindowsForm") && IsWindow(hwnd)!=0)
{
GetWindowText(hwnd, sb, sb.Capacity);
window = hwnd;
}
}
}
return true;
}
HookProc HookProcedure;
private const int WH_CBT = 5;
public void startHook()
{
if (hHook == 0)
{
HookProcedure = new HookProc(CbtHookProc);
int threadID = GetWindowThreadProcessId((IntPtr)window, out processHandle);
IntPtr hMod = System.Runtime.InteropServices.Marshal.GetHINSTANCE(typeof(Form1).Module);
hHook = SetWindowsHookEx(WH_CBT, HookProcedure, hMod, threadID);
if (hHook == 0)
{
MessageBox.Show("SetWindowsHookEx Failed");
return;
}
}
else
{
bool ret = UnhookWindowsHookEx(hHook);
//If the UnhookWindowsHookEx function fails.
if (ret == false)
{
MessageBox.Show("UnhookWindowsHookEx Failed");
return;
}
hHook = 0;
button1.Text = "Set Windows Hook";
}
}