• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

single program instance to run when clicking on associated files

I have written a Delphi program.
I have associated it with "my" own file .fbn
When I click a .fbn, wonderful, my program starts, detect it has a param (the file) and open it accordinlgy
But now, my program is running, and when I click a second .fbn, a second instance of my program starts
Can I forbid that and in fact, get the "open this other file" info in my running program so that it can take action with it ?
0
LeTay
Asked:
LeTay
1 Solution
 
BlackTigerXCommented:
the first thing would be to prevent multiple instances:

uses
  Windows,Forms,
  Unit1 in 'Unit1.pas' {Form1};


var
  Mutex : THandle;
{$R *.RES}

begin
Mutex := CreateMutex(nil, True, 'YourApplicationUniqueName');
if (Mutex <> 0) and (GetLastError = 0) then
  begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
  if Mutex <> 0 then
    CloseHandle(Mutex);
  end;
end.
0
 
BlackTigerXCommented:
then you need to define a message on your form, which when received opens the file, I assume you have some kind of MDI application
0
 
JDSkinnerCommented:
You could use the approach indicated on the link below
http://delphi.about.com/od/windowsshellapi/l/aa100703c.htm

or you could just use a component from the ABF Software, if the program is for non commercial use
it is provided free of charge.
http://www.abf-dev.com/abf-components.shtml

The component to use is abfOneIstance, drop a copy of this on your form to catch a subsequent program copy.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
BlackTigerXCommented:
here's an example on how to do that

http://delphi.about.com/od/windowsshellapi/l/aa100703b.htm

and here's an example of sending strings in a message, which is what you need to specify which file would need to be open

http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm
0
 
Slick812Commented:
maybe the code in this EE Question may help you?

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20623659.html

ask questions if you need more information
0
 
LeTayAuthor Commented:
Hello BlackTigerX !
Your references look interesting
I have first tried the first one you suggest, writing a simple program
Unfortunately, the first instance of the program does not receive the message sent by the second one...
I can post it to you if you want to have a look at it
Then I tried the other approach, which looks better and which is to send a message directly to the "good" application (so the first instance)
I get a compilation error on this : procedure WMCopyData(var Msg:TWMCopyData); message WM_COPYDATA;
The error is : unknown directive 'message'.
No idea how to correct this.
I am running Delphi 5.
Thanks to help
0
 
LeTayAuthor Commented:
Sorry about this, I think I found the trick for compilation
I didn't declare the procedure as a private method of the form
Now it works
I still need to check and then, maybe, give you the points !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now