-
April 19th, 2011, 03:45 PM
#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
-
April 19th, 2011, 04:21 PM
#2
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.
-
April 21st, 2011, 04:46 AM
#3
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
-
April 21st, 2011, 07:15 AM
#4
Re: Restart a program
Originally Posted by golanshahar
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|