|
-
May 15th, 2012, 07:02 AM
#1
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);
}
-
May 15th, 2012, 07:41 AM
#2
Re: What does this source code do?
Where did you get/find it?
Does it work?
Victor Nijegorodov
-
May 15th, 2012, 07:50 AM
#3
Re: What does this source code do?
 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.
-
May 15th, 2012, 07:56 AM
#4
Re: What does this source code do?
 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!
Victor Nijegorodov
-
May 15th, 2012, 11:51 PM
#5
Re: What does this source code do?
 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
-
May 21st, 2012, 01:57 AM
#6
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?
-
May 21st, 2012, 03:26 AM
#7
Re: What does this source code do?
 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.
 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?
-
May 21st, 2012, 03:31 AM
#8
Re: What does this source code do?
 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.
-
May 21st, 2012, 04:32 AM
#9
Re: What does this source code do?
 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.
-
May 21st, 2012, 05:07 AM
#10
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?
-
May 21st, 2012, 09:52 AM
#11
Re: What does this source code do?
 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.
Victor Nijegorodov
-
May 21st, 2012, 03:07 PM
#12
Re: What does this source code do?
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.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|