Application Start Before Windows Explorer - windows

Some installation applications stop (or appear to stop) the normal windows booting. The computer starts, the user logs in and then the installation program starts before others (like Windows explorer).
How can I replicate this behaviour in my own program?
E.g.
OS Boot
Login
The program runs, updates etc.
The rest of the programs run (e.g. windows explorer and what ever runs on startup)

If you want to start an application before the shell starts, you can add a value to the Userinit value in the registry. In this key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
There is a value named Userinit. Change it so your program is run before userinit.exe. For example, to start notepad before the shell/everything else is initialized:
C:\WINDOWS\system32\notepad.exe,C:\Windows\system32\userinit.exe
Use commas to separate the programs that should be started.
This works for Windows XP, Vista, and 7.

I have not tried it but I assume that this is done by the registry entry
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce\Setup
There are even more registry keys - see the complete list documented here:
Definition of the RunOnce Keys in the Registry
But for your use case I would recommend to start your application as desktop shell similar to the proposed solution by "vcsjones". When your program has finished you can call explorer.exe to start loading the regular desktop.

You could possibly wrap a windows batch file .bat around explorer.exe
#echo off
something.exe
explorer.exe
But that wouldn't really make sure it's always started before explorer.exe
You could possibly change some registry value to select another 'shell' instead of explorer for that . .
If that is what you are actually looking for.

You should have a look at Windows Task Scheduler. Tasks can be scheduled to execute when a user (specific or any) logs on. The UI provided out-of-the-box by Windows illustrates what can actually be done with this standard Windows feature.
Another solution is to write a Windows service (the .msi Windows Installer is in fact a service).
I'm not sure you will be able to block Windows Explorer though...

Related

Windows cmd.exe detaches from processes after creating them

I'm trying to run some dynamic analysis routines on Windows programs, using things like Intel Pin and DynamoRIO. I am running Windows 11. A problem I am having is when I use cmd.exe to launch an interactive application, control returns immediately to the interpreter after the application has been launched. This means those apps don't get analyzed properly.
For example, if I use DynamoRIO to instrument the File Explorer like this:
drrun.exe -t drcachesim -- explorer.exe
Then DynamoRIO returns immediately after explorer.exe is launched, and it doesn't actually instrument File Explorer when the user can interact with it.
I've had similar issues with Intel Pin. It seems the root issue is cmd.exe detaches from processes immediately after launching them. I am a complete novice Windows developer, so there is probably some mechanism I am unaware of that is causing this. I would be grateful or any insight or feedback here.
Two things to note here.
cmd.exe waits for console programs but not GUI programs. Use the start command to wait on GUI programs: start /wait notepad.exe.
Explorer.exe will often communicate with an existing instance of itself and then just exit. Exit Explorer and then use Task manager to kill all Explorer.exe instances and then run your command.

StartUp Folder Removed but Applications still start up automatically

I accidentally removed my Startup Folder in AppsData so the StartUp Tab in my Task Manager is empty. However, some applications still start up automatically. What do I do in order to stop these applications from starting up? I'm afraid that my BIOS will take a long time to start up once I download more programs in my PC.
There are several places in Windows which specify start-up programs. You can see the full list using the msconfig.exe utility.
More details here: https://www.howtogeek.com/74523/how-to-disable-startup-programs-in-windows/
Also, note that some programs register themselves as "services" and started by the Windows Service Manager, so you might take a look on "Control Panel"->"Administrative tools"->"Services". But be careful, most of the stuff there is essential for normal system operation.

Starting windows apps from cmd using path executables

I have an app that listens to certain process events, such as starting a program and than recording the full commands. I would like to replay starting the programs using the commands as well. For some programs e.g. word, this works very well.
For windows app programs, this does not work. For example, starting calculator:
Command inputs: "C:\Program Files\WindowsApps\Microsoft.WindowsCalculator_10.1709.2703.0_x64__8wekyb3d8bbwe\Calculator.exe"
Access is denied.
I could start the calculator using calc, but unfortunately, I don't have that information in my program. Is there any way to start such a program using the executable path?
I used admin mode for the latter command.
Have you tried running cmd as administrator?
Just kidding ;-)
Actually, starting Windows Store apps (or UWP apps, or whatever they are called these days) is not easy. Finding the correct command line for them is a very long-winded process. It's described here:
https://answers.microsoft.com/en-us/windows/forum/windows_10-windows_store/starting-windows-10-store-app-from-the-command/836354c5-b5af-4d6c-b414-80e40ed14675?auth=1
I know it's bad form to just give links as answers, but on this occasion the answer itself would take pages to write. If the above link disappears, google "Starting Windows Store apps from the command line".
I think cmd running as a normal user it required administrator access, try cms as administrator.

Supporting two shells in windows

I'd like to write my own (very simple) explorer.exe alternative that I could actively switch between without having to restart my computer.
Is it possible to run two shells simultaneosly (or to write a program that temporarily disables the current shell)?
If not, is it possible to stop explorer without it restarting itself, and have my shell start itself instead?
Edit
More info: I'd like to write a simple productivity tool for myself. I want to set up a very simple task manager that prevents me from starting/opening/using anything but a whitelisted set of applications I list ahead of time. Locking me into that set of apps for whatever time period I've set. If there's another (better) way to prevent people from shutting down my app, switching from my app (with alt-tab, etc) I'm all ears.
Note: I'm fine with the app/shell/whatever being escapable by restarting my computer. I just want to make it massively inconvenient to switch to being distracted, and I wanted to learn a bit more about the Windows API.
See this question for details about writing a shell.
No, there can only be one real shell process (SetShellWindowEx only works when there is no other shell process) WH_SHELL can be used by other processes and it might be enough for your needs (Maybe in combination with IShellExecuteHook)
When explorer.exe is started and it detects a different shell it will not display the taskbar, just a file browser window. Explorer also looks at the shell value in the registry IIRC. You might also want to look into the shift to exit trick.

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.

Resources