Solved

How do I pass commandline parameter from a 2nd instance just starting to the 1st instance allready running

Posted on 1998-09-24
6
232 Views
Last Modified: 2010-04-06
I have an app already running and need to pass the parameter given on the commandline of a 2nd instance of an app to the first running instance. The 2nd instance wouldnt start but the first instance would use the parameter instead.
0
Comment
Question by:jmail
6 Comments
 
LVL 10

Expert Comment

by:viktornet
ID: 1340829
Why not use a DLL for that?
0
 

Expert Comment

by:huizhang
ID: 1340830
Hi jmail,

This is an interesting question. It is possible I am sure, but
need a bit work to do.

1. The App must have a message handler to check the message que.
2. When the app start, it shoud ckeck if or not there is already
   an instance there, if so send a message to the existing
   instance, and make sure the message is recieved then close
   itself.
3. The first instance will process the message.

If I were you, I will make two app instead of one. The first can
be a service app. The second is an client or a controler, just
for send data.

Good luck!
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1340831
jmail,

look at "http://www.experts-exchange.com/Q.10081302".

There you'll find source code for what huizhang suggested (steps 1-3).

Regards, Madshi.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Accepted Solution

by:
Jacco earned 100 total points
ID: 1340832
I have answered a question like that before:

(http://www.experts-exchange.com/topics/comp/lang/delphi/Q.10078852)

Because it is my own answer I'll post it again:

*** start of answer ***

You need to prevent multiple applications to start. I use the PrevMultiple unit for that.

Change the UniqueAppStr to the desired value, any string will do.
When the app is called for the second time a globalatom is added with the filename. The first instantiated finds the global back by its number, and reads it.

I tried this with the standard Delphi 2 demo of TextEdit and added the following things:

in  MDIFrame.Pas:

procedure TFrameForm.Open(const s : String);
begin
  with TEditForm.Create(Self) do
    Open(s);
end;

in TextEdit.Dpr

begin
  Application.CreateForm(TFrameForm, FrameForm);
  if ParamCount<>0 then
    FrameForm.Open(ParamStr(1));
  Application.Run;
end.

This is the PrevMultiple unit. Pay attention that it uses the MDIFrame unit!! And MDIFrame has an extra method Open!!!

*** start of code ***
unit UPrevMultiple;

interface

uses
  Forms, Windows, Dialogs, SysUtils;

const
  MI_NO_ERROR          = 0;
  MI_FAIL_SUBCLASS     = 1;
  MI_FAIL_CREATE_MUTEX = 2;

function GetMIError : Integer;

implementation

uses
  MDIFrame;

const
  UniqueAppStr : PChar = 'My Text Editor';

var
  MessageId : Integer;
  WProc     : TFNWndProc = nil;
  MutHandle : THandle = 0;
  MIError   : Integer = 0;

function GetMIError: Integer;
begin
  Result := MIError;
end;

function NewWndProc(Handle : HWND; Msg : Integer; wParam, lParam : LongInt) : LongInt; StdCall;
var
  s : PChar;
begin
  // Initialize result
  Result := 0;
  // if this is the registered message ...
  if Msg = MessageID then begin
    // if main form is minimized, normalize it
    if Application.MainForm.WindowState = wsMinimized then
      Application.MainForm.WindowState := wsNormal;
      // set the focus to the application
      SetForegroundWindow(Application.MainForm.Handle);
      // load the text!
      GetMem(s,100);
      try
        GlobalGetAtomName(Atom(lParam),s,100);
        GlobalDeleteAtom(Atom(lParam));
        TFrameForm(Application.MainForm).Open(s);
      finally
        FreeMem(s,100);
      end;
  end else
    // Otherwise, pass message on to old window proc
    Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
end;

procedure SubClassApplication;
begin
  {we subclass application window so that
  application.onmessage remains available for user}
  WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
                        LongInt(@NewWndProc)));
  {set appropriate error flag}
  if WProc = nil then
    MIError := MIError or MI_FAIL_SUBCLASS;
end;

procedure DoFirstInstance;
begin
  SubClassApplication;
  MutHandle := CreateMutex(nil, False, UniqueAppStr);
  if MutHandle = 0 then
    MIError := MIError or MI_FAIL_CREATE_MUTEX;
end;

var
  s: PChar;

procedure BroadCastFocusMessage;
{ this is called when there is already an instance running}
var
  BSMRecipients : DWORD;
  AAtom : Atom;
begin
  { don't flash main form }
  Application.ShowMainForm := false;
  { post message and inform other instace to focus itself }
  BSMRecipients := BSM_APPLICATIONS;
  s:=PChar(ParamStr(1));
  AAtom:=GlobalAddAtom(s);
  BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
    @BSMRecipients, MessageID, 0, LongInt(AAtom));
  Application.Terminate;
end;

procedure InitInstance;
begin
  MutHandle :=OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
  if MutHandle = 0 then
    { mutex object has not yet been created, meaning that no previous
      instance has been created }
    DoFirstInstance
  else
    BroadCastFocusMessage;
end;

initialization
  MessageID := RegisterWindowMessage(UniqueAppStr);
  InitInstance;
finalization
  if WProc <> nil then
    // restore the old window procedure
    SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
end.

*** end of code ***

P.S. A far better apraoch would be using DDE in stead of globalatoms. But I have litle experience with that.

B.T.W. Your link in the explorer should be something like:

".../textedit.exe" "%1"

This because the of long file that include spaces.

Let me know if it works! I had fun trying this out. It works!

Regards Jacco

*** end of answer ***

*** start of comment ***

The unit does everything itself.

The initialisation/finalization part of the unit is responsible for it.

The initialisation part is executed when the application starts

It checks if there is a previous instance running. If not it registers a special message. If so it broadcasts this special message to the previous instance. If they is is add a global atom and broadcast a message that the atom is there.

The finalisation part is executed when the application ends

This unregisters the special message.

Try it!

Regards Jacco

*** end of comment ***


0
 
LVL 10

Expert Comment

by:Jacco
ID: 1340833
The solution I provided here uses a global atom for the transfer of a string. A special message signal the previous instandce using a message in which the identifier of the global atom is placed. The first instance get this atom uses it and deletes it.

This sample workes for the text editor that comes with delphi. The command line parameter is a string. I have not experimented with spaces etc.

This should get you started !

If you'd rather view my old answer which is only 6 pts, that's ok with me. But I can explain or help you with further working out of the problem as well. (At this moment I am asking myself why I didn't post the previous as a comment, am I getting greedy?)

Regards Jacco
0
 

Author Comment

by:jmail
ID: 1340834
Thanks for the help, and sorry about getting back with the thanks sooner.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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