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
241 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
[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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month8 days, left to enroll

617 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