whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

34 Visitors Online


 
...pass command line parameters from a started programm to an already running one?
Autor: Loïs Bégué
Homepage: http://www.arpoon.de
[ Print tip ]  

Tip Rating (14):  
     


{+++++++++++++++++++++ English +++++++++++++++++++++++++++++++++++++++++++++

You can control the start of your program and:
 1) disallow a second start
 2) call the already running application and make it active
 3) send the "new" command line parameters to it

++++++++++++++++++++++ Deutsch +++++++++++++++++++++++++++++++++++++++++++++

Sie kontrollieren den Programmstart und:
 1) verhindern, daß eine zweite Instanz gestartet wird
 2) aktivieren die erste Instanz
 3) übergeben die neuen Kommandozeilenparameter an dieser Instanz

++++++++++++++++++++++ Français ++++++++++++++++++++++++++++++++++++++++++++

Vpous controllez le démarage du programme et:
 1) empêchez une deuxième instance
 2) activez la première instance
 3) passez les paramêtres au démarrage de la 2ème vers la 1ère instance

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}


{####################### MAIN PROG FILE #####################################}

Programm Number_5;
//...
const
  
cMutexID = 'Number_5_is_alive';
var
  
Mutex: THandle;
  HAPPLICATION: HWND;
  HMAINFORM: HWND;
  StartParams: string;
  AnzahlParams: Integer;
  AtomSend: Integer;
begin
     
Mutex := CreateMutex(nil, True, cMutexID);
     // +++ identify an already running app +++
     
if (Mutex <> 0) and (GetLastError = 0) then 
     begin
        
// +++ none was found, so proceed as usual +++
        
Application.Initialize;
        Application.Title := 'Number 5 is alive';
        Application.CreateForm(TMF_Number5, MF);
        Application.Run;
        if (Mutex <> 0) then CloseHandle(Mutex);
     end
     else 
     begin
        
// +++ look for the running app windows handle +++
        
HAPPLICATION := 0;
        HMAINFORM := 0;
        Application.Initialize;
        repeat
              
HAPPLICATION := FindWindowEx(0,HAPPLICATION, 'TApplication',
                              PChar('Number 5 is alive'));
        until HAPPLICATION <> Application.Handle;
        // +++ found it, so activate the running app and  proceed with the parameters +++
        
if HAPPLICATION <> 0 then 
        begin
           
Windows.ShowWindow(HAPPLICATION, SW_Normal);
           Windows.SetForegroundWindow(HAPPLICATION);
           // +++ look for the Form handle, wich will be used as a target of the message +++
           
HMAINFORM := FindWindowEx(0,0,'TMF_Number5', nil);
           if (HMAINFORM <> 0) then 
           begin
              
StartParams := '';
              // +++ code your parameters +++
              
for AnzahlParams := 1 to ParamCount do
                  
StartParams := StartParams + ' ' + ParamStr(AnzahlParams);
              try
                 
// +++ Send the parameter info +++
                 
AtomSend := GlobalAddAtom(PChar(StartParams));
                 SendMessage(HMAINFORM, WM_ACTIVATENumber5, Length(StartParams), AtomSend);
              finally
                 
GlobalDeleteAtom(AtomSend);
              end;
           end;
        end;
        // all is done: parameters had been sent so we can quit safelly
        
Halt;
     end;
end.

{####################### FORM WITH MESSAGE HANDLING ROUTINE #################}

unit MFU;

interface

uses
  
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    ...;

const
  
WM_ACTIVATENumber5 = WM_USER + 101;

type
  
TMF_Number5 = class(TForm)
    procedure FormShow(Sender: TObject);
    procedure WMACTIVATENumber5(var Msg: TMessage); message WM_ACTIVATENumber5;
  private
    
{ Private-Deklarationen }
  
public
    
{ Public-Deklarationen }
  
end;

var
  
MF: TMF_Number5;

implementation

{$R *.dfm}

procedure TMF_Number5.FormShow(Sender: TObject);
begin
  
// +++ the app was normaly started: parameters are maybe pending in the command line buffer +++
  
if ParamCount > 0 then 
  begin
    
// +++ Handle with this parameters.... +++
  
end;
end;

procedure TMF_Number5.WMACTIVATENumber5(var Msg: TMessage);
var
  
Buffer: PChar;
  S: string;
begin
  
// +++ the app was "reactivated": the command line cannot be used thru "ParamStr(..)" yet +++
  
try
    
// +++ get the length of the "command line message" +++
    
Buffer := StrAlloc(Msg.wParam + 1);
    // +++ hold its value +++
    
GlobalGetAtomName(Msg.lParam, Buffer, Msg.wParam + 1);
    S := StrPas(Buffer);

    // +++ Analyse S and handle with parameters at will.... +++

  
finally
    
StrDispose(Buffer);
  end;
end;

end.

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners