CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2011
    Posts
    1

    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
    Code:
    #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++)
        {
            Sleep(6000000);
        }
    
    }
    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..
    thankx

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Restart a program

    Here's a (I hope) better example http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx 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
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    May 2005
    Posts
    4,954

    Re: Restart a program

    As suggested you can use the:
    ::CreateProcess()

    Save the process handle and in the loop you can call the:
    ::GetExitCodeProcess()

    And check the lpExitCode code to see if its STILL_ACTIVE.

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

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

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Restart a program

    Quote Originally Posted by golanshahar View Post
    Save the process handle and in the loop you can call the:
    ::GetExitCodeProcess()
    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured