Thread: Start and kill program from within program

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    Start and kill program from within program

    So I have a subprogram that I need to run at various stages throughout my main program. This program also needs to be in a separate window.

    I've got the program opening with this code:

    Code:
    void menu::startSerial()
    {
         char szPath[] = "C:\\Dev-Cpp\\UKGS\\standAloneSerial.exe";
      PROCESS_INFORMATION pif;  //Gives info on the thread and..
                               //..process for the new process
      STARTUPINFO si;          //Defines how to start the program
    
      ZeroMemory(&si,sizeof(si)); //Zero the STARTUPINFO struct
      si.cb = sizeof(si);         //Must set size of structure
    
      BOOL bRet = CreateProcess(
            szPath, //Path to executable file
            NULL,   //Command string - not needed here
            NULL,   //Process handle not inherited
            NULL,   //Thread handle not inherited
            FALSE,  //No inheritance of handles
            0,      //No special flags
            NULL,   //Same environment block as this prog
            NULL,   //Current directory - no separate path
            &si,    //Pointer to STARTUPINFO
            &pif);   //Pointer to PROCESS_INFORMATION
    
      if(bRet == FALSE)
      {
        MessageBox(HWND_DESKTOP,"Unable to start program","",MB_OK);
        return ;
      }
    
      CloseHandle(pif.hProcess);   //Close handle to process
      CloseHandle(pif.hThread);    //Close handle to thread
      return;
    }
    But it runs in the window of the main program. How do I get it into it's own separate place?


    Also, I'll need to terminate this process from a completely different part of the program. What's the best way to get the handle in order to use TerminateProcess()?
    Thanks a bunch.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think you need to specify: CREATE_NEW_PROCESS_GROUP in dwCreationFlags.

    I could be wrong, but from the brief description, it sounds right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    As for killing the process, try using TerminateProcess(...);

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by matsp View Post
    I think you need to specify: CREATE_NEW_PROCESS_GROUP in dwCreationFlags.
    Thanks, this looks about right. I don't have the equipment I need to test it right now, I'll post back on if it works once I'm able to test it properly.


    Quote Originally Posted by abachler View Post
    As for killing the process, try using TerminateProcess(...);
    I know to use TerminateProcess(), what I was asking was how to best get the handle to close it. This is much later in the program, so passing the address doesn't appeal to me.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by System_159 View Post
    I know to use TerminateProcess(), what I was asking was how to best get the handle to close it. This is much later in the program, so passing the address doesn't appeal to me.
    The other solution is to let the process finish by itself somehow - you either have a way to signal the process to "commit suicide", or for example have a thread in the process that looks for a file called "KillYourSelf.Now" in some particular directory. If the file exists, the process should remove the file and exit. Your starting side can watch for the file "disappearing" to see that the process has exited.

    But holding on to the process handle I would see as a decent way to do it. Shouldn't be too difficult to pass in a variable to hold that and then pass that along to the TerminateProcess(). You can also find process by name - but this is somewhat flawed, as there may be other processes running with the same name.

    Another option is of course to have a global variable (if need be, an array) where you store the process handle. That should be a last resort, and you need to be aware of the problems with global variables if you use this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. How to kill a dragon with various programming languages
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-01-2007, 12:13 PM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM