CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2002
    Location
    NJ
    Posts
    63

    SetWindowsHookEx - Controlling other process?

    Hey, all. I'm trying to hook another GUI process so I can alter the data in some edit boxes.

    I have the window handle of the app, and i have the PID of the app, retrieved with
    Code:
    GetWindowThreadProcessId();
    The problm I have is, SetWindowsHookEx() wants a Thread ID, not a process ID. In other words, this code fails with error 87, BAD_PARAM:

    Code:
    GetWindowThreadProcessId(hDlg, &pid);
    sg_hHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)pHookProc, hRes, pid);
    
    if (sg_hHook == NULL)
    { 
       // Process error
    }
    But this code passes:

    Code:
    sg_hHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)pHookProc, hRes, GetCurrentThreadId());
    
    if (sg_hHook == NULL)
    {
       // Process error
    }
    But then my app just hooks itself

    So, is there a way to convert that PID iinto a Thread ID? Or, is there a better way to meddle with another applications edit controls?

    Thanks,

    -Joe
    Intellivision is still where it's at

  2. #2
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457
    If you just want to set/retrieve text from another processes edit controls, you can use just post WM_SETTEXT and WM_GETTEXT messages to it.

    Use FindWindow and/or FindWindowEx to get a handle to the edit control window and send it a message.

    Do not use SetWindowText or GetWindowText. You must use SendMessage.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  3. #3
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457
    For example, here is how you can get the text from Internet Explorer's address bar edit control:
    Code:
    // This is the "family tree" from IE's main window to the edit control...
    static const TCHAR szClassNames[][64] = {
    						TEXT("WorkerW"),
    						TEXT("ReBarWindow32"),
    						TEXT("ComboBoxEx32"),
    						TEXT("ComboBox"),
    						TEXT("Edit")
    					};
    
    // This is the number of generations (windows) in the family...
    static const int iWindows = sizeof(szClassNames) / sizeof(szClassNames[0]);
    
    // Get the handle to the IE window (currently on Google.com)...
    HWND hIe = FindWindow(NULL, "Google - Microsoft Internet Explorer");
    
    if (hIe == NULL)
    {
    	// Couldn't find the main IE window...
    	return -1;  // Error
    }
    
    // Start with the top level window...
    HWND hChild = hIe;
    
    // And continue getting children until we reach the edit control...
    for (int i = 0; i < iWindows; i++)
    	if (!(hChild = FindWindowEx(hChild, NULL, szClassNames[i], NULL)))
    		return -1;  // Error
    
    // We now have a handle to IE's address bar edit control.  Get the text...
    TCHAR szUrl[MAX_PATH] = {0};
    SendMessage(hChild, WM_GETTEXT, (WPARAM) sizeof(szUrl), (LPARAM) szUrl);
    
    // szUrl should now contain "http://www.google.com"
    Last edited by Bond; June 1st, 2004 at 08:30 AM.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  4. #4
    Join Date
    Nov 2002
    Location
    NJ
    Posts
    63
    Bond,

    Thanks. I was able to get text, but not set text. I was using GetWindowText and SetWindowText. I changed it to use SendMessage, and it works just fine. Thanks.

    Back to my original question, though, there must be a way to get the Thread ID. I'd still like to be able to hook the window procedure.

    -Joe
    Intellivision is still where it's at

  5. #5
    Join Date
    Jan 2004
    Posts
    56
    Hi teleplayr, pliz read more about GetWindowThreadProcessId and GetCurrentThreadId's documentation . GetWindowThreadProcessId returns both Process ID and Thread ID of a specific window, while GetCurrentThreadId returns Thread ID of the current window.
    Regards
    Trust urself!

  6. #6
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457
    Why not just subclass it?
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  7. #7
    Join Date
    Nov 2002
    Location
    NJ
    Posts
    63
    sephiroth2m,

    I'm not sure how I missed that in the documentation. Thanks

    Bond,

    Yes, I could, thanks.

    -Joe
    Intellivision is still where it's at

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