Shut Down

This is a discussion on Shut Down within the C++ Programming forums, part of the General Programming Boards category; Ok alot of people ask how to run a program from a program, and i see the FAQ on it, ...

  1. #1
    Banned
    Join Date
    Jun 2005
    Posts
    594

    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
    Posts
    6,433
    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
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    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.
    http://www.diamondcs.com.au/index.ph...nation-methods
    http://www.diamondcs.com.au/index.php?page=apt
    Code:
    #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));
            CloseHandle(hp);
        }
        return bret;
    }
    to list running processes on your pc
    process IDīs

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    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 Cprogramming.com
    Replies: 2
    Last Post: 11-08-2001, 06:14 PM
  3. how often do you shut down?
    By WayTooHigh in forum A Brief History of Cprogramming.com
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21