14 responses to “Run only one copy of an application”

  1. Tory (aka elem)

    Hey, I remember…you’ve been trying to get this functionality in the taskbar for years in GS. Congrats…I’m thinking of trying this out on my XP system. Now, if we could just get it built in (or hooked into) the Explorer taskbar…

  2. Arno

    I’ve been looking for an application that does exactly this, but without the automatic “switch to”. >> I am building a couple of antique gaming pc’s and would like users to be able to run only one instance of the game (ms-dos based games..) , but if they alt-tab to windows (to change volume or anything) and accidently click the icon again, they should not be auto-forced to switch back to the (fullscreen) game.

  3. K H

    For situation that the prog is being minimized, RunOnlyOne is not able to bring it to front. Can this be done ?

  4. JF

    KH> I’m not really a C coder, but I think you would just need add these lines:

    ::ShowWindow(itw.First(), SW_SHOWNORMAL);
    ::BringWindowToTop(itw.First());

    right after the SwitchToWindow line…that would restore the window if it’s minimized, as well as bring the window to top if it’s open but behind some other window(s).

    Joel – do you think you could update the code & binary to include the changes?

  5. JF

    Actually I just played around with this with VS 2008 Express (as in free). All that is needed is:

    ::ShowWindow(itw.First(), SW_SHOWNORMAL);

    works like a champ. Perfect for use with cygwin.

  6. JF

    Actually, after a little more playing, I discovered what I think is the ‘right way’. Apparently SwitchToApplication messes with the Z-ordering of any applications between the currently focused app, and the app you wish to switch to – like the way Alt-Tab brings each window into focus as you tab through them. Furthermore, if the window is maximized and focused, and you RunOnlyOne again, it shouldn’t restore the window. Anyway, I think I’ve fixed all possible scenarios and have been running the new code and haven’t detected any bugs:

    //add these lines over the process iteration loop
    WINDOWPLACEMENT wp;
    wp.length = sizeof(wp);

    // replace SwitchToThisWindow() with:

    ::GetWindowPlacement( itw.First(), &wp);
    if( wp.showCmd == SW_SHOWMINIMIZED ) {
    ::ShowWindow( itw.First(), SW_RESTORE );
    } else {
    ::SetForegroundWindow( itw.First() );
    }

  7. Daniel

    The program seems to get the error:
    “This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem.”

    Is this a Vista only app? Did now know if there was a dll or such that xp does not have.

    Best Regards,
    -daniel

  8. Doug

    Joel, I was looking for something like this to be able to hotkey an Explorer window to open to a specific directory or to switch back to the explorer window if it was currently open (regardless of the directory I left it at). The problem with RunOnlyOne is that I believe explorer changes the name of the Window each time the directory changes. If this is the case, the only way I could see RunOnlyOne working is if it tracked the process id instead of the name?

    Does this sound like it could work, or maybe there’s just a better way to do what I’m wanting.

    Thanks
    -Doug

  9. Doug

    Actually I did read the article (no need to be pissy). I also tried the proc parameter and it doesn’t work properly with “Explorer.exe” as there is always an instance running, so it will not bring up a window initially when used with “proc”.

    In order to make it work I had to change the code and have it store the process id in the registry. It will try to find that process id running and switch to it. If it’s not valid or another named process is in its place it spawns a new instance and updates the id in the registry.

    With that change and a change to how the focus is reset, it works fine.