Solved

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

Posted on 2000-03-03
15
876 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 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