views:

1296

answers:

3

Hello!

I have a .NET C# WPF application that I am trying to make into a single-instance application using a Mutex.

This .NET application is called by a C++-based DLL using CreateProcessAsUser() and is given parameters via environment variables.

Subsequent instances will also be created by the C++ DLL in the same way.

Subsequent instances would then need to pass their parameters to the first instance of the application before exiting.

The problem is what methods can be used in the .NET application so that the subsequent instances would be able to pass their data to the first instance of the .NET application? The simpler, the better.

I have researched some but I hope there are simpler ways.

Things I have researched:

  • Named Pipes
  • .NET Remoting
  • Windows Messaging (Sending WM_COPYDATA to the first instance window)

Since I am just trying to pass 4 strings to the first instance, I am trying to avoid the above mentioned methods because they are somewhat overkill for my problem.

The simplest I can think of is to export a function from the .NET application so that the subsequent instances of the .NET application can just call this function on the first instance of the .NET application and pass the data as the parameters of the function. However, is this possible in .NET? I've read that .NET EXE or DLLs could not export functions.

Thanks!

+1  A: 

The simplest I can think of is to export a function from the .NET application and then the subsequent instances can just call this function and pass the parameters to it.

This is not how this works. You'll load the .NET assembly in the calling process, not magically cross the process boundary and talk to the child.

Just have the parent open the child with redirected pipes using the Process class, and have the child read from stdin using Console.Read*

Paul Betts
A: 

thanks for the reply, Paul!

I've added more detail to my question above, though, coz I'm not sure if my scenario was understood correctly.

But regarding your answer, the parent of the .NET app will be a C++-based DLL and all it will do is call the .NET app and give it parameters. The C++-based DLL will also exit after this so I wouldn't want to add anymore behavior to it.

So, passing of data would then be done between the instances of the .NET applications only.

A: 

Since you are going from .NET to .NET, I'd recommend just doing a WCF call. You can use a named pipes transport between the two .NET instances to expose the "service" (which is what your first instance would expose).

Subsequent instances would do the single instance check, and if they detect an already running instance, they could make a WCF call to the service running in the first instance and pass the data that way.

Jeremy Wiebe