tags:

views:

707

answers:

6

What's the best way to pass data from one Windows Forms app (an office plugin) to another (exe written in C#) in C#?

+2  A: 

Interprocess communication in .NET

John
this is not the same question... my question relates to two different apps communicating, not the same app running multiple times needing some central state object
OS
@OS: I'm not sure that makes a huge difference...
Marc Gravell
+1  A: 

WCF is one of the easiest ways to do this. You can register a service endpoint in each app, or if one is the service and another is the client you can just use a single service host.

Check out the getting started tutorials.

Aaron Weiker
+1  A: 

There are numerous ways to achieve this. I've used named pipes in the past. Support for named pipes are also now built into .NET (as per 3.0?) so it is fairly straight forward.

Ty
+5  A: 

I'll take a wild stab at this and say you probably want the office app to phone home to your exe? In this context, the "exe" is the server and the office app is the client.

If you're using .NET 3.0, WCF is likely your best bet. I would structure the solution into three parts:

  1. "Shared Contracts". These are interfaces that describe your services. If you have custom data objects that will be passed between the applications, they should be defined in this assembly as well. This assembly is shared between the client and the server. See "Designing Service Contracts" for more info.
  2. "Service". This assembly is your "exe" and it will reference the contracts and define the classes based on your service contracts. Your app will also host a ServiceClient for your service. The configuration file for this app will define how your ServiceClient will be exposed to the client (available as a web service, tcp, etc). See "Implementing Service Contracts" for more info.
  3. "Client". Your plugin will reference the "Shared Contracts" assembly and will contain service-clients based on the contracts. The client can be auto-generated using the svcutil.exe tool.

Both the "exe" and the "plugin" will require configuration files that define the bindings.

When you want to pass data between client and server, your client will create an object from the "Shared Contracts" assembly and pass it to the service-client. The client's configuration file will figure out where to send the data.

For a step-by-step tutorial on how to create a basic WCF service, check out this Tutorial.

bryanbcook
+1  A: 

old-school but it works - using WM_COPYDATA to send messages between windows apps

Steven A. Lowe
A: 

Yup. WCF is the way to go. I recommend checking out iDesign.net to use the InProcFactory class. You can shim up your office class into a service and call into your other application which is hosting a service. The other service can then call back into the office based service.

You can use an IPC endpoint which will make the communication snappy.

-Scott