Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to multiple running instances of an application

Tags:

c#

winforms

I'm trying to create a connection to multiple instances of an open application (WRQ Reflection). Connecting to the first instance that was opened is fine using this:

Session appInstance = (Marshal.GetActiveObject("Reflection4.Session.8") as Session);

But I'd like to be able to connect to multiple instances. I've been doing alot of research and found some helpful links such as this, but that solution wont work in this situation as all the open instances have the same progId.

I've also tried looking at the window handles, which are obviously different for each instance. Using this:

Process[] processes = Process.GetProcessesByName("r4win");
foreach (Process p in processes)
{
    IntPtr windowHandle = p.MainWindowHandle;
    string handle = windowHandle.ToString();

    MessageBox.Show(handle);
}

But I haven't been able to figure out how to create a connection to the window via the window handle.

Any assistance is appreciated.

Additional Code:

void TestROT()
{
    // Look for open instances
    string[] progIds = {"Reflection4.Session.8"};

    List<object> instances = GetRunningInstances(progIds);

    foreach (object refleObjs in instances)
    {
        Session session = refleObjs as Session;
        session.Transmit("12345");
    }
}

For this scenario, I have 2 instances of the target application running. In the above code, it will send the string 12345 to the same instance of the application, twice.

I need it to send 12345 to the first instance, and then 12345 to the second instance.

like image 707
Kieran Quinn Avatar asked Nov 06 '15 09:11

Kieran Quinn


People also ask

How do I open multiple instances of a program?

Open multiple instances of an app using Shift + Click First, open the application you want to run in multiple instances. Then, hold down the Shift key on your keyboard and click with your cursor - or tap with your finger - on its taskbar icon.

Can you have multiple instances Visual Studio?

To open a second instance of the integrated development environment (IDE), right-click on the Visual Studio icon in your dock or Applications folder, and select New Instance.

How do I block multiple instances of an application?

include the android:launchMode="singleTask" and it will not be possible to have multiple instances of your Activity launched at the same time.


1 Answers

You can use the classes NamedPipeClientStream and NamedPipeServerStream in the System.IO.Pipes-namespace to send data from your application to another one. In your first application implement a NamedPipeServerStream like this:

NamedPipeServerStream pipeServer = new NamedPipeServerStream("MyApp1");
pipeServer.WaitForConnection();//wait for connection of client

Place an instance of NamedPipeClientStream in your second application:

NamedPipeClientStream clientStream = new NamedPipeClientStream("MyApp1");
clientStream.Connect();

After connecting the client to the server you can send the data by using the methode Write:

Sending data from your server:

byte[] test = Encoding.UTF8.GetBytes("Hello World");
pipeServer.Write(test, 0, test.Length);

Sending data from the client:

byte[] test = Encoding.UTF8.GetBytes("Hello World");
clientStream.Write(test, 0, test.Length);

You can use Read to get the received bytes. This method exists in both classes.

You also can use more than one server and one client at the same time to communicate with more applications.

like image 64
daniel59 Avatar answered Oct 07 '22 12:10

daniel59