1

The steps of my application are:

  1. Go to the setting page first, and the setting page will register the Registry Log (as 'regedit' in command line) in the background (people may seldom go to the setting page).
  2. When users clicks the URL in a web page, it will trigger the registry and open my application.
  3. The appplication reads the parameter that it gets and does things depending on the parameter value.
  4. User may click on different links to send different parameters to my application
  5. That is, if the application is not opened, it should be launched it and reads the parameter. If the application is already opened, it should just read the parameter.

The problem is: how could find out the different situations of my application - whether it is opened or not - and then use the parameter correctly?

The part of registry( in setting page):

Registry.ClassesRoot.CreateSubKey("MyApp").SetValue("", "URL:MyApp Protocol");
Registry.ClassesRoot.CreateSubKey("MyApp").SetValue("URL Protocol", "");
Registry.ClassesRoot.CreateSubKey("MyApp\\DefaultIcon").SetValue("", "\"" + Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\" + "MyApp.exe" + ",1\"");
Registry.ClassesRoot.CreateSubKey("MyApp\\shell\\open\\command").SetValue("", "\"" + Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\" + "MyApp.exe" + "\" \"%1\"");

%1 is the parameter I will get( from url to my application).

And the web link may be:

<a href="MyApp://function1">Call for Function 1</a>
<a href="MyApp://function2">Call for Function 2</a>

So there are many links in the web page to call same application.

But I cannot let my application be opened every time (that is, there should be only one application opened, and other clicks on links will only send parameters to the app).

I know how to find out whether the application is opened or not by the code:

Mutex mul = null;
bool is_createdNew;
try
{
    string mutexName = Process.GetCurrentProcess().MainModule.FileName.Replace(Path.DirectorySeparatorChar, '_');
    mul = new Mutex(true, "Global\\" + mutexName, out is_createdNew);
    if (!is_createdNew)
    {
        // application be opened already, I close the application originally
        Environment.Exit(Environment.ExitCode);
    }
    else
    {
        // the application is first run, open my MainWindow
    }
}
catch (Exception ex)
{
    throw ex;
}
finally
{
}

Is it possible to send the parameter as a method of registry when the application is opened?

I even think about reading registry by Registry.GetValue, when my application starts up,

to use timer to read registry value per second......

This is my first time face this situation of user's request,

hope someone can give me any direction!

Thanks in advance.

Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
Shuinvy
  • 251
  • 2
  • 6
  • 23

2 Answers2

1

When you find out that another instance of your application is already running (which you do in your code above using Mutex), you can programatically pass the parameter (of the second app instance) to the first, already running app instance and then just close the second app instance. This code for passing parameter to the first app instance would then be just before Environment.Exit(Environment.ExitCode);

(Presuming that your app is relatively small and does not loads lots of libraries on startup - in that case it would be better to create a separate small launcher app)

The problem is, how to pass the parameter between two independent processes - two instances of your app.exe. There are of course several options, look here:

Send/Receive message To/From two running application

I would use FileWatcher or Memory mapped file as it is specified in that answer. The solution with timer and changing registry values is not good (registry operations require admin access, registry operations are not so fast etc.).

Here is a nice library that shows, how to pass parameters between 2 processes using Memory mapped file.

http://code.msdn.microsoft.com/windowsdesktop/Inter-process-communication-e96e94e7

Community
  • 1
  • 1
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
0

I found another way to solve my problem,

just simply add 'static' before Mutex.

See the detail: C# static

Shuinvy
  • 251
  • 2
  • 6
  • 23