Solved

Loading a second file in MDI-App

Posted on 1998-10-16
8
157 Views
Last Modified: 2010-04-06
I registered a file-type XYZ with my MDI-application,
so that a file can be loaded from Windows Explorer with a double-click.
If my app is already running and a second file is opened
via double-click, a second app is being opened.
But I want a second MDI-child to be opened in the first app.
Can someone give me a source sample for this ??
I don't know if this is a hard question, but I need a
quick (and working) answer, so it's worth the points for me.
0
Comment
Question by:Tulip
  • 5
  • 3
8 Comments
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1343143
Hello Tulip,

what you need is a three step procedure...

1. find out that another instance of your program is already running
2. send a message to the first instance of your program
3. process the message (i.e. open the file in another MDI window)

I'm preparing an example and will mail it in soon

Roadrunner
0
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 200 total points
ID: 1343144
Back again

I always wanted to know how to share memory between applications...

so here it comes...
-----------------------------------------------------------
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


Thanks for the question, I learned a lot answering it!


Roadrunnner
0
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1343145
One thing I forgot... this was done under Windows NT 4, I could not test the code with 95/98 - please tell me if it worked on your OS!
0
 

Author Comment

by:Tulip
ID: 1343146
Hi Roadrunner!

Thanks for your detailed answer, it looks very good.
I'm gonna try it out tomorrow because for today I'm running out of time.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1343147
There is one additional hint that slipped my mind when answering:

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)!
0
 

Author Comment

by:Tulip
ID: 1343148
Thank you Roadrunner !
I tested the source and it works perfectly. Since the Delphi-made source code comments
are in German, I suppose you are living in a German speaking country, just like me.
So again:  danke schoen
0
 

Author Comment

by:Tulip
ID: 1343149
Oh, I forgot to tell you my OS:

Win NT 4 with SP3
0
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1343150
Gern geschehen
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IExtractImage Delphi 14 198
Help on project with Soap 10 47
LAN or WAN ? 11 81
Dynamically Created Query 3 49
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 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

14 Experts available now in Live!

Get 1:1 Help Now