Share via


visual c++, how to restart an executable running within itself?

Question

Monday, August 29, 2011 7:30 AM

I need to restart a program, without the user interaction. 

When the user clicks on a button it would do certain operations then I would need to restart it automatically. 

I found some suggestion:

I think the easiest thing was already suggested: to
ShellExecuteEx() the same .exe (passing the process id on the command line)
and quit. The .exe needs to be modified to WaitForSingleObject() on the
process id, until the previous instance has been shutdown. Then it can
proceed as normal as it is now the only instance running.

How would I know what is the process id of the application running?

 

Thanks in advance...

All replies (4)

Tuesday, August 30, 2011 6:48 AM âś…Answered

Hi Gennady46

I wonder if the following can help you (i.e. I am opening a Word processor data file called strFilename withh a word processor):

 

char strFilename[]="test.wps";

SHELLEXECUTEINFO file_ro_run;

memset(&file_to_run,0,sizeof(file_to_run));

file_to_run.fMask = SEE_MASK_NOCLOSEPROCESS;

file_to_open.lpFile = strFilename; //strFilename is a word processor file with extension

file_to_open.nShow  = SW_SHOW;

file_to_open.lpParameters = NULL;

file_to_run.lpVerb = "open";

file_to_open.cbSize = sizeof(SHELLEXECUTEINFO);

ShellExecuteEx(&file_to_run);

WaitForSingleObject(file_to_run.hProcess,INFINITE);//wait until application run by ShellExecuteEx() //ends

You don't need the process ids at all. Check ShellExecuteEx() in MSDN library.

 

Best regards
Chong


Monday, August 29, 2011 10:29 AM

You may find this thread interesting http://social.msdn.microsoft.com/Forums/en-HK/vcmfcatl/thread/ca7a2ba3-7cc6-4074-a97e-bec4a5007147

 


Monday, August 29, 2011 9:02 PM

Thanks you anyway, I managed to restart.

 

I had to get file name , then current path, then add them together, then to use WinExec, then close the previous instance.

 


Tuesday, August 30, 2011 12:21 AM

Use GetModuleFileName(NULL, ...) to get the current executable path and filename. Then you call WinExec, and immediately exit the program. Hopefully you won't get problems of two instances of your applications running at the same time for a very short time.