Detecting Multiple Instances And Passing Variables

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Everydaydiesel
Earned some good credits
Earned some good credits
Posts: 125
Joined: Wed Oct 28, 2015 9:48 pm

Detecting Multiple Instances And Passing Variables

Post by Everydaydiesel »

Hello,

I have an application that I want to have only one instance running. I have the following code that is able to detect the 2nd instance but I am having trouble passing the command line parameter from the second application to the first.

Code: Select all

#include <wx/snglinst.h>

void Application_Dialog::OnInit(wxInitDialogEvent& event)
{
    wxSingleInstanceChecker m_oSingleInstanceChecker;
    if (m_oSingleInstanceChecker.IsAnotherRunning())
    {
        if (wxTheApp->argc > 1)
        {
            // pass this into first instance wxTheApp->argv[1]);
            wxTheApp->ExitMainLoop();  // exit this second instance
        }
    }
}

Example of what should happen.
1. First instance is started
2. Second instance is started
3. The command line parameter from 2nd instance is passed into 1st instance
4. 2nd instance exits

Does anyone have any ideas on how I would achieve this?

Thanks in advance.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Detecting Multiple Instances And Passing Variables

Post by PB »

I think you need some kind of IPC. Check the IPC sample to see an example.

EDIT
I know nothing about this code except that it exists but it is supposed to do what you asked for
https://github.com/XPsoud/wxSnippets/tree/master/Ipc
Post Reply