Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Shut down second instance, but use commandline

Posted on 1998-12-09
6
Medium Priority
?
305 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 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

618 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