?
Solved

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

Posted on 2000-03-03
15
Medium Priority
?
883 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 900 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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