Kill Process - FAQ code ??
Dear All,
this is the code from FAQ :
PostThreadMessage(piProcessInfo.dwThreadId, WM_CLOSE, 0, 0);
WaitForSingleObject(piProcessInfo.hProcess, 1000);
// Check exit code
DWORD dwExitCode = 0;
GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
if(dwExitCode == STILL_ACTIVE)
{
// Process did not terminate -> force it
TerminateProcess( piProcessInfo.hProcess,
0); // Zero is the exit code
// in this example
}
As per the FAQ,
the code for Killing an process/application,
the API PostThreadMessage
actually returns success to me but the application doesn't receive the message at all ( I check this using the Spy++).
and then the 2nd part of the code -
TerminateProcess (... )
succeeds and the application actually is killed.
Why does the first API call doesn't kill the app safely?
Can anyone explain this?
regards
Sankar
# 1 Re: Kill Process - FAQ code ??
If the app you're trying to kill doesn't process messages using PeekMessage/GetMessage, either because it's hung or because it's busy doing something else, it won't see the WM_CLOSE, and thus spy won't see it being available to the application either.
# 2 Re: Kill Process - FAQ code ??
If the process dont have Win32 Windows (visible ) ,or Message Pump code in it then you wont get the message.
Dont worry about them , terminate them directly. Post the WM_CLOSE to thread , but dont check the return value after WaitForSingleObject directly terminate the process.
# 3 Re: Kill Process - FAQ code ??
Originally posted by Krishnaa
If the process dont have Win32 Windows (visible ) ,or Message Pump code in it then you wont get the message.
In this case 'PostThreadMessage()' would already fail as mentioned in the FAQ... ;)
Originally posted by Krishnaa
Dont worry about them , terminate them directly. Post the WM_CLOSE to thread , but dont check the return value after WaitForSingleObject directly terminate the process.
Why would you try to terminate a process which is already closed??
Meaning: Why should I call 'TerminateProcess()' although the 'WM_CLOSE' message already did the trick?
# 4 Re: Kill Process - FAQ code ??
Dear Andreas,
Pl. read my question again.
PostThreadMessage(..., WM_CLOSE) doesn't work as expected even though the API returns 1 for success.
the application is still ALIVE and not CLOSED.
Also, I tried some other command like WM_ACTIVATE using the post threadmessage. it doesn't work.
Does your code work??
Have you tested it.
thanks & regards
sankar
# 5 Re: Kill Process - FAQ code ??
Below is the description of PostThreadMessage() from MSDN
The function fails if the specified thread does not have a message queue. The system creates a thread's message queue when the thread makes its first call to one of the User or GDI functions. For more information, see the Remarks section.
So i think, if your thread is not peeking message, it is doing other jobs, you must call TerminateThread() to terminate your thread immediately. And that's what you are expecting.
---
i am a freshman..
wuyh at 2007-11-10 8:53:20 >

# 6 Re: Kill Process - FAQ code ??
I am telling to call TerminateProcess in order to make it sure that if PostThreadMessage dosent works then also process is killed.
# 7 Re: Kill Process - FAQ code ??
Originally posted by shrishankaraan
Dear Andreas,
Pl. read my question again.
PostThreadMessage(..., WM_CLOSE) doesn't work as expected even though the API returns 1 for success.
the application is still ALIVE and not CLOSED.
Also, I tried some other command like WM_ACTIVATE using the post threadmessage. it doesn't work.
Does your code work??
Have you tested it.
thanks & regards
sankar
Sankar,
Messages sent by 'PostThreadMessage()' are not associated with a window therefore these messages cannot be dispatched by a call to 'DispatchMessage()'. If the thread of the other process is in a modal loop (like 'MessageBox' or 'DialogBox'), the message will be lost. That might be one reason, since dialog-based applications always run within a modal dialog box...
Nevertheless, I might change the FAQ entry to a more 'reliable' way...if you look at the knowledge base article (the link is provided at the end of the FAQ entry) you will see that they basically post a 'WM_CLOSE' message to all top-level windows of the process to be shut down. I have not tested whether the same problems with being in a modal loop occur with that solutions but I might try it over the weekend...
# 8 Re: Kill Process - FAQ code ??
Originally posted by shrishankaraan
actually returns success to me but the application doesn't receive the message at all ( I check this using the Spy++).How are you using Spy++ to determine this?
# 9 Re: Kill Process - FAQ code ??
Dear All,
Thankls for all the help.
Let me reply to each one of you:
1) Krishnaa:
( a ) TerminateProcess has been already used in my code and it works, but I don't want to use this because this would not allow my child applications to close safely
( b ) Even if I use TerminateProcess, some of my child apps, take more than 30 seconds to finish their shutdown procedure, beforef which the "WAIT/ENDTASK/CANCEL" idlaog box appears which is what I'm trying to avoid in the whole issue.
2) Wu :
I read that post regarding the PeekMessage and it also goes on to say that I have to use a Hooker function. How can I do this & where should I do this, the concept is not very clear.
3) Sam Hobbs:
Debug the application, and open the Spy++, Use the Finder to attach the CHild Window app with Spy++, and step through the PostMessageThread (.. ) and still see that the Child APP doesn't receive the message.
4) Andreas :
Thanks for your efforts. PL. let me know your testing result.
at this point, I'm still stuck up with this issue. any suggestions are most welcome.
Again, thank you all very much .
regards
Sankar
