Rerunning program just opens a window

I noticed this behaviour in Microsoft's Explorer.exe and some other programs, where if you run the program while it wasnt running, it opens the taskbar and desktop, but if you run the executable when it's already running, instead of opening another time the same application, only one keeps running, and it opens a default folder.

Now, i've a program that when opened creates two windows (2 separate threads), of which one is behind the taskbar and one is normally on screen. The user can close the window on screen, the other one will keep being back there.

What i want to achieve is the following:
the user runs the program again after having closed the main window with the bar-window still runnning => no other instance of the program launches, instead the main window is opened again.

Is this do-able? How? Also is this a windows-only feature or can it be done portably?
Last edited on
The general process works like this:
1
2
3
4
5
6
7
8
9
10
int main(int argc, char **argv){
    if (!system_global_object_exists())
        create_system_global_object();
    else{
        auto other_process = get_existing_process();
        other_process.notify_new_process(argc, argv);
        return 0;
    }
    //run normally
}
The system-global object can be anything, such as a file, a named pipe, a network socket, etc. It just needs to be findable by any process.
Then to notify the original process that a new process was started, you need to use some IPC mechanism to send some data. Again, anything that can send binary data will work: a pipe, a socket, shared memory, etc.
In order for this to work, all you need is being able to listen on a TCP port on localhost, which practically any system can do. Named pipes and named shared memories are typically more versatile because you can segregate different users (e.g. if two users are logged in and one of them has a text processor running, you don't want that when the other user attempts to open a document the first user's process to display that file).

For example here https://github.com/Helios-vmg/Borderless/blob/master/src/SingleInstanceApplication.cpp#L32 I have a portable Qt application where I use QSharedMemory and QLocalSocket to implement a single-instance application on a per-user basis.
thanks for the explanation!
Topic archived. No new replies allowed.