Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to run a exe or bat file when window shut down? I want to unmount few drives , which are mounted by my application , when user shutdown the system.

For that I need to execute another exe on shutdown, which will disconnect drives mounted by my app.

I got few links which shows we can create a entry in Group Policy to execute a programe while window shut down. I am not sure how I can add a entry to Group ploicy by C++ code.

Again , group policy doesn't work with Win Vista. Any other better approach ?

share|improve this question

migrated from stackoverflow.com Nov 12 '10 at 14:00

This question came from our site for professional and enthusiast programmers.

    
What if somebody pulls the plug on their computer? Software can't have dying wishes. –  Greg Hewgill Nov 12 '10 at 10:29
    
Agree but I want to configure it through my code –  Simsons Nov 12 '10 at 11:41
add comment

4 Answers 4

In addition to writing your own batch file to shut down and do what you want, you may be able to schedule a task to do the same thing so you wouldn't have to remember to click a shortcut to shutdown a special way or anything.

Using Admin Tools | Task Scheduler you can schedule a task based on logged events in the system event log. The shutdown/restart event is associated with the System event log with USER32 as the source, event ID 1074. When the event log is updated with that info the scheduled task should be started. I tested this on a box at work and it worked just fine, but I am an admin in the domain and not sure if you would run into permission problems or something.

share|improve this answer
    
The task scheduler is probably your best option, as it is built into Windows and is quite simple to implement. –  Anders Jul 3 at 4:23
add comment

Write a program that has a message-only window (HWND_MESSAGE), then handle: WM_ENDSESSION, then run your bat file or do what you want.

Windows: http://msdn.microsoft.com/en-us/library/ms632599(VS.85).aspx

WM_ENDSESSION: http://msdn.microsoft.com/en-us/library/aa376889(VS.85).aspx

share|improve this answer
    
@Moo-Juice, My application wont be running while the system shutsdown, In fact I want to start the exe on window shut down –  Simsons Nov 12 '10 at 10:36
1  
This "agent" that I have suggested would run on system start up and sit quietly in the background, it's sole existence to be waiting for that WM_ENDSESSION message. Better yet, you could make it have a taskbar icon, a list of tasks to do, a configuration dialog and then provide it as a download for other users who want to do stuff when their machines shutdown. Call it "ShutDownPro". All the women will flock to you. –  Moo-Juice Nov 12 '10 at 10:43
add comment

What you can do is write your batch file to include all of your commands you would like to execute just prior to shutting down and then as the last command include the shutdown.exe in a sytax similar to this: shutdown.exe -s -t 45. In this case, I am telling the command to shutdown the system, with the -s switch and then the -t switch is telling to do it in 45 seconds. You may need to use the -f switch to force any program to close if they will prevent the system to shutdown. If you are running Windows XP, Vista or 7 the shutdown command is already available. Anything older, though , you may have to download it from Mircrosoft's site. I hope this info helps!

Danbo

share|improve this answer
add comment

Since that version of Vista does not have GPEDIT.MSC installed, perhaps find what entries GPEDIT would normally make for a Shutdown script and see if those registry keys exist on your Vista Home?

I don't have Vista running here, but did a preliminary test on Server 2003. I started REGMON and opened GPEDIT.MSC, then edited the Shutdown script. The key change was located here:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts\Shutdown

If that key tree structure exists on Vista, it may just be a matter of building the entry on a machine that does have group policy editor, export the key and import it into Vista. Pure speculation, I haven't tried this.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.