Solved

Shut down second instance, but use commandline

Posted on 1998-12-09
6
303 Views
Last Modified: 2010-04-06
Hi!

I want that only one Instance of my program can run. If the user starts a second instance, the commandline from the 2nd should be send to the first instance and the second should be terminated.
This sould be run with D3.

Any ideas?

Bye, Sascha
0
Comment
Question by:otti
[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
6 Comments
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 150 total points
ID: 1350162
I have answered this one a month ago, look it up in the 'answered questions':
'Loading a second file in a MDI app', you'll find it in my profile.

The main trick was...

1. Find out if an instance is running
2. Send the instance a message containing the command line (that's the hard part).
3. Terminate your program.


Your DPR should look similar to this...


    program Demo;

    uses
      Forms,
      Windows,
      Dialogs, SysUtils,
      ufMain in 'ufMain.pas' {fMyMDIMainForm};

    {$R *.RES}

    function CheckPreviousInstance:boolean;
    { Returns FALSE if a previous instance is running... }
    var
      H         : HWND;
      st        : string;
      HFilename : THandle;
      Filename  : pointer;
    begin
      H:=FindWindow('TfMyMDIMainForm',NIL);
      Result:=(H=0);
      if not Result then begin
        st:=GetCommandLine+#0;
        { Get a shared memory block }
        HFilename:=CreateFileMapping(-1, { use paging file }
                                     NIL,
                                     PAGE_READWRITE,
                                     0, length(st), 'SharedFilename');
        Filename:=MapViewOfFile(HFilename, FILE_MAP_WRITE, 0,0,0);
        move(st[1], Filename^, length(st));
        UnmapViewOfFile(Filename);

        MessageDlg(IntToStr(longint(HFilename)), mtInformation, [mbOK], 0);
        SendMessage(H, WM_USER+UM_OpenNewMDIChild, 0, longint(HFilename));
        MessageDlg(IntToStr(longint(Filename)), mtInformation, [mbOK], 0);
        CloseHandle(HFilename);
      end;
    end;

    begin
      Application.Initialize;
      if not CheckPreviousInstance then
        EXIT;

      Application.CreateForm(TfMyMDIMainForm, fMyMDIMainForm);
      Application.Run;
    end.
    -------------------------------------------------------------------------------
    And the define a message processing method in your TfMyMDIMainForm

    const
      UM_OpenNewMDIChild = 42;

    type
      TfMyMDIMainForm = class(TForm)
        Label1: TLabel;
      private
        { Private-Deklarationen }
        procedure WMUserMsg(var Msg:TMessage); message WM_USER+UM_OpenNewMDIChild;
      public
        { Public-Deklarationen }
      end;

    var
      fMyMDIMainForm: TfMyMDIMainForm;

    implementation

    {$R *.DFM}

    procedure TfMyMDIMainForm.WMUserMsg(var Msg:TMessage);
    var
      HFilename : THandle;
      Filename  : pChar;
    begin
      { Get a handle to the shared memory }
      HFilename:=OpenFileMapping(FILE_MAP_READ, true, 'SharedFilename');
      { Get a pointer }
      Filename:=MapViewOfFile(HFilename, FILE_MAP_READ, 0,0,0);
      { Do something with the filename... }
      Label1.Caption:=Filename;
      { Release pointer }
      UnmapViewOfFile(Filename);
      { Release handle }
      CloseHandle(HFilename);
    end;
--------------------------------------------------------------------

CheckPreviousInstance looks for a class with the name of your programs main window (so this has to be unique!). Then a shared memory block is created and the command line written in it. After that a message is posted to the first instance of your program. The message is processed by your main form.

I did not include any code for creating mdi-childs, loading file etc. for I'm shure you already have written some.

If you want more information on memory mapped files there is Don Taylor et. al., 'High performance delphi 3 programming', Coriolis Group Books 1997

Hint:
The program searches for the classname of the main window. As long as your form defining this window is loaded in delphi there already is a form with this name. So, if you want to test this, close your main form's unit first (just closing the form is not enough - it is only invisible, but still created)!

Roadrunner

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350163
hi otti,

there is a component that prevents second instances when dropped at your application:
(free, with source)

http://www.torry.webnorth.com/vcl/system/gmjustone.zip

meikl
0
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1350164
Hello Kretzschmar,

I looked into your recommended component - yes, it prevents a second instance, but it does not hand on parameters - and that's the tricky part. Maybe I should build such a component myself with the added functionality...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350165
well then roadrunner

take a look to this free component (with source and example)

http://www.torry.webnorth.com/vcl/system/maininst.zip

meikl
0
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1350166
Hello meikl,

yes, that one seems do do the trick - they also figured a more elegant way of passing the parameter. Anyway, I learned a lot in answering that question - and that's the real value of participating in ExEx.

Hello Otti,

please consider rejecting my answere and try testing the component meikl pointed out.
0
 

Author Comment

by:otti
ID: 1350167
Hi!

Roadrunner will get the points. Such a detailed answer must be honored. Thank you roadrunner - it does what i need!

To Meikel: Thanks for the hint at Torry's page. I will try it.

Bye, Otti
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 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