Link to home
Start Free TrialLog in
Avatar of LeTay
LeTay

asked on

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 ?
Avatar of BlackTigerX
BlackTigerX

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.
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
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.
ASKER CERTIFIED SOLUTION
Avatar of BlackTigerX
BlackTigerX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
maybe the code in this EE Question may help you?

https://www.experts-exchange.com/questions/20623659/PostMessage-returns-wierd-string.html

ask questions if you need more information
Avatar of LeTay

ASKER

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
Avatar of LeTay

ASKER

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 !