How to do this? Post a short example.Quote:
Originally posted by sephiroth2m
U can also subclass that window then intercept its WM_DESTROY message.
:rolleyes:
Printable View
How to do this? Post a short example.Quote:
Originally posted by sephiroth2m
U can also subclass that window then intercept its WM_DESTROY message.
:rolleyes:
There's several ways to do this, here is one (DLL) that works fine on Win9x/NT/2K/XP. The SetMyHook(HWND my_window, HWND subclassed_window) function subclasses the 'subclassed_window' in another process and PostMessage to 'my_window' when it's destroyed. ReleaseMyHook() unsubclasses it.
Regards
Exactly why I wait for the Window to close instead of the process. I have noticed that when this window is closed the process is still running. This is why I watch for the window to close, then use terminate process on the running process.
I would change the code of the application, but it is a vendor application that they don't allow changes to.
Hm.. I would use TerminateProcess only on extreme cases. It is kind of an ugly brute force technique.
Well, how would the other process terminate otherwise? I mean, the normal case.
It doesn't until you reboot the machine. It is a Java Applet using the MVM to run. It opens a port that I need to be closed and reopened every time, but this port is locked as long as the process it running.
Hm.. interesting. Does that applet provide some protocol for closing the port at all ?
Doesn't look like it, but I am not allowed to view the code since it is a vendor application.
You may not have a source code, but some vendors implement some sort of interprocess communcation to allow for such things.
I remember one where we had to send UDP packets to a particular port and another one where we had to change an xml file to update certain things.
I implemented VictorN's 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"));
}
}
But it didn't work since the process was still running after I closed the window. Thanks for your help - any other ideas?
1. I need to correct you: ovidiucucu had suggested this code, not Victor !
2. What does it mean "it didn't work "?
a) dwThreadID == 0 ?
b) hProcess == NULL ?
c) WaitForSingleObject doesn't return ?
In case of c) - you will need to ::TerminateProcess(hProcess ); :(
regards,
Victor
Sorry about that, my mistake.
What doesn't work is the WaitForSingleObject because it is waiting on a process that doesn't quit. I need a way to find out when the window is closed so that the process can be terminated through TerminateProcess.
To Victor,Code:DWORD dwProcessID = 0;
DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwProcessID);
if(0 != dwThreadID)
{
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, NULL, dwProcessID);
if(NULL != hProcess)
{
while(::IsWindow(hWnd) )
{
::Sleep(200);
}
::TerminateProcess(hProcess, 0);
::CloseHandle(hProcess);
}
}
As promissed, I have not forgot CloseHandle ;)
2 ovidiucucu :
:thumb: :thumb: :thumb:
Victor
have you read .....
CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ FAQs > Threads: How to end a thread?
http://www.codeguru.com/forum/showth...hreadid=231242
and what Jeffrey Richter has to say ...
http://www.microsoft.com/msj/0799/Win32/Win320799.aspx