CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    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);
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: What does this source code do?

    Where did you get/find it?
    Does it work?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2005
    Posts
    1,828

    Re: What does this source code do?

    Quote Originally Posted by VictorN View Post
    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: What does this source code do?

    Quote Originally Posted by maverick786us View Post
    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

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: What does this source code do?

    Quote Originally Posted by maverick786us View Post
    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

  6. #6
    Join Date
    Apr 2005
    Posts
    1,828

    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?

  7. #7
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: What does this source code do?

    Quote Originally Posted by maverick786us View Post
    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?

  8. #8
    Join Date
    Apr 2005
    Posts
    1,828

    Re: What does this source code do?

    Quote Originally Posted by Ejaz View Post
    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.

  9. #9
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: What does this source code do?

    Quote Originally Posted by maverick786us View Post
    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.

  10. #10
    Join Date
    Apr 2005
    Posts
    1,828

    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?

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: What does this source code do?

    Quote Originally Posted by maverick786us View Post
    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

  12. #12
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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
  •  





Click Here to Expand Forum to Full Width

Featured