stop the c++ program and start another one

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gopython
    New Member
    • Mar 2007
    • 18

    stop the c++ program and start another one

    Hi all,

    OS: Windows XP
    C++: Microsoft Visual C++ 2005

    I am wondering if it is possible to kill the C++ program after calling another program (i.e. .bat). I did a simple test and check the "Windows Task Manager". After the exe run the .bat the exe is still there until the .bat finished running. Is that possible to kill the .exe after the .bat is called.

    Code:
    // Filename:test.cpp
    #include <iostream>
    
    int main()
    {
        std::cout << "Calling run.bat";
        std::cout << "\n";
        system ("stage\\run.bat");
        
        return 0;
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The system(" ... ") call is a synchronous call, i.e. it won't return until the called
    shell has finished. Have a look at threads but I'm afraid (from the top of my head)
    that all child processed (including the system(" ..." ) call) will be stopped as
    soon as the main thread finishes; check it out.

    Maybe a system(" ... &") call can help out (Unix specific) or a
    system("start ...") call (Windows specific).

    kind regards,

    Jos

    Comment

    • gopython
      New Member
      • Mar 2007
      • 18

      #3
      Thank you for the pointer.

      I did some research on the net. And I come across function called "execlp". But I don't have any success using this function. I really appreciate if someone can point me to the right direction. Thank you.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Try using CreateProcess.

        That is the WIN32 API function for starting applications.

        Comment

        • Savage
          Recognized Expert Top Contributor
          • Feb 2007
          • 1759

          #5
          Originally posted by gopython
          Thank you for the pointer.

          I did some research on the net. And I come across function called "execlp". But I don't have any success using this function. I really appreciate if someone can point me to the right direction. Thank you.

          Function execlp(and spawnlp which is simmiliar) is used to run a *.exe programm and it can't run *.bat.If *.bat is specifed as a program name and perror is used, it will submit error like:

          exec error:Exec format error.

          But it does what u recuire with *.exe files.

          Function CreateProcess,w hich admin Banfa specifed, does what u want,but I'm
          not sure that it can run *.bat(Never tryed that).

          I hope that it will work!!!

          :D

          Savage

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by Savage
            Function execlp(and spawnlp which is simmiliar) is used to run a *.exe programm and it can't run *.bat.If *.bat is specifed as a program name and perror is used, it will submit error like:

            Function CreateProcess,w hich admin Banfa specifed, does what u want,but I'm
            not sure that it can run *.bat(Never tryed that).
            Remember you can always "run" a batch file by actually running a command line processor (cmd.exe) with the batch file on its command line or using the start command Jos mentioned earlier.

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by Banfa
              Remember you can always "run" a batch file by actually running a command line processor (cmd.exe) with the batch file on its command line or using the start command Jos mentioned earlier.
              Yes,and that would be the simplest solution for our OP.

              So if we run cmd.exe with execlp or spawnlp it will overwrite it's parent and start
              instead that *.bat file when specifed in command line.


              Savage

              Comment

              Working...