1

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 ?

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

5 Answers 5

2

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.

1
  • The task scheduler is probably your best option, as it is built into Windows and is quite simple to implement.
    – Anders
    Jul 3, 2014 at 4:23
0

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

2
  • @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, 2010 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, 2010 at 10:43
0

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.

0

In Windows Task Scheduler, create a Basic Task like that:

Click here to Open the Image of Windows Task Manager

-1

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

1
  • They don't want to schedule a shut down of the machine. They want to have a program that does something that happens whenever the shutdown happens for reasons completely unrelated to the program. Jan 27, 2023 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .