Board index » delphi » Avoiding 2 Coplies of my program, and telling the older 1 to do someting

Avoiding 2 Coplies of my program, and telling the older 1 to do someting

I found a way to determine if a program is already in memory. So if I
know that my program is already running, how can i tell my program (from
the new copy and then exiting the new copy) to do something. I need to
tell my program to load a file, and i need to pass the filename to the
program.

If there any one who can help me I would appreciate it, an example will
be great.

Thanks
??? Oren

--
?????????????? \|||/
?????????????? /'^'\
????????????? ( 0 0 )
----------oOOO--(_)--OOOo----------
Oren Nahum
E-Mail : OrenNa...@SoftHome.Net
WWW: WWW.AngelFire.Com/Tx/OrenNahum
ICQ: 1100822
Israel
-----------------------------------
?

 

Re:Avoiding 2 Coplies of my program, and telling the older 1 to do someting


Quote
Oren Nahum <OrenNa...@SoftHome.Net> wrote:

: I found a way to determine if a program is already in memory. So if I
: know that my program is already running, how can i tell my program (from
: the new copy and then exiting the new copy) to do something. I need to
: tell my program to load a file, and i need to pass the filename to the
: program.

Just an idea...

From the "new" copy, allocate string memory and initialize it with the
desired filename.  Then send a user-defined message to the "old" copy,
passing the string's memory address in lParam, and then terminate.  When
the "old" copy has processed the string, it should deallocate the string's
memory on its own.

Won't this work?

- Mike

Re:Avoiding 2 Coplies of my program, and telling the older 1 to do someting


Quote
Oren Nahum <OrenNa...@SoftHome.Net> wrote:
>: I found a way to determine if a program is already in memory. So if I
>: know that my program is already running, how can i tell my program (from
>: the new copy and then exiting the new copy) to do something. I need to
>: tell my program to load a file, and i need to pass the filename to the
>: program.

Here's what I do (Delphi 1 and 2):

My main program (DPR) looks like this:

begin
  Application.Title := 'Anon';
  ClipBoard.Clear;
  filename:=ParamStr(1);
  ViewSOLmsgID:= RegisterWindowMessage('ViewSOLmessage');
  H:=FindWindow('TApplication','ViewSOL');
  AlreadyRunning:=(H<>0);
  if AlreadyRunning then begin
   if (filename<>'') then ClipBoard.AsText:=filename;
   PostMessage(HWND_BROADCAST,ViewSOLmsgID,666,666);
    exit;
  end;
  Application.Title := 'ViewSOL';
  Application.HelpFile := 'ViewSOL.hlp';
  Application.CreateForm(TMainForm, MainForm);
  Application.OnMessage:= MainForm.HandleAppMessage;  {note this}
  Application.Run;
end.

Then in the unit for the main form I have:

procedure TMainForm.HandleAppMessage(var Msg : TMsg; var Handled : Boolean);
var
  Msg1 : TMsg;
 FileName : array[0..200] of Char; newfilename : String[255];
begin
 newfilename := '';
 Handled := False;
 if (Msg.Message = ViewSOLmsgID) then begin
  Handled := True;
  while PeekMessage(Msg1, 0, ViewSOLmsgID, ViewSOLmsgID, PM_REMOVE) do begin end;
  MessageBeep(0);
  if IsIconic(Application.Handle) then Application.Restore;
  MainForm.BringToFront;
  MainForm.SetFocus;
  if Clipboard.HasFormat(CF_TEXT) then begin
   newfilename := Clipboard.AsText;
   Clipboard.Clear;
  end;
  if (newfilename <> '') then begin
    if ValidSl4File(newfilename) then LaunchJob;
  end;
 end;
 end;

It may not be the best way but it works for me.

Mark Horridge

Mark Horridge email: mark.horri...@buseco.monash.edu.au

Other Threads