Solved

Shut down second instance, but use commandline

Posted on 1998-12-09
6
300 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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