One thing I've done with some of my programs is put a snippet of code in the beginning of the project source file in order to detect whether a copy is running and stop it. More or less, such a thing has to go in the main module of the project. So if we start with:
Then the code I use can be:
Obviously, the question becomes modularizing this code, as this still seems a substantial amount of required code to support the function. Is there a way around this I'm not seeing, or would it require modifying the "new Project" type in order to put the code out like this? Or is it more trouble than it's worth?
Code:
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
Then the code I use can be:
Code:
function CheckExistence(var SemHandle: THandle; ID: String): Boolean;
begin
Result := true;
SemHandle := CreateSemaphore(nil,0,1,PChar(ID));
if ((SemHandle <> 0) and (GetLastError = ERROR_ALREADY_EXISTS)) then
begin
CloseHandle(SemHandle);
Result := false;
ExitProcess(0);
end;
end;
var FSemaPhore: Thandle;
if CheckExistence(FSemaPhore, 'Form1Test') then
try
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
finally
CloseHandle(FSemaPhore);
end;
Obviously, the question becomes modularizing this code, as this still seems a substantial amount of required code to support the function. Is there a way around this I'm not seeing, or would it require modifying the "new Project" type in order to put the code out like this? Or is it more trouble than it's worth?