C++全局钩子里添加了通过控件句柄获取控件标题的代码,在获取时程序死机
我添加了一段通过某一控件的句柄,获取应用程序中某一窗口中的该控件标题,可是当程序运行到获取标题时程序就死机了,请大家帮忙看看代码如下:
程序代码:
LRESULT CALLBACK ShellHookProc(int nCode,WPARAM wParam,LPARAM lParam) { if (nCode>=0)// Windows Created { const int MyMaxParentWinCount = 2; // 父窗口类名数组 char *A_szClassName[MyMaxParentWinCount] = { "#32770","Button" }; // 父窗口标题数组 char *A_szWinName[MyMaxParentWinCount] = { "询问","是(&Y)" }; // 首先求得顶级父窗口 HWND hLastWins =FindWindow(A_szClassName[0], A_szWinName[0]); HWND hLastWinss = FindWindowEx(hLastWins, 0, "Static", NULL); char title[200]; GetWindowText(hLastWinss, title, 200);//在这里死机 char *t=title; char *pattern="小于成本"; char *patter="小于最低销售价"; char *isMatch1= strstr(t,pattern); char *isMatch2= strstr(t,patter); if(title!=NULL) { if (isMatch1!=NULL||isMatch2!=NULL) { /*MessageBox((HWND)isMatch1,NULL,NULL,NULL);*/ // 逐次用FindWindowEx函数求出各级子窗口 for(int i=1; i<MyMaxParentWinCount; i++) { hLastWins = FindWindowEx(hLastWins, 0, "Button", "是(&Y)"); } EnableWindow(hLastWins, false); } } } return CallNextHookEx(g_hHookShell,nCode,wParam,lParam); } BOOL CQJHook1::SetHook() { g_hHookShell = SetWindowsHookEx(WH_CALLWNDPROC, ShellHookProc,glhInstance,0); return g_hHookShell ? TRUE : FALSE; } BOOL CQJHook1::DestroyHook() { if (g_hHookShell == NULL) { return FALSE; } UnhookWindowsHookEx(g_hHookShell); return TRUE; } CQJHook1::CQJHook1() //类构造函数 { } CQJHook1::~CQJHook1() { DestroyHook(); }