Is it possible to run a batch file when a program is launched? - windows

I have a batch file that toggles aero. Is there a way that when you open a certain program the batch file will run when it is launched and run again when the program is closed?

Well yes, but on the Properties for the program you can tick a checkbox to do this on the compatability tab.
Find out info on what the browser is doing. Sart your program and then use Task manager to find your game (it is a real game and not some jscript web thing?).
Compatability layers are scritable.
See http://support.microsoft.com/kb/286705
set __compatlayer=256Color (note wrong spelling)
Running a program will have same problem as setting compatability. You have to find something to run it on.
Windows can start a debugger automatically when a program is started. You can substitute any program that can start a program (as a batch can) for the debugger.
You can also run a script that triggers on program start. Task Scheduler can run tasks when certain events occur.
But you have to know what exact object to trap. Use task manager in the first instance.

Related

Get from a started program path to .lnk file that started it

Is there a way in Windows OS to detect whether you program has been started from .bat or .lnk file and obtain the full path to it?
I know I can get the parent process, but how to detect... for the lack of a better term let's name it 'way'... the way a program has been started?
UPDATE
Some malware modifies .lnk file (adding an ad to its params) pointing to my app. Also it starts my app from .bat. I want to check hash of the .lnk file and just quit if it's been started from .bat.
Regards,
How does the program get started? Is it a SERVICE or does it run on startup?
If you killed the program, can you re-start it predictably?
I would use Processs Monitor to triangulate on what launched the app.
Process Monitor logs EVERYTHING the machine is doing. You can filter out all the noise to just .lnk, .bat, and/or your specific program.
Kill the program
Start Process Monitor (with filters applied)
Start the program
Stop Process Monitor
Search Process Monitor log by your program name
Scroll back to see what processes lead up to the program running
Hope it helps.
-Allen

VB6 program when run as schedule task freezes until focused

I have been searching the web for an answer however came up with nothing. I created a VB program that calls a .bat script to call commands to run a WinSCP script. This WinSCP script simply transfer files from a sftp (linux) server to a windows machine. When running the program either via VB6 IDE or as a compiled executable the program run as normal.
However when running the program as a schedule task the program freezes and only runs until focus is brought to it. Have any encountered this error? (I'm running win 7 32bit)
There was a question similar to mine that was asked here however about VBScript and it seems the person was able to pin point the line causing the issue, not the same for me. Here is the link to the thread vbs runs by double clicking, freezes when run from task scheduler
Thanks in advance,
j3rg
I have found what was causing the error to occur. My project consist of a form and a module. I call the form via the module in the manner:
form.show
I forgot to set in the properties of the project to have the program start from the module Sub Main sub-routine. After selecting this option the program work as normal when called by a schedule task.
I hope this information help anyone that runs into the same scenario.

Perform an action when a program starts

Let's say I wanted to run a batch file when a specific EXE is started. Are there any hooks for this in the Win32 API?
I'm using VCL with Delphi, but general info on this would be nice as well.
The program is supposed to run in the background, and, when the user starts an application, the program detects this, checks whether the application matches the criteria and runs the associated batch file if so.
Have you tried to use the Create Process Function of Delphi?
Here is a small link that it can guide you for the process:
http://snipplr.com/view/59064/using-createprocess-to-execute-programs/

How to restart program automatically if it crashes in Windows?

How can I start my program automatically if it crashes on windows 2003 server? Sometimes my program just crashes, is there a way in windows or settings that I can set?
There are several ways to create a process supervisor/guardian process on Windows.
First, is to leverage windows command line capabilities. Create a bat file:
#echo off
:start
start /w "your app to watch.exe"
goto start
start /w will wait for the process to exit. When the process crashes and exits, the bat script will relaunch it.
Another option is to use free supervisor tool https://github.com/chebum/Supervisor. It allows to restart the crashed app, plus it allows to monitor two or more apps at once and it will automatically close these apps when supervisor's window is closed.
The usual approach is to run what is known as a guardian process. This is a separate process, often a service, that monitors the state of the main process. When the guardian detects that the main service has died, it re-spawns it.
To the very best of my knowledge, there is not built in Windows functionality to do this for you.
Notice: running self-looping bat files can be useful, but unless you know what you're doing, they can wreak all kinds of havoc. This goes especially if you run them on startup. You have been warned.
Anyway. I just remembered something from my 286 days, when I played around a lot with BAT files. If you write the file
yourprogram.exe
some other event
the BAT file will run yourprogram, and then pause and wait around in the background until the program exits. After that it will run "some other event". This used to be kind of annoying if you wanted to run multiple things at once, but here it's actually useful. Using this, it's possible to make it run a loop that restarts the program (and reruns the bat file) as soon as it exits. Combine this with https://superuser.com/questions/62525/run-a-completly-hidden-batch-file, and you'll never even see it happening.
The final BAT file ("restart.bat" in this example) will look something like:
c:\[location]\yourprogram.exe
wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"
That's about it. Start the program (on startup via task or even just startup folder) with line 2, and this ought to solve your problem :)
Oh, if you want to stop the loop, just rename the bat file or put "// " in front of the two lines, save it, and exit the program.
If the program you are running requires admin rights, the solution I found was using psexec (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx) to run both the program and the bat with elevated privileges. In that case the BAT will look like:
c:\[location]\psexec -h c:\[location]\yourprogram.exe
c:\[location]\psexec -h wscript "C:\[location]\invisible.vbs" "C:\[location]\restart.bat"
Then you run the bat as administrator, or run the second line (without the psexec part) from task scheduler with elevated privileges. BEWARE: running it as a normal user and clicking "no" on the UAC prompt gave me a BSOD, probably because it looped "can't run program because of lacking privileges" a couple of billion times or something :)
You can use RegisterApplicationRestart.
"If you register for restart and the application encounters an
unhandled exception or is not responsive, the user is offered the
opportunity to restart the application; the application is not
automatically restarted without the user's consent. "
For automatic restart without user intervention, there is also RestartOnCrash. Works with all Windows versions.
I was looking for something similar. There are two options to handle this - either you can write a small script by yourself or use something that is already existing.
After some googling I came across this nice list. The blogger has compiled about 8 tools to automatically restart a crashed or closed application.
Unfortunately there are no settings in Windows to automatically restart a regular program when it crashes.
Do you need to actively interact with your application's GUI? Some of the Service Wrappers (designed to run any application as a Windows Service) will monitor your application and restart it when it fails, but be sure investigate Session 0 Isolation to ensure that it won't get in the way.
You may use some special app like BDV SystemEvents or any other. It allows you to specify application which will be started if some another application is closed. Specify the same application as a Condition and as an Action and you will get expected results.

Possible to only initiate scheduled task if shortcut/button clicked?

I've created 2 tasks in Task Scheduler on my Vista PC start uTorrent at 2am then close uTorrent (and shutdown PC) at 7am. However i'd like to only like this task to run if I've clicked a shortcut - ideally show something in the tray as well if possible. But not sure how?
Thanks in advance!
I managed to to something like this myself in windows 7, should be the same in Vista.
To do what you want you could create a scheduled task that starts utorrent, another to close it later and another to then shut down the pc, all timed to run every day at 2am. You would then create a task that runs a batch file to delete the other tasks every time you boot up.
You will have exported the first three tasks as XML files just after you created them and saved them somewhere.
The key is that you then create a batch file that, upon double clicking will create the scheduled tasks by importing the XML files. This will cause the tasks to run as before and shut down your pc afterwards. Next time you boot up your machine the tasks are deleted again by the first batch file.
Read up on batch files (simple command propt commands in a file that run when clicked) and the command-line interface for scheduled tasks.
Type schtasks /? at the command promt for more info.
Hope that makes sense.

Resources