Discussion:
Run an application once...
(too old to reply)
Gurur
2007-06-27 20:25:33 UTC
Permalink
I have an application .
I want that it should run only once.
How is it possible?
Uwe Volland
2007-06-27 20:57:31 UTC
Permalink
Post by Gurur
I have an application .
I want that it should run only once.
How is it possible?
uses
Windows, Messages, SysUtils, Variants,
.........
ComCtrls, Tlhelp32,

.....


public
{ Public-Deklarationen }

function isProgrunning(programm:string):boolean;

.....


function TMainform.isProgrunning(programm:string):boolean;
var Snap : THandle;
ProcessE : TProcessEntry32;
i:integer;s1,s2 : string;
webstartcont : byte;
processes : tstrings;
begin
result:=false;
processes:=TStringList.Create;
Snap:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessE.dwSize:=SizeOf(ProcessE);
if Process32First(Snap,ProcessE) then
begin
processes.Add(string(ProcessE.szExeFile));
while Process32Next(Snap,ProcessE) do
processes.Add(string(ProcessE.szExeFile));
end;
CloseHandle(Snap);
i:=0;
webstartcont:=0;
repeat
if (pos(lowercase(processes[i]),lowercase(programm))<>0) then
begin
inc(webstartcont);
if webstartcont>1 then
begin
i:=processes.Count;
result:=true;
end;
end;
inc(i);
until (i>=processes.Count);
processes.Free;
end;

..........



procedure TMainform.FormShow(Sender: TObject);
begin
if isProgrunning(<YourExe,e.g. myprog.exe>) then
begin
showmessage(<...>);
close;
end else ....
end;
Vertuas
2007-07-01 18:01:16 UTC
Permalink
An easier way would be to create a Mutex

If second program cannot create the mutex because it is in use, it quits.
Post by Uwe Volland
Post by Gurur
I have an application .
I want that it should run only once.
How is it possible?
uses
Windows, Messages, SysUtils, Variants,
.........
ComCtrls, Tlhelp32,
.....
public
{ Public-Deklarationen }
function isProgrunning(programm:string):boolean;
.....
function TMainform.isProgrunning(programm:string):boolean;
var Snap : THandle;
ProcessE : TProcessEntry32;
i:integer;s1,s2 : string;
webstartcont : byte;
processes : tstrings;
begin
result:=false;
processes:=TStringList.Create;
Snap:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessE.dwSize:=SizeOf(ProcessE);
if Process32First(Snap,ProcessE) then
begin
processes.Add(string(ProcessE.szExeFile));
while Process32Next(Snap,ProcessE) do
processes.Add(string(ProcessE.szExeFile));
end;
CloseHandle(Snap);
i:=0;
webstartcont:=0;
repeat
if (pos(lowercase(processes[i]),lowercase(programm))<>0) then
begin
inc(webstartcont);
if webstartcont>1 then
begin
i:=processes.Count;
result:=true;
end;
end;
inc(i);
until (i>=processes.Count);
processes.Free;
end;
..........
procedure TMainform.FormShow(Sender: TObject);
begin
if isProgrunning(<YourExe,e.g. myprog.exe>) then
begin
showmessage(<...>);
close;
end else ....
end;
Loading...