Reputation: 1115
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.
Upvotes: 9
Views: 1949
Reputation: 124746
From the description in your question, perhaps what you want to do is get all instances of a running COM object.
Upvotes: 0
Reputation: 916
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.
Upvotes: 5