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
Solved

Shut down second instance, but use commandline

Posted on 1998-12-09
6
301 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Delphi Dbf export problem to a Visual Foxpro application 6 207
Delphi - replicating a form 8 81
can't find the executable in Simulator 1 100
Firemonkey BASS_Init into a thread 17 30
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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 …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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