I wrote a simple batch script that kills explorer.exe and then start it again. Its a common operation that I need for the Windows client software I'm developing.
The negative side effect is that any opened explorer window is closed and I need to manually open it up.
I want to add some pre and post operation to my script that will restore the opened Windows.
Best case would be if we could keep the commands in the form of a batch script, though I suppose it would be much simpler to implement using C#.
Related
I have little previous experience with Windows (for programming, anyway), but recognizing that Windows has an enormous market share, I am trying to support it in my programs (even though they are just for fun, I like to pretend they're big projects). I have written a tiny shell with minimal (and when I say minimal, I mean minimal) features.
I am trying to port it to Windows and would like to use it independently from cmd.exe in a Win32 Console window (meaning the shell part of cmd.exe isn't running at all, but the window used for it shows). I have already done most of the other porting stuff such as build system (CMake) and changing appropriate Unix syscalls to Windows ones in a #define. I have done a little research and found little on this topic, however. I know it is possible because I've seen it done with Bash. Visual Studio also used to do it when I ran a program in its GUI.
Reference article I got some of this info from: https://en.wikipedia.org/wiki/Win32_console
Note: What I mean is when you click on it and it opens it without running cmd in its own little console window. Or when you type it into cmd it opens in a separate window that isn't running cmd. I am assuming cmd.exe and the console window it runs in are two separate things, but if I am wrong, please let me know. :)
This question is inspired by https://askubuntu.com/questions/111144/are-terminal-and-shell-the-same and a similar question where I got that Wikipedia link. Someone said that the console window and the shell were separate. I was writing my own shell so I started to wonder how to make mine independent of the default one.
The Win32 Console and cmd.exe are two different things. Windows automatically opens a console window when a program that needs one is started. It decides whether do to so by switches hard-wired into the executable. This window will be running said program. If the program that started the process is running in a console window, the two programs will share that console window.
As Noodles said, it really is that simple. You just start it. Double-clicking on it will do it. The CreateProcess() function with CREATE_NEW_CONSOLE passed to it will do it. Running the program from cmd.exe with
start <command>
will do it.
There is also a family of functions in the Windows API, called FreeConsole() and AllocConsole() that will free a program from its current console and create a new console for it, respectively.
Reference link (given by Noodles): https://msdn.microsoft.com/en-us/library/windows/desktop/ms682010(v=vs.85).aspx
I have a batch script (yes I know batch is awful, no I don't care) that checks my VM's on the local machine to the ones stored on my USB, if they're out of date, it updates them, then boots them. I use multiple machines at uni, so this makes it easier to ensure the VM I'm working on are always the latest.
When I open them like this;
PATH "%PROGRAMFILES%\VMware\VMware Workstation\"
START vmware.exe -x "%USERPROFILE%\Desktop\VMs\VM1\VM1.vmx"
START vmware.exe -x "%USERPROFILE%\Desktop\VMs\VM2\VM2.vmx"
START vmware.exe -x "%USERPROFILE%\Desktop\VMs\VM3\VM3.vmx"
It causes the VM's to open in separate windows, rather than tabs of the same window for easy switching.
The workaround I came up with is to boot the VMware program first, then when I open the .VMXs, they all open as tabs in the same window.
The problem is that the VMware program sometimes takes a long time to open. Similar to Photoshop's loading splashscreen, but instead with no visual indicator, VMware opens up to 20 seconds after the icon has been clicked, or it has been summoned with a script.
So finally, here is my question.
Is there a way to make a batch that waits for the program to open before continuing? I know by omitting START I can stop the batch until the program closes, but obviously this is useless for my purposes.
If all else fails, I may just have to include a 30 second timeout and hope it's enough.
I don't think there's any reliable way to do this.
If you have a program that you need to open and then wait for a certain state to change in the program before doing something else, that state could be set by any arbitrary operation running on any thread spawned by the application. There would be no way to know when the application had set that state unless you were able to somehow communicate with the program to query or be notified of its state.
Theoretically if you knew enough about what the program was doing internally you could monitor the thread count or file system accesses or something to determine roughly when it had changed to the desired state, but just using a timer would be much much simpler.
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 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.
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...