What does this source code do?
I am still unable to understand how this source code extracts text from dialog box controls within a given rectangle boundry of a 3rd party application?
Code:
HHOOK hHook = NULL;
TCHAR szBuff[2048]; // any buffer
CString ScrapeText(HWND hWnd, RECT& rect)
{
hHook = SetWindowHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, AfxGetApp()->GetCurrentInstance(), GetCurrentThread);
RedrawWindow(hWnd, (const RECT*)&rect, NULL,RDW_ALLCHILDREN);
// unhook after processing
UnHookWindowsEx(hHook);
return szBuff; // after concactenation from hook procedure
}
LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam )
{
if( nCode < 0 )
return CallNextHookEx( hHook, nCode, wParam, lParam);
MSG* pMsg = (MSG*)lParam;
switch( nCode )
{
case HC_ACTION:
if( pMsg->message == WM_SETTEXT && CLASSOF(pMsg->hWnd) ){
_tcscat(szBuff, (LPCTSTR)lParam);
}
break;
default:
break;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
Re: What does this source code do?
Where did you get/find it?
Does it work?
Re: What does this source code do?
Quote:
Originally Posted by
VictorN
Where did you get/find it?
Does it work?
It was provided by one of the client's (the one who asked for this initial requirement) developer and yes this code is having some errors.
Re: What does this source code do?
Quote:
Originally Posted by
maverick786us
It was provided by one of the client's (the one who asked for this initial requirement) developer and yes this code is having some errors.
Then fix these errors or ask "one of the client's" to fix them, and also ask him about the main purpose of this code!
Re: What does this source code do?
Quote:
Originally Posted by
maverick786us
I am still unable to understand how this source code extracts text from dialog box controls within a given rectangle boundry of a 3rd party application?
Code:
HHOOK hHook = NULL;
TCHAR szBuff[2048]; // any buffer
CString ScrapeText(HWND hWnd, RECT& rect)
{
hHook = SetWindowHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, AfxGetApp()->GetCurrentInstance(), GetCurrentThread);
RedrawWindow(hWnd, (const RECT*)&rect, NULL,RDW_ALLCHILDREN);
// unhook after processing
UnHookWindowsEx(hHook);
return szBuff; // after concactenation from hook procedure
}
LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam )
{
if( nCode < 0 )
return CallNextHookEx( hHook, nCode, wParam, lParam);
MSG* pMsg = (MSG*)lParam;
switch( nCode )
{
case HC_ACTION:
if( pMsg->message == WM_SETTEXT && CLASSOF(pMsg->hWnd) ){
_tcscat(szBuff, (LPCTSTR)lParam);
}
break;
default:
break;
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
The code is trying to install an application level hook and then redraw the particular window, to filter WM_SETTEXT message for it.
For further details, take a look at SetWindowsHookEx
Re: What does this source code do?
Thanks. Just one last question.
1) What is the purpose of redraw window in this scenerio? Is it necessary for the child window approach to work?
2) Only a small number of windows return any text from WM_GETTEXT. Is there another way? Some technique that DOES repainting on the screen?
Re: What does this source code do?
Quote:
Originally Posted by
maverick786us
1) What is the purpose of redraw window in this scenerio? Is it necessary for the child window approach to work?
It is trying to kick the particular window so that WM_SETTEXT message against the particular area (i.e. passed in rect), can be filtered.
I haven't used hooks for long time, therefore please check it at your end.
Quote:
Originally Posted by maverick786us
2) Only a small number of windows return any text from WM_GETTEXT. Is there another way? Some technique that DOES repainting on the screen?
Again, I haven't done this myself, but I think it will take more then simple WM_GETTEXT, especially when it comes to non-text static controls (such as images, icons etc). Check the remarks section for WM_GETTEXT in MSDN.
I would like to ask here is that what exactly you want to achieve here?
Re: What does this source code do?
Quote:
Originally Posted by
Ejaz
It is trying to kick the particular window so that WM_SETTEXT message against the particular area (i.e. passed in rect), can be filtered.
I haven't used hooks for long time, therefore please check it at your end.
Again, I haven't done this myself, but I think it will take more then simple WM_GETTEXT, especially when it comes to non-text static controls (such as images, icons etc). Check the remarks section for WM_GETTEXT in MSDN.
I would like to ask here is that what exactly you want to achieve here?
I want extract text from an existing Dialog Window? Its a 3rd party dialog window so I don't have the source code of it. I have to extract text from its Lables, Text box, radiobuttons, checkboxes etc.
Re: What does this source code do?
Quote:
Originally Posted by
maverick786us
I want extract text from an existing Dialog Window? Its a 3rd party dialog window so I don't have the source code of it. I have to extract text from its Lables, Text box, radiobuttons, checkboxes etc.
If this is what you want, then I think you need to enumerate the top level window first and all the child windows subsequently. For each window, use GetClassName() API first and see what kind of window you are dealing with, so that you can choose more appropriate method to get the details out of it that you want.
Take a look at this link and try the sample to enumerate the windows.
Re: What does this source code do?
I already have the handle of that window plus the rectangular region whose area I have to extract text.
How does RedrawWindow calls WM_GETTEXT internally?
Re: What does this source code do?
Quote:
Originally Posted by
maverick786us
How does RedrawWindow calls WM_GETTEXT internally?
It does not. It only causes window to redraw. See MSDN for details.
Besides, the code snippet you showed cannot help you since it only applies to the current process. To hook another process you have to place the hook code ina DLL. Again: see MSDN for details.
PS: there may be additional security problems in Vista and above.
Re: What does this source code do?
Quote:
How does RedrawWindow calls WM_GETTEXT internally?
It doesn't. You have to get accustomed with the thought that something may differ from what it looks like for the first glance.
My impression is that it's called just to force the dll injection, and the text extraction itself occurs inside the hook code.