CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

Thread: Restart a program

  1. #1
    Join Date
    Apr 2011

    Restart a program

    hi all,
    i have a basic c++ knowledge and was wondering how to restart a process if crashed or ended.
    for example i want a virtualbox run in the background this is what i have so far
    #include <windows.h>
    #include <iostream>
    using namespace std;
    int main()
        const char* app = "virtualbox.exe";
       ShellExecute(GetDesktopWindow(), "open", app, "--startvm \"OpenSuseServer\"", "c:\\program files\\oracle\\virtualbox", SW_HIDE);
        for (int i = 1; i > 0; i++)
    im thinking maybe a basic loop that checks the process[virtualbox.exe] to see if its still alive or not if not recall the same shellex command..
    i have done some searching about getting the pid and check it to see if its still active butevery sample code i have gotten from the web didnt compile properly and a little confusing..

    any help would be appreciated.. my comipler is GNU compiler using Code:Blocks..

  2. #2
    Join Date
    Oct 2006

    Re: Restart a program

    Here's a (I hope) better example to base your loop on.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    and before posting

    Refresh your memory on formatting tags here

    Get your free MS compiler here

  3. #3
    Join Date
    May 2005

    Re: Restart a program

    As suggested you can use the:

    Save the process handle and in the loop you can call the:

    And check the lpExitCode code to see if its STILL_ACTIVE.

    But what will you do if someone will kill your watchdog program?

    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order


  4. #4
    Join Date
    Feb 2003
    Iasi - Romania

    Re: Restart a program

    Quote Originally Posted by golanshahar View Post
    Save the process handle and in the loop you can call the:
    Much better is to use WaitForSingleObject. This avoids a little cute bug in case that watched process exits with the code 259 (STILL_ACTIVE ).
    Last edited by ovidiucucu; April 21st, 2011 at 07:17 AM.
    "When in Rome, do as Romans do."
    My latest articles:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)