Solved

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

Posted on 2000-03-03
15
882 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +6
15 Comments
 
LVL 3

Expert Comment

by:ckaneta
ID: 2582640
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
ID: 2582658
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
ID: 2582667
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:ahalya
ID: 2582678
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
ID: 2582679
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
ID: 2582934
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 2583987
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
 
LVL 10

Expert Comment

by:ptmcomp
ID: 2584773
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
ID: 2584792
This mutex stuff is descibed in solution 2 of the link I metioned in my previous comment.
0
 
LVL 1

Expert Comment

by:yk030299
ID: 2586783
listen
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 2588900
Just, use this mutex stuff...
0
 
LVL 7

Accepted Solution

by:
ahalya earned 300 total points
ID: 2589477
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
ID: 2632742
Adjusted points from 200 to 300
0
 

Author Comment

by:mac_rowe
ID: 2632743
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
ID: 2632778
Oh.. you didn't have to.  

Thx anyway.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

718 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