-
June 1st, 2004, 07:21 AM
#1
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
-
June 1st, 2004, 08:04 AM
#2
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.
-
June 1st, 2004, 08:28 AM
#3
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.
-
June 1st, 2004, 08:40 AM
#4
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
-
June 1st, 2004, 09:29 AM
#5
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!
-
June 1st, 2004, 09:40 AM
#6
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.
-
June 1st, 2004, 01:05 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|