Posts Tagged ‘RunOnlyOne’

As I’ve said before, the Windows Vista system automatically reserves the Windows’ Logo + Number key combinations for the shortcuts on your Quick Launch bar, so that you can easily launch your Quick Launch applications. That is, to start the first shortcut on your Quick Launch bar, you just press Win+1, and to start the second, you press Win+2 … and so on.

The problem is that pressing this hotkey always runs a new copy of the application (although obviously some applications have their own system for making sure only a single app is running, so for instance, you won’t get multiple copies of Windows Media Player) ... so I wrote a little application called RunOnlyOne which solves this problem. RunOnlyOne allows you to call it with an executable as an argument and it will either run that application or switch to it if it’s already running, making sure you only have a single copy running. You can use it in shortcuts on your Quick Launch bar, but also in geoShell shortcuts and hotkeys, Start Menu shortcuts, or even explorer desktop shortcuts (did you know you can assign a “Shortcut Key” to any shortcut in your Start Menu or on your desktop?).

I’ve actually just posted a new version of it, so if you downloaded it earlier (after my first post) you should go ahead and get the new version: the main change is the addition of a new command-line argument: -proc: which allows you to specify the name of the process module to look for, in case you want to run a shortcut or “launcher” app, but check for something else to be running.

For instance, if you want to run PowerShell, you probably want to run it via the shortcut which has font settings, window sizes, etc. So if you want to make sure you only keep one instance running, you can just specify the command line as: RunOnlyOne.exe -proc:PowerShell.exe "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Windows PowerShell 1.0\Windows PowerShell.lnk" You might have to specify the full path to RunOnlyOne.exe, and if you’re not on Vista, the path to the PowerShell.lnk file is probably different.

At any rate, I thought I’d share the source code and a couple of pointers about how to do this sort of thing. The source code includes a copy of some Iterator classes (two files: EnumProc.cpp and EnumProc.h) which were part of an MSDN Article back in 2002 … and basically uses them to do it’s magic:

  1. Loop through all the processes
  2. Check the first module from each process against either the -proc argument, or the name of the executable in the command-line path …
  1. If we find a match, pick the first window in the process and call SwitchToThisWindow, the Windows’ API for activating application windows.

The only real trick — apart from using the iterators — is that this is a “Windows” application, not a command-line application, so I had to remember to make a call to get the command-line arguments: int argc; LPWSTR *argv = CommandLineToArgvW ( GetCommandLineW(), &argc );. If you run it with no arguments, it pops up a MessageBox which shows the parameters which it accepts:

  • -? to show help
  • -s to RunAs Administrator (still only runs a new copy if the application is not already running, but you can bypass this by using the -proc argument to pass something impossible, like -proc:???)
  • -proc to specify the process module name, like: -proc:Explorer.exe.
  • the first argument after the flags should be the path to what you want to run
  • any following arguments will be passed to that application as arguments

As a side note: despite what you might have thought from my previous article, this is not a Vista-only application, it should run fine all the way back to Windows 2000 at least. Also: I’m not 100% sure what would happen if you call this to try to ‘sudo’ a command line app or any other app which has no window — it may crash (the other application I announced yesterday (sudo) doesn’t check for windows, so it’s a better choice for that).

On Windows Vista, the system automatically reserves the Windows’ Logo key + Number key combinations for Quick Launch, which means that you can easily launch any of your Quick Launch applications. That is, to start the first shortcut on your Quick Launch bar, you just press Win+1, and to start the second, you press Win+2 … and so on.

Run Only One

Anyway, this made me think that it sure would be nice to have the functionality I’m used to with geoShell via the QuickLaunch hotkeys — regardless of whether geoShell’s actually running or not. So I wrote up a little app called RunOnlyOne which takes another application (and any command-line parameters for that app) as arguments and either starts that app, or activates an existing instance of it.

In other words, if you drop RunOnlyOne.exe into a folder on your computer (ideally in your path) ... and put a shortcut to Firefox or IE on your QuickLaunch bar …. you can then right-click the Firefox shortcut and edit the Properties ... put (the path to) RunOnlyOne.exe at the front of the “Target” box, and make sure you edit the icon (RunOnlyOne has no icon, and you want to make sure the icon still points at your application, anyway). Now you can click that button (or press it’s Win+# hotkey as I mentioned earlier) and it will start Firefox (or IE) — but if you click it or press it’s hotkey again later, you’ll just activate the running application, instead of starting another copy!

While I was working on that, I thought it would be nice to be able to run things as administrator using that same tool, so I put in a “-s” command-line parameter, so if you call RunOnlyOne.exe -s Notepad.exe for instance, (on Vista) you’ll be prompted to elevate, and then Notepad.exe will run with administrative rights.

Vista Sudo

Having done that … I went ahead and made a separate app: Sudo which basically does the same thing, but without the “OnlyOne” restriction, so you can use it to force-start anything as administrator. Of course, you could do that from the Start Menu searchbox by pressing Ctrl+Shift+Enter anyway, but it never hurts to have more ways. Also, sudo has a “-multi” command-line parameter that allows invocations like this:

Sudo.exe -multi "Notepad.exe C:\Windows\system.ini" "Notepad C:\Windows\win.ini"

The idea is that if you run Sudo -multi you’ll get a very ugly Unidentified Application dialog instead of the usual elevation prompt, but you’ll only get it once, because once you allow Sudo to run as administrator, it will run each application in the command line as though you’d elevated each one individually. Again, there’s lots of other ways to do this (like running an administrator level PowerShell window and launching everything from there) but I’m of the opinion that there can’t be too many ways to skin a cat.

Source code to RunOnlyOne and Sudo.

Search My Content