ethrbunny has asked for the wisdom of the Perl Monks concerning the following question:

Im working on some code to replace applications on remote clients. I have the new apps copied over and am searching the process list to see if they are running. If they are running then I want to tell them to close so that I can replace them. To this point Ive been working with Win32::OLE->GetObject('winmgmts:\\\\') and calling ->Terminate() using the process id *and* Win32::Process::KillProcess( $objProcess->ProcessId, 1 ) The issue Im facing is that neither of these seem to be a 'clean' exit. The apps in question need to believe that they are being shut down in an 'orderly' fashion so they can write data out. Suggestions?

Replies are listed 'Best First'.
Re: 'Gracefully' closing a win32 app
by BrowserUk (Patriarch) on May 31, 2007 at 22:40 UTC

    If all the applications you're trying to terminate have a message queue, then sending them a WM_CLOSE or WM_QUIT message as bart has suggested may solve your problem, assuming they don't do anything nasty like only actioning that message, if they generated it.

    However, many GUI apps respond to that message by popping up one of those eternally annoying "Do you really want to do what you just said you want to do?" dialog boxes. So then you'll be in the game of having to wait for the dialog to appear, track it down and send it a button push (see Win32::GuiTest ), to dismiss the dialog. (Or waiting for a user to do it and hoping that they don't choose to ignore you or just go home).

    But if any of them do not have a message queue, ie. most command line applications, then you're out of luck.

    Win32 perl scripts do have a message queue and they do respond to the two messages above as a part of the opcode dispatch loop (win32_async_check()). However, they translate them into a SIGHUP. I'm not sure if that is trappable under win32 using a %SIG handler--I've never had occasion to try it--but if it is, the script could choose to ignore your request.

    Of course, if you are writing the applications you want to terminate/replace, then you can use the above information to ensure that next time your applications do the right thing and save their state and terminate when requested to do so. But that won't help you this time.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      They are my apps so I know they don't have confirmation dialogs when they close. I didn't know you could 'inject' a message into someone elses queue though. That's an interesting trick. Im off to give it a try. TY!
Re: 'Gracefully' closing a win32 app
by bart (Canon) on May 31, 2007 at 21:47 UTC
    Using Win32::API, use the SendMessage (or PostMessage) API call, and send the message WM_CLOSE to its main window; or maybe, use the PostQuitMessage API call instead. I'm a bit iffy on the details, but you can read up on them on Microsoft's MSDN website.