Thread: Shut Down

  1. #1
    Join Date
    Jun 2005

    Shut Down

    Ok alot of people ask how to run a program
    from a program, and i see the FAQ on it,
    but how bout this, how can i shut a program down
    from a program.

    i was looking at terminateprocess and exitprocess,
    but even msdn doesnt give to much stuff on it,
    any idea? Thanks

  2. #2
    Registered User
    Join Date
    Jun 2005
    All sorts of ways. If it is a GUI application, simply send a WM_CLOSE message to the main window of the application you wish to shut down. While an application can choose to ignore that message (eg it is allowed to ask permission of the user to close down, and the user can refuse), the common result will be application shutdown.

    TerminateProcess() is a nasty way of terminating a running program, as it does not allow for cleanup of associated system resources (eg DLLs that are in use by the program). This is one reason why help files with the task manager (at least under NT/2000) warn that closing programs down can cause system instability.

    ExitProcess() only allows a process to terminate itself.

  3. #3
    Registered User
    Join Date
    Jun 2004
    you can't kill a process and do cleanup from the outside. Obviously the process has to know when to end and do the cleanup. TerminateProcess it the only direct method. There are many others. The program doesn't do cleanup, but the OS obvously frees up the resources.
    #include <windows.h>
    bool kill_process(DWORD ppid){
        bool bret = false;
        HANDLE hp = OpenProcess(PROCESS_TERMINATE,FALSE,ppid);
        if(hp != NULL){
            bret = (TRUE == TerminateProcess(hp,0));
        return bret;
    to list running processes on your pc

  4. #4
    Registered User
    Join Date
    Jun 2005
    Quote Originally Posted by xErath
    The program doesn't do cleanup, but the OS obvously frees up the resources.
    Unfortunately, at least under windows, that is only partially true. For a process terminated with TerminateProcess(), the OS cleans up a lot of the resources used by a program but not all of them. Examples of resources that are not always cleaned up properly are references to shared memory and to system DLLs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hardware interrupt to shut down
    By GanglyLamb in forum Tech Board
    Replies: 13
    Last Post: 06-11-2003, 01:23 PM
  2. Shut off DOS screen automatically to Windows
    By Unregistered in forum A Brief History of
    Replies: 2
    Last Post: 11-08-2001, 07:14 PM
  3. how often do you shut down?
    By WayTooHigh in forum A Brief History of
    Replies: 24
    Last Post: 10-12-2001, 02:18 PM
  4. Displaying the Shut Down Windows dialog
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 09-14-2001, 05:52 PM