Single Instance of app while handling params?

  

I want my app to have only one instance and handle params of newly fired one. I want to use params and do some processing based on those params.

I’ve tried David Heffernan’s answer in related question but I didn’t find anything related to TCreateParams in FMX and in the Delphi Rio documentation.

Can’t comment there as I don’t have reputation though.

program StartupProject;

uses
SysUtils,
Messages,
Windows,
Forms,
Form3 in ‘firetest.pas’ {MainForm};

{$R *.res}

procedure Main;
var
i: Integer;
Arg: string;
Window: HWND;
CopyDataStruct: TCopyDataStruct;
begin
Window := FindWindow(SWindowClassName, nil);
if Window=0 then begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end else begin
FillChar(CopyDataStruct, Sizeof(CopyDataStruct), 0);
for i := 1 to ParamCount do begin
Arg := ParamStr(i);
CopyDataStruct.cbData := (Length(Arg)+1)*SizeOf(Char);
CopyDataStruct.lpData := PChar(Arg);
SendMessage(Window, WM_COPYDATA, 0, NativeInt(@CopyDataStruct));
end;
SetForegroundWindow(Window);
end;
end;

begin
Main;
end.

 

unit fireTest;

interface

uses

type
TForm3 = class(TForm)
procedure FormCreate(Sender: TObject);
public
procedure ProcessArgument(const Arg: string);
{ Public declarations }
protected
procedure WMCopyData(var Message: TWMCopyData); message WM_COPYDATA;

var
Form3: TForm3;

implementation

{$R *.dfm}

{ TMainForm }

procedure TForm3.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 1 to ParamCount do begin
ProcessArgument(ParamStr(i));
end;
end;

procedure TForm3.ProcessArgument(const Arg: string);
begin
Memo1.Lines.Add(arg);
end;

procedure TForm3.WMCopyData(var Message: TWMCopyData);
var
Arg: string;
begin
SetString(Arg, PChar(Message.CopyDataStruct.lpData), (Message.CopyDataStruct.cbData div SizeOf(Char))-1);
ProcessArgument(Arg);
//Application.Restore;
//Application.BringToFront;
end;

end.

Comments are closed.