Re: CreateProcess and TerminateProcess

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



See below..
On Tue, 21 Jul 2009 18:21:03 +0200, "Guido Franzke" <guidof73@xxxxxxxx> wrote:


Hello NG,

I want to kill a process that I created with CreateProces . This is what I
do:

char* szProgramm = "C:\\Windows\\System32\\mspaint.exe";

STARTUPINFO m_si;
PROCESS_INFORMATION m_pi;

BOOL bRet = CreateProcess( NULL, // No module name (use command line).
szProgramm, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&m_si, // Pointer to STARTUPINFO structure.
&m_pi ); // Pointer to PROCESS_INFORMATION structure.
if (!bRet)
{
if (m_pi.hProcess) CloseHandle( m_pi.hProcess );
if (m_pi.hThread) CloseHandle( m_pi.hThread );
****
So if the createprocess fails, you try to close a handle on an uninitialized variable
whose value is problematic (if it fails, there won't be a handle) and then you just fall
through and execute the code below anyway...?
****
}
// now MS Paint has started
// ...
// after some time I want to kill MS Paint again:
****
In what way do you magically determine that it is time to close it? This seems to be a
very bad design, in that any program the user can now use should be under control of the
user, who can close it when done, but not under control of the program that launched it,
because it has no idea what the user is doing.
****
if (m_pi.hProcess) CloseHandle( m_pi.hProcess );
if (m_pi.hThread) CloseHandle( m_pi.hThread );
if (m_pi.hProcess) TerminateProcess(m_pi.hProcess,0);
****
Any program that contains a gratuitous TerminateProcess like this is deeply flawed. There
is no reason to call TerminateProcess. It is a mistake. How do you know the user is not
doing something important to the image at that point? TerminateProcess kills the process
dead, right now, right this instant. Ask about saving the work? No. No chance.

The failure mode should be pretty obvious. You carefully CLOSE the handle BEFORE you
attempt to use it! OF COURSE it won't terminate! You naively assume that
TerminateProcess worked, and apparently neglected to actually check the return code to see
if it succeeded, and therefore failed to examine GetLastError, which almost certainly
gives an error like "invalid handle" or "invalid parameter" or "you are a twit for trying
to use a handle you just closed" or something like that. How do you expect to
successfully use the handle? You just closed it!

The correct way to write code that requires knowing if an operation succeeded would be as
shown here:
if(m_pi.hProcess != NULL)
{ /* terminate */
if(!TerminateProcess(m_pi.hProcess, 0))
{ /* termination failed */
DWORD err = ::GetLastError();
... deal with error
} /* termination failed */
} /* terminate */

That said, TerminateProcess is exactly the WRONG way to do this! See the MSDN article on
terminating a process. The correct way is to PostMessage a WM_CLOSE to the main window of
the application (which you can find by searching the top-level windows and finding the
top-level window whose process ID matches the process ID of your child process).
TerminateProcess is the equivalent of those tire-puncturing strips police use to stop a
vehicle. It is destructive. DO NOT use it except in desperation. This is not a
situation in which desperation exists. Therefore, the choice of using TerminateProcess is
a Very Bad Decision. Don't do it.

Have you actually noticed that "enter" key on your keyboard? Putting the if-statement
and the statement it controls on a single line is poor coding practice.

joe
****


But the process is not terminated/killed.
How can I kill the process?

Thanks for help,
Guido

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: pipe channel getting blocked on windows
    ... >the Win32 function TerminateProcessas some kill implementations are, ... > The TerminateProcess function is used to unconditionally cause a process to exit. ... , there's no true mapping for SIGKILL to an exceptional exitcode, but I ...
    (comp.lang.tcl)
  • Re: Process.Kill() problem
    ... When Process.Killthrows the Win32Exception the NativeErrorCode ... When I use the alternative TerminateProcess() it just returns false ... iexplore.exe isn't what I need to kill anyways. ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: how to kill word session forcibly...
    ... which has the code to kill the winword session. ... >> The TerminateProcess function is used to unconditionally cause a process to ... >> causes a process to exit, but DLLs attached to the process are not notified ...
    (microsoft.public.word.vba.general)
  • Re: Process.Kill() problem
    ... You *can* kill Internet Explorer (try the Remote Process Viewer and you can ... When I use the alternative TerminateProcess() it just returns ... >>> Rick ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: TerminateProcess?
    ... From CreateProcess()? ... "Dick Monahan" wrote in message ... > Process 00 detected the hang and did a TerminateProcess. ...
    (microsoft.public.win2000.developer)