Solved

Shut down second instance, but use commandline

Posted on 1998-12-09
6
297 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
  • 3
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

11 Experts available now in Live!

Get 1:1 Help Now