Search code examples
c++systemexitterminatekill-process

How to terminate a running program in c++


I have written a program using C++, which I call

system("C:\xampp\xampp-control.exe");
to run the xampp control panel. When I run the program after compiling, it runs smoothly, except the program I have written is still running. Once the XAMPP control panel is launched, I want to terminate the program. What could possibly be done? Any help is much much appreciated.


Solution

  • You can replace your application by the one being called with exec.

    // Note: this waits for the exectued program to finish
    // before the call to `system()` returns.
    system("C:\xampp\xampp-control.exe");
    
    // You can replace the current processes with the one you
    // are starting like this.
    execl("C:\xampp\xampp-control.exe", "xampp-control.exe");
    // If this returns the applicaion failed to start.
    std::cerr << "Failed to start application\n";
    exit(1);