Solved

Loading a second file in MDI-App

Posted on 1998-10-16
8
155 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

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

19 Experts available now in Live!

Get 1:1 Help Now