Link to home
Start Free TrialLog in
Avatar of nchenkin
nchenkin

asked on

Terminate app after creating a new process

Greetings experts,

I have an MFC program running on hundreds of machines in a factory. I want to add the capability for the program to automatically update itself when a new version comes out. The program is running off the local system hard disk as opposed to off a server. However, all machines can access a disk on a remote server.

When a certain event occurs I want the program to go out to this server and see if a new version of itself is out there. If so, this program will spawn a new program and then kill itself. This new program will copy the updated version of the other program to the local disk, start it ip and then kill itself. Voila, I will have updated the entire factory. Or so I hope.

Here, in 2 lines of code or less is what I am trying to do:

if( CreateProcess( NULL, "AnApp.exe", ..., &StartupInfo, &ProcInfo ) )
     AfxGetApp()->m_pMainWnd->SendMessage( WM_CLOSE );

The results are:

The destructor of my app's document class is called, but not the ExitInstance() function of the app. Also, if running in VC++ debug environment the debug program doesn't close until I close the app that was spawned (I'm actually using CALC.EXE for testing purposes). Then when the app closes, my debug log reports memory leaks.

Is there a problem with how I am closing my application (that is what CWinApp::OnAppExit() does), and/or is there some option in CreateProcess() that I need to make sure is properly set? Or is there a completely different and better way to do this?

Thanks much for your time,
NC
ASKER CERTIFIED SOLUTION
Avatar of raschalk
raschalk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mnewton022700
mnewton022700

You could use ShellExecute instead of CreateProcess. Like this:

    if ((int)::ShellExecute(NULL, NULL, "Calc.exe", NULL, NULL, SW_SHOWNORMAL) > 32)
    {
        ::AfxGetApp()->m_pMainWnd->SendMessage(WM_CLOSE);
    }
send a WM_QUIT instead of WM_CLOSE

Avatar of nchenkin

ASKER

Thanks experts,

One of my guys is looking into your solutions right now and we will get back to you shortly.

We appreciate it,
NC
raschalk,

Thanks for your information. But you say that you have an:

"SDI app to startup and shutdown applications at will"

But what we want is for an application to startup another application and then to cleanly shutdown ITSELF. It is NOT going to shutdown the app that it started.

How do we go about this?

The other suggestion didn't seem to work.

Thanks,
NC
Hi nchenkin,

If you want to shut down your own application you should post a WM_CLOSE message. Rather than my  giving you a garbled account of how to go about it, here is the procedure I have followed in the past. out of MSDN:


32-Bit Processes (and 16-Bit Processes under Windows 95)
Under Win32, the operating system promises to clean up resources owned by a process when it shuts down. This does not, however, mean that the process itself has had the opportunity to do any final flushes of information to disk, any final communication over a remote connection, nor does it mean that the process' DLL's will have the opportunity to execute their PROCESS_DETACH code. This is why it is generally preferable to avoid terminating an application under Windows 95 and Windows NT.

If you absolutely must shut down a process, follow these steps:

1.
Post a WM_CLOSE to all Top-Level windows owned by the process that you want to shut down. Many Windows applications respond to this message by shutting down.

2.
Use WaitForSingleObject() to wait for the handle of the process. Make sure you wait with a timeout value, because there are many situations in which the WM_CLOSE will not shut down the application. Remember to make the timeout long enough (either with WaitForSingleObject(), or with SendMessageTimeout()) so that a user can respond to any dialog boxes that were created in response to the WM_CLOSE message.

3.
If the return value is WAIT_OBJECT_0, then the application closed itself down cleanly. If the return value is WAIT_TIMEOUT, then you must use TerminateProcess() to shutdown the application. NOTE: If you are getting a return value from WaitForSingleObject() other then WAIT_OBJECT_0 or WAIT_TIMEOUT, use GetLastError() to determine the cause.

By following these steps, you give the application the best possible chance to shutdown cleanly (aside from IPC or user-intervention).
Your problem is that you did a SendMessage rather than a PostMessage.

Also, try WM_QUIT instead of WM_CLOSE.  

WM_CLOSE applies to a window - and your app may be written in a way so that closing the main window doesn't shut down the app.

WM_QUIT applies to the application (when single threaded).

NOTE: If multithreaded, you may need to take some extra action to terminate other threads, but that is something you should be doing already when you shut down.

raschalk,

For some reason I wasn't notified by E-mail that you had responded to my comment.

Thanks, here's the points.

Ronslow,

Thanks for your further input as well. I appreciate it. We seem to have things working now.

NC