Hi all,
I am using a system hook to insert a callback function which intercepts a window message (when the user open an IE window). But I have the problem.
int MSAAScene::initHook(){
HWND hWndMainWindow;
CoInitialize(NULL);
/*Check if the Target Window is already opened (If NULL - set WinEvent hook to detect
the moment when the target window is created; If Found - it posts the message
WM_TARGET_WINDOW_FOUND to inform the main thread that the target window is available.*/
MessageBox (NULL, "In the WHILE (1)", "FIRST", NULL);
if(NULL == (hWndMainWindow = FindWindow(szClassName, NULL)))
{
hEventHook = SetWinEventHook(
EVENT_MIN,
EVENT_MAX, //end event id
NULL, //always NULL for outprocess hook
WinEventProc,
0, //hooked into
0, //idThread, //hooked into
//always the same for outproc hook
WINEVENT_SKIPOWNPROCESS | WINEVENT_OUTOFCONTEXT);
}
else
{
MessageBox(NULL, "Found the Window!","NOT IN EVENT HOOK", MB_OK);
PostThreadMessage(GetCurrentThreadId(), WM_TARGET_WINDOW_FOUND, 0, 0);
}
char sp[256];
MSG msg;
BOOL bRet;
while (bRet = (GetMessage (&msg, NULL, 0, 0)) > 0)
{
MessageBox(NULL, "In the 1st Line of the WHILE Loop", "WHILE", MB_OK);
if(msg.message == WM_TARGET_WINDOW_FOUND)
{
if(hEventHook)
{
UnhookWinEvent(hEventHook);
hWndMainWindow = FindWindow(szClassName, NULL);
}
// Bring the window to foreground
SetForegroundWindow(hWndMainWindow);
// Returns the pointer to IAccessible that represents the found window
if(S_OK == (hr = AccessibleObjectFromWindow(hWndMainWindow,
OBJID_WINDOW, IID_IAccessible,(void**) &paccMainWindow)))
{
//TO DO
paccMainWindow->Release();
} //end AccessibleObjectFromWindow
//return 1;
} //end if(msg = Found)
TranslateMessage (&msg);
DispatchMessage (&msg); // Should be going back to the CALLBACK function
//return 1;
} //end WHILE Loop
return 0;
}
The problem I get is, it seems to be only go through the while loop for GetMessage once (where it shows the MessageBox only once. But the programme is still running after that but nothing happen no matter what I do. Another thing is that, you can see I make two "return 1" into comments, since as far as I inderstand, with the return command, it will leave the entire function directly. So I was using try-and-error method to see if it works if I cancel the return.
Could anyonepoint out to me where I get it wrong? Why the while loop for GetMessage only goes for once (whilst the programme still running until I terminate it using Tast Manager)? Do I need those return command?
Look forward to hearing from any of you!
Thanks in advance,
S.