views:

272

answers:

3

Hi,

I want to check for the existence and if it doesn't exist add some actions in the Explorer's right click (context) menu.

Besides of the above requirement (for which one can find easily solutions on web) I want to add one more:

Suppose that I register the following commands:

  • "Command #1" - triggers 'C:\MyProg.exe /cmd1'
  • "Command #2" - triggers 'C:\MyProg.exe /cmd2'

... (aso.)

How do I check if there is already a running instance of MyProg.exe and passing it the appropriate command/command line parameter together with the filename on which the user clicked?

(IOW, I want to use the already running instance of my program to do my task and not to open a new one)

A Delphi code snippet would be appreciated.

TIA

+1  A: 

There are two ways to add items to the context menu.

Registry

This method is easy since it comes down to adding some registry keys. The downside is that you can't put any logic in it. You can read about it here and here a simple example in Delphi. You get a bit more control if you are using DDE to execute the menu items. See here for a Delphi example. If DDE doesn't solve your 'already running' problem you could try and have your applications communicate with each other trough some way of IPC.

Shell Extension

This method is a bit more work, but you can completely control the context menu from code. You would have to write a DLL, implement IContextMenu (or others) and register the dll with Windows Explorer. You can read about it here. You could also check out Shell+.

Lars Truijens
+3  A: 

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
+2  A: 

Here are some examples: http://delphi.about.com/od/adptips2006/qt/activaterunning.htm

Torbins