Explorer Integration in the Context Menu but using the already running instance

蹲街弑〆低调 提交于 2019-12-01 11:19:00

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+.

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#)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!