Solved

Preventing multiple instances when launching from an associated file...

Posted on 2000-03-03
15
874 Views
Last Modified: 2010-05-18
I have a Delphi program (say MyProgram.Exe) that is associated with a particular file extension - lets say '.XXX' files for arguments sake.

When I double click on the file Fred.XXX in explorer, it launches MyProgram.Exe.  This is good.  If I double click another file in Explorer (say Fred2.XXX) it launches *another* copy of MyProgram.Exe.  This is Bad.

I can prevent this simply by putting some code in the start of MyProgram.Exe that looks for another instance running and, if it finds it, sends a message to the other instance to tell it what file to load before terminating itself.

This seems very messy though, and it prevents the user from EVER running two copies of MyProgram.Exe together.

Other programs (such as Microsoft Word) seems to handle this properly, so how do I do it my own software ??

Thanks in advance,

Mac.
0
Comment
Question by:mac_rowe
  • 3
  • 2
  • 2
  • +6
15 Comments
 
LVL 3

Expert Comment

by:ckaneta
Comment Utility
how about having the new instance of the program send the old instance of the program a WM_CLOSE type message and having the old one shut down.
0
 
LVL 1

Expert Comment

by:XAN
Comment Utility
I think you should describe a global variable (App) that will be created in initialization section - something like the next

  initialization
    if not Assigned(App) then
      App := TApp.Create();

  or

you can do the check in TApp.Create()

  finalization
    FreeAndNil(App);

I do so and it works :)
0
 
LVL 6

Expert Comment

by:reddarin
Comment Utility
Wouldn't you be able to check for the command line and if it is a file that is already open by your app just set focus to the open one and exit. An absence of the command line would allow the app to just open normally.

reddarin
0
 
LVL 7

Expert Comment

by:ahalya
Comment Utility
I *think* that explorer will not launch a second instance, if you use DDE to communicate with your app.  You need to process the DDE messages from your app.

The following set of registry key should get you going, i'd think.

[HKEY_CLASSES_ROOT\.XXX]
(Default)  = YourDoc

[HKEY_CLASSES_ROOT\YourDoc\shell\open\command]
(default) = "yourapp /ddeex"

[HKEY_CLASSES_ROOT\YourDoc\shell\open\ddeexec]
(defualt) = "Your DDE Commands if your app is already running"

[HKEY_CLASSES_ROOT\YourDoc\shell\open\ddeexec\ifexec]
(defualt) = "Your DDE Commands if your app is NOT already running"

I think this last registry key is optional. if it's not specified Explorer will use the same DDE commnds from the "ddeexec" key.
0
 
LVL 1

Expert Comment

by:XAN
Comment Utility
Actually, programs like MS Word use COM-interfaces and OLE objects. To prevent load second instance it uses ActiveObject
At first you need register the OLE-object as ActiveObject, then you can use it so many time as you want
0
 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
0
 
LVL 3

Expert Comment

by:SteveWaite
Comment Utility
Sorry but I may misunderstand your question, however..
Sounds like you are nearly there but you need to sand back a message too.
As you say the second instance sends the file name to the first instance. If the first instance does not have the same file loaded as that in the message then it sends a message back for the second instance to load the new file and stay as a second instance. Otherwise it sends back a message that it has the same file loaded and the second instance will then close.


0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
I think the easiest way is to create a mutex (in the .dpr unit). If your second instance tries to create a second mutex, it fails. Then you can look for your App's window (FindWindow) and send a message to it.
ptm.
0
 
LVL 13

Expert Comment

by:Epsylon
Comment Utility
This mutex stuff is descibed in solution 2 of the link I metioned in my previous comment.
0
 
LVL 1

Expert Comment

by:yk030299
Comment Utility
listen
0
 
LVL 10

Expert Comment

by:ptmcomp
Comment Utility
Just, use this mutex stuff...
0
 
LVL 7

Accepted Solution

by:
ahalya earned 300 total points
Comment Utility
I am making my comment as an answer, so that you can evaluate it.


Others have suggested solutions to avoid a second instance, and AFAIK your question was NOT to always avoid a second instance, but to avoid it at certian times.

I think my solution would do the trick.
0
 

Author Comment

by:mac_rowe
Comment Utility
Adjusted points from 200 to 300
0
 

Author Comment

by:mac_rowe
Comment Utility
Sorry for the amount of time it took me to implement your answer.

I've added an extra 100 points as compensation.

Yes, you were the only one who answered the basic question.

Thanks,

Mac.
0
 
LVL 7

Expert Comment

by:ahalya
Comment Utility
Oh.. you didn't have to.  

Thx anyway.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now