I've seen tons of examples for identifying if app is already running, and close if it is. Well I found one function that does the job really easily, very little code, and works. All the rest either is a lot of funky code, or just doesn't work. Well Now I have this function in my app's unit, and when trying to use it, the function always returns True, because literally the app IS running (but this is the only instance that is running).
So the question is how do I make this function return an Integer representing how many times the process is running?
Source found at
And after my little modifications:
JD Solutions
So the question is how do I make this function return an Integer representing how many times the process is running?
Source found at
And after my little modifications:
Code:
program RMProTray;
uses
Forms,
Dialogs,
Windows,
TlHelp32,
SysUtils,
uRMPTray in 'uRMPTray.pas' {frmMain},
AlertThread in '..\Small\AlertThread.pas';
function ProcessExists(exeFileName: string): Boolean;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
Result := False;
while Integer(ContinueLoop) <> 0 do begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then Result := True;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
{$R *.res}
begin
if ProcessExists('RMProTray.exe') then begin
MessageDlg('App already running!', mtError, [mbOK], 0);
Application.Terminate;
end else begin
Application.Initialize;
Application.CreateForm(TfrmMain, frmMain);
Application.Run;
end;
end.
JD Solutions