single program instance to run when clicking on associated files

Posted on 2006-04-30
Last Modified: 2010-05-18
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 ?
Question by:LeTay
    LVL 13

    Expert Comment

    the first thing would be to prevent multiple instances:

      Unit1 in 'Unit1.pas' {Form1};

      Mutex : THandle;
    {$R *.RES}

    Mutex := CreateMutex(nil, True, 'YourApplicationUniqueName');
    if (Mutex <> 0) and (GetLastError = 0) then
      Application.CreateForm(TForm1, Form1);
      if Mutex <> 0 then
    LVL 13

    Expert Comment

    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
    LVL 4

    Expert Comment

    You could use the approach indicated on the link below

    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.

    The component to use is abfOneIstance, drop a copy of this on your form to catch a subsequent program copy.
    LVL 13

    Accepted Solution

    here's an example on how to do that

    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
    LVL 33

    Expert Comment

    maybe the code in this EE Question may help you?

    ask questions if you need more information

    Author Comment

    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

    Author Comment

    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 !

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    APK file name 7 60
    Best Firemonkey component pack 1 67
    Printing problem 2 65
    Delphi IDE crash without error message ... 7 47
    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…
    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…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    746 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

    20 Experts available now in Live!

    Get 1:1 Help Now