views:

539

answers:

3

i have been trying the code using mutex but im unable to open my exe after button click im successful in not making the multiple entries of the application on the taskbar at button click but my application is launched only when i close my form.. i want to launch my application on button click and if the application is already launched then i need to focus on the previous running application.. how could i able to resolve my need to launch as well as focusin and reopening that application again.. im sending u my code that im using on button click event and plz modify my errors...

  • coding at program.cs

    static void Main() {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
    
    
    
    Application.Run(new Form1());
    System.Diagnostics.Process.Start("filename.exe");
    

    } :

  • coding done at form1.cs

:

private void button1_Click(object sender, EventArgs e) {

        try
        {

            bool createdNew;
            Mutex m = new Mutex(true, "e-Recording", out createdNew);
       System.Diagnostics.ProcessStartInfo f = new System.Diagnostics.ProcessStartInfo("C:\\windows\\system32\\rundll32.exe", "C:\\windows\\system32\\shimgvw.dll,ImageView_Fullscreen " + "filename.exe".TrimEnd(null));

if (createdNew) Launch();

       else
                  {

           MessageBox.Show("e-Recording is already running!", "Multiple Instances");


       }

        }
        catch (Exception ex)
        {

            System.Diagnostics.Debug.WriteLine(ex.ToString());

} }

A: 

I posted an answer a while back to a question about Delphi. I explained that I didn't have a background in Delphi but I described at a high level what I did in C# to build a component that uses InterProcess Communication (IPC) with .NET remoting to not only activate a running instance, but also forward the command line parameters from the second instance into the first instance. I linked to a pretty simple to use component that wraps all this functionality up. It may be useful to you.

Hers's my answer from the other question:

The best way to do this is actually in the the startup code of your exe. In other words, let Explorer launch a second copy of the exe which then proceeds to detect that it is already running and have it send a message to the running instance.

Personally, I have practically no experience with Delphi, but the way I did this in a .NET application was using a mutex and an interprocess communication channel.

The general idea was that the first instance of the application would start, and begin listening on an IPC channel. It would also create a named interprocess mutex. When the second instance launched, it would be unable to create the mutex of the same name which meant that a previous instance was running and listening for calls on the IPC channel. The second instance then sent the command line arguments to the first instance over IPC and the first instance took action on them. The second instance then exits without showing any UI.

I've uploaded the code for this component (C#) and the link is below. I don't believe it has any external dependencies and I don't know what the equivalent communication mechanism in Delphi would be - but hopefully this gives you some ideas.

InstanceManager Component (C#)

Josh Einstein
thanks for ur answer......as im new to delphi so i was unable to understand the code..thats why i need the code in c#.net
zoya
I think you misunderstood what I said... The code *is* in C# but the other person's question was about Delphi. I don't use Delphi either.
Josh Einstein
A: 

This is be able to find and switch to an already running process that matches what you are trying to start.

[DllImport( "user32.dll" )]
public static extern bool ShowWindowAsync( HandleRef hWnd, int nCmdShow );
public const int SW_RESTORE = 9;

public void SwitchToCurrent() {
  IntPtr hWnd = IntPtr.Zero;
  Process process = Process.GetCurrentProcess();
  Process[] processes = Process.GetProcessesByName( process.ProcessName );
  foreach ( Process _process in processes ) {
    // Get the first instance that is not this instance, has the
    // same process name and was started from the same file name
    // and location. Also check that the process has a valid
    // window handle in this session to filter out other user's
    // processes.
    if ( _process.Id != process.Id &&
      _process.MainModule.FileName == process.MainModule.FileName &&
      _process.MainWindowHandle != IntPtr.Zero ) {
      hWnd = _process.MainWindowHandle;

      ShowWindowAsync( NativeMethods.HRef( hWnd ), SW_RESTORE );
      break;
    }
  }
 }
JDMX
A: 

Note that usage of named mutexes is discouraged for security reasons. Any process (even one running under guest account) can create a mutex with the same name before your process was started. Solving these security problems is usually harder than just not using named mutex at all.

To solve your problem, you just need to store process handler or process ID and then look for a window with that process ID. This is similar to the way task manager works.

Vladimir Lifliand