views:

55

answers:

3

How can I force an application, say myapp.exe, to close using C++ on Windows CE from a different application?

The scenario is that I have a previous installation of some software that does not behave properly when upgrading to a new version. I therefore need to kill a process (from the updater) before continuing the update.

+2  A: 

TerminateProcess? (MSDN)

BOOL TerminateProcess( HANDLE hProcess, 
                       DWORD uExitCode );

You will need the HANDLE to the process which you can easily obtain using the Toolhelp32 APIs. An eample of their use to enumerate all processes on the system can be found here.

Konrad
A: 

I think ExitProcess is more systematic approach than TerminateProcess. ExitProcess provides clean process shutdown where as TerminateProcess exit the process unconditionally. Syntax for ExitProcess:

VOID ExitProcess(
  UINT uExitCode
);

For more information please visit this link.

It totally depends on your application how it wants to exit.

user001
Agreed, but the question was about forcing an application closure. ExitProcess may not cut it if the application is not behaving. Also, I don't think ExitProcess can be used on a different process.
Konrad
Yeah it needs to work on a different process
Chris
that is what my last statement mean, it depends on application to applicationYep..for different process TerminateProcess will do.
user001
This only works for the process itself. You can call ExitProcess to quit yourself, but you can't use it to shut something else down.
ctacke
A: 

The first thing to do is to send it a WM_QUIT to see if it might close down gracefully. WM_QUIT should cause the app's message pump loop to exit and subsequently terminate. This should allow the app to clean up it's resources cleanly.

If that fails, (and only after it fails) then you can use the toolhelp APIs to find the process (create a snapshot using NOHEAPS, then iterate to find it with First/Next calls) and terminate it using TerminateProcess.

ctacke