Page 1 of 1

Run program before shutdown

Posted: Sun Apr 10, 2011 8:38 pm
by whertz
Does anyone know how to run a program before windows shuts down? Like this tool does: http://www.fileware.com/products.htm#LastChance

I don't need to shut down windows myself, what I need is to detect when Windows shuts down, launch a program and when it is finished then carry on shutting windows down. I know you can do it via gpedit.msc but I need to do it by a PB program or batch file. Hope I made sense! Thanks.

Re: Run program before shutdown

Posted: Sun Apr 10, 2011 9:14 pm
by c4s
You could catch the #WM_QUERYENDSESSION message (http://msdn.microsoft.com/en-us/library ... 85%29.aspx), block shutting down, run the program you need and then shut down yourself.

Re: Run program before shutdown

Posted: Sun Apr 10, 2011 9:36 pm
by whertz
c4s wrote:You could catch the #WM_QUERYENDSESSION message (http://msdn.microsoft.com/en-us/library ... 85%29.aspx), block shutting down, run the program you need and then shut down yourself.
Thanks c4s but that's beyond my capability. I just thought someone on the forum might have wanted to do this before and had some code handy.

Re: Run program before shutdown

Posted: Mon Apr 11, 2011 8:22 am
by Shardik
whertz wrote:I just thought someone on the forum might have wanted to do this before and had some code handy.
ricardo posted a code example just 3 weeks ago:
http://www.purebasic.fr/english/viewtop ... 12&t=45779

Re: Run program before shutdown

Posted: Mon Apr 11, 2011 11:38 am
by whertz
Shardik wrote:
whertz wrote:I just thought someone on the forum might have wanted to do this before and had some code handy.
ricardo posted a code example just 3 weeks ago:
http://www.purebasic.fr/english/viewtop ... 12&t=45779
Thanks Shardik. Seems simple enough, but this code would not wait until your program has finished what it is doing, so there could be a problem if windows shuts down too early.

Re: Run program before shutdown

Posted: Mon Apr 11, 2011 3:05 pm
by buddymatkona
@whertz
Don't give up on Group Policy if you want to keep this simple. Just add a batch file into the Shutdown folder used by the MMC Group Policy snap-in. You will need admin privileges for MMC setup and to put the .BAT file in the proper folder but once there it can run a PB EXE which will not be cut off. It is easy to have the program look for a command somewhere to tell it what to do so you will not have to edit policy more than once.

Re: Run program before shutdown

Posted: Mon Apr 11, 2011 4:51 pm
by whertz
buddymatkona wrote:@whertz
Don't give up on Group Policy if you want to keep this simple. Just add a batch file into the Shutdown folder used by the MMC Group Policy snap-in. You will need admin privileges for MMC setup and to put the .BAT file in the proper folder but once there it can run a PB EXE which will not be cut off. It is easy to have the program look for a command somewhere to tell it what to do so you will not have to edit policy more than once.
I need to add the program not just for my computer, but other's as well. This is why I need a PB program to do it, or a batch file or something. So it has to be 'portable' in that it will run the program when the user finishes setting up my program. Can it be added to Group Policy via script? Maybe adding it via registry but that is kinda messy.