I know that I did it before, but I canโt remember how to do it.
I have a program that I installed to run singleton using a mutex on an executable name. GlobalSU unit
interface function IsAppRunning: Boolean; implementation uses Windows, SysUtils, Forms; function IsAppRunning: Boolean; var rtn : Cardinal; begin result := False; CreateMutex(nil, False, PWideChar(ExtractFileName(Application.ExeName))); rtn := GetLastError; if rtn = ERROR_ALREADY_EXISTS then result := True; end;
The program accepts certain command line parameters that determine what data should act. I do not want more than one instance of a program working with the same command line arguments. But I want to be able to run the second instance with different arguments.
I did it about a year ago, but I canโt remember how to do it. I change the name with the command line options in DPR and then test it with the mutex.
I tried renaming Application.ExeName, but it is read-only, so I had to change something.
Below is the code that will not compile, but added to clarify what I want to do. btw - "##" is always the first two characters of the 3rd parameter, but I check it with RegEx.
program EPRmailer; uses Vcl.Forms, uMainMailer in 'uMainMailer.pas' {frmMainMailer}, configXML in 'configXML.pas', GlobalSU in 'GlobalSU.pas', CVUtils in 'CVUtils.pas', QMConst in 'QMConst.pas', ServerAttachmentDMu in 'ServerAttachmentDMu.pas'; {$R *.res} var i : integer; begin for i := 0 to ParamCount do if TestParam('##', ParamStr(i)) then Application.ExeName := Application.ExeName + '-' + ParamStr(i); if IsAppRunning then exit; Application.Initialize; ReportMemoryLeaksOnShutdown := DebugHook <> 0; Application.MainFormOnTaskbar := false; Application.CreateForm(TfrmMainMailer, frmMainMailer); frmMainMailer.RunEPR; end.