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

x
?
Solved

Loading a second file in MDI-App

Posted on 1998-10-16
8
Medium Priority
?
167 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
[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
  • 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 800 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
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.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

610 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