-
April 15th, 2004, 11:53 AM
#1
WaitForSingleObject Question
Does anyone know if you can use WaitForSingleObject on a window? I have a handle to the window (HWND) and I want to know when the user closes the window. WaitForSingleObject seems to only work on processes and not windows.
Your help is greatly appreciated.
Thanks
-
April 15th, 2004, 12:13 PM
#2
According to MSDN:
The WaitForSingleObject function can wait for the following objects:
Change notification
Console input
Event
Job
Mutex
Process
Semaphore
Thread
Waitable timer
There's no "window handle" in this list
Sorry!
-
April 15th, 2004, 12:15 PM
#3
Thanks - I think I will use the following code instead:
BOOL WaitForWindowExit()
{
Sleep(5000);
HWND WFWindow = FindWindow("IEFrame", "My Window - Microsoft Internet Explorer");
while(WFWindow != NULL)
{
Sleep(3000);
WFWindow = FindWindow("IEFrame", "My Window - Microsoft Internet Explorer");
}
return true;
}
-
April 15th, 2004, 12:23 PM
#4
You cannot use window handle but you can use thread or process handle.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
April 15th, 2004, 04:48 PM
#5
Catch the WM_CLOSE
What you should do is catch the WM_CLOSE message from the window, and then send a (user-defined) message (or call a function) to whoever wants to know.
"GOTO's make your co-workers laugh at you"
-
April 15th, 2004, 04:58 PM
#6
The assumption is that drm15 wants to know when some other window is closed - window created by another application not programmed by drm15.
Therefore in order to catch WM_CLOSE message global hook would have to be set and that is not necessary for that task.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
April 15th, 2004, 05:00 PM
#7
The last post is correct, I don't know how to listen for the WM_CLOSE when I don't have the process. The reason I cannot use the waitForSingleObject on the process id is because the process id does not go away when the applications window is closed. I think this is a feature of Java running on the MVM? The problem is that it has ports open and if the process is not stopped then the ports are not closed. This is why I need to listen for the window to close and not for the process to terminate.
Thanks to everyone for your help!
-
April 15th, 2004, 06:03 PM
#8
Are you having the handle to the window you want to spy on ?
-
April 15th, 2004, 08:38 PM
#9
Originally posted by drm15
I have a handle to the window (HWND) and I want to know when the user closes the window.
Again, I think that:
Originally posted by me
You cannot use window handle but you can use thread or process handle.
There are only 10 types of people in the world:
Those who understand binary and those who do not.
-
April 16th, 2004, 01:09 AM
#10
U can also subclass that window then intercept its WM_DESTROY message.
Trust urself!
-
April 16th, 2004, 03:56 AM
#11
You have got a window handle, and want to use WaitForSingleObject?
Try this:
Code:
DWORD dwProcessID = 0;
DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwProcessID);
if(0 != dwThreadID)
{
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
if(NULL != hProcess)
{
::WaitForSingleObject(hProcess, INFINITE);
AfxMessageBox(_T("Terminated"));
}
}
-
April 16th, 2004, 03:58 AM
#12
Originally posted by sephiroth2m
U can also subclass that window then intercept its WM_DESTROY message.
Only the window is created by your current process.
Best Api Monitor tool.
Trace the target program automatically and monitor the parameters of all API and COM interfaces.
Auto Debug for Windows 4.0
Auto Debug for .Net
http://www.autodebug.com/
-
April 16th, 2004, 04:20 AM
#13
DWORD dwProcessID = 0;
DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwProcessID);
if(0 != dwThreadID)
{
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
if(NULL != hProcess)
{
::WaitForSingleObject(hProcess, INFINITE);
AfxMessageBox(_T("Terminated"));
}
}
And don't forget to close the handle!
Code:
.........
{
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
if(NULL != hProcess)
{
::WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
AfxMessageBox(_T("Terminated"));
}
}
-
April 16th, 2004, 05:15 AM
#14
Originally posted by VictorN
And don't forget to close the handle!.........
Ok, thanks! I will remember.
-
April 16th, 2004, 09:00 AM
#15
Originally posted by pengch
Only the window is created by your current process.
I think there's some way to subclass another process' window?. Anyway, we can't rely on a thread/process handle to verify if a window is closed. Process can have multiple threads, and thread can have multiple windows
Trust urself!
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
|