Solved

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

Posted on 2000-03-03
15
875 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Activex get website that activex running on 3 85
Wininet read php file from internet issue 6 83
Mydac connection data base issue 3 140
Dynamically Created Query 3 50
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

23 Experts available now in Live!

Get 1:1 Help Now