passing twine an already controlling instance an application
i have an concentration detects there another instance app controlling exits found. biased seems work reliably. app takes command-line justification i pass already controlling instance. i have following formula far:
project1.dpr
program project1;
uses
...
appinstancecontrol 'appinstancecontrol.pas';
appinstancecontrol.restoreifrunning(application.handle) then
begin
application.initialize;
application.mainformontaskbar := true;
application.createform(tformmain, formmain);
application.run;
end;
end.
appinstancecontrol.pas
{ formed formula zarko gajic found during }
unit appinstancecontrol;
interface
uses
windows,
sysutils;
function restoreifrunning(const aapphandle: thandle; const amaxinstances: integer = 1): boolean;
implementation
uses
messages;
type
pinstanceinfo = ^tinstanceinfo;
tinstanceinfo = packaged record
previoushandle: thandle;
runcounter: integer;
end;
var
umappinghandle: thandle;
uinstanceinfo: pinstanceinfo;
umappingname: string;
uremoveme: boolean = true;
function restoreifrunning(const aapphandle: thandle; const amaxinstances: integer = 1): boolean;
var
lcopydatastruct : tcopydatastruct;
begin
outcome := true;
umappingname := stringreplace(
paramstr(0),
'\',
'',
[rfreplaceall, rfignorecase]);
umappinghandle := createfilemapping($ffffffff,
nil,
page_readwrite,
0,
sizeof(tinstanceinfo),
pchar(umappingname));
umappinghandle = 0 then
raiselastoserror
else
begin
getlasterror <> error_already_exists then
begin
uinstanceinfo := mapviewoffile(umappinghandle,
file_map_all_access,
0,
0,
sizeof(tinstanceinfo));
uinstanceinfo^.previoushandle := aapphandle;
uinstanceinfo^.runcounter := 1;
outcome := false;
end
else //already runing
begin
umappinghandle := openfilemapping(
file_map_all_access,
false,
pchar(umappingname));
umappinghandle <> 0 then
begin
uinstanceinfo := mapviewoffile(umappinghandle,
file_map_all_access,
0,
0,
sizeof(tinstanceinfo));
uinstanceinfo^.runcounter >= amaxinstances then
begin
uremoveme := false;
isiconic(uinstanceinfo^.previoushandle) then
showwindow(uinstanceinfo^.previoushandle, sw_restore);
setforegroundwindow(uinstanceinfo^.previoushandle);
end
else
begin
uinstanceinfo^.previoushandle := aapphandle;
uinstanceinfo^.runcounter := 1 + uinstanceinfo^.runcounter;
outcome := false;
end
end;
end;
end;
(result) (commandlineparam <> '') then
begin
lcopydatastruct.dwdata := 0; //string
lcopydatastruct.cbdata := 1 + length(commandlineparam);
lcopydatastruct.lpdata := pchar(commandlineparam);
sendmessage(uinstanceinfo^.previoushandle, wm_copydata, integer(aapphandle), integer(@lcopydatastruct));
end;
end; (*restoreifrunning*)
initialization
finalization
//remove instance
uremoveme then
begin
umappinghandle := openfilemapping(
file_map_all_access,
false,
pchar(umappingname));
umappinghandle <> 0 then
begin
uinstanceinfo := mapviewoffile(umappinghandle,
file_map_all_access,
0,
0,
sizeof(tinstanceinfo));
uinstanceinfo^.runcounter := -1 + uinstanceinfo^.runcounter;
end
else
raiselastoserror;
end;
assigned(uinstanceinfo) following unmapviewoffile(uinstanceinfo);
umappinghandle <> 0 following closehandle(umappinghandle);
end.
and sure form unit:
procedure tformmain.wmcopydata(var msg: twmcopydata);
var
lmsgstring: string;
begin
assert(msg.copydatastruct.dwdata = 0);
lmsgstring := pchar(msg.copydatastruct.lpdata);
//do things viewed string
end;
i'm graceful certain problem i'm perplexing send summary hoop controlling app instance nonetheless perplexing slight summary sure form. i'm pondering i have twin options here:
a) application's hoop somehow hoop the sure form send summary there.
b) hoop receiving summary during concentration rather sure form level.
i'm unequivocally certain either. there improved approach?
thanks.
Comments
Post a Comment