Hai all,

In delphi, how to make the program only one exe only on executing.
example :
I have a project tools
after execute it appears tools.exe

how to make the people can not execute tools.exe in the second time??
Only one exe can run.

you can make this through:
1) registry - in runtime an reg key is set to an value, when the execution is finished set the key to another value
2) files - principle is the same as above
3) processes - search if another process with this name exist

best regards,

Hai all
How to set registry?? in order the execute one exe only in delphi application.

How to check the process in delphi??

Thanks,


dodol

Hai all,

In delphi, how to make the program only one exe only on executing.
example :
I have a project tools
after execute it appears tools.exe

how to make the people can not execute tools.exe in the second time??
Only one exe can run.

Hi Radu 84, I have check in the website that you give. By my Applicatin still can run more than 1 time. Do you have a simple code to run only one.exe?
Any one have delphi code to run only one exe?

GBU

Dodol

many ways to do this.
when your program is runin for first time u can write string in registry.( example: run value =1 ).
when close and run program again u can reead this string. if this string exist u can terminate your application.

uses registry

procedure TForm1.FormActivate(Sender: TObject);
Var
Reg : TRegistry;

begin
Reg := TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
if Reg.KeyExists('\Software\RunOnceExe') then
// RunOnceExe is an optional string
begin
reg.CloseKey;
reg.Free;
showmessage('you can not use this software');
application.Terminate
end
else
begin
Reg.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('\Software', true)then
reg.CreateKey('RunOnceExe');
Reg.CloseKey;
reg.Free;
end;
end;

Hi dodl

here is a very simple and working code that i always use(except of course in programs that i want to run more then once).

you just need to place it in the DPR file of your project, and it's 100% working.

Notice something important:
The "UniqueAppName" is what you choose it to be, and it's not the Application.Title .
it's something the Windows need to identify as something unique.

i found this code somewhere on the net and i always use it since.
(sorry for the guy who wrote it and i can't remember who...)

======================
uses
Forms,
Windows,
SysUtils,
ShellAPI,
Main in 'Main.pas' {Form1};


VAR MutexHandle : THandle;
hwind       : HWND;


begin
//Run Only one instance
MutexHandle := CreateMutex(nil, TRUE, 'UniqueAppName'); // should be a unique string
IF MutexHandle <> 0 then
begin
IF GetLastError = ERROR_ALREADY_EXISTS then
begin
CloseHandle(MutexHandle);
hwind:=0;
repeat
hwind:=Windows.FindWindowEx(0,hwind,'TApplication','UniqueAppName');
until (hwind<>Application.Handle);
IF (hwind<>0) then
begin
Windows.ShowWindow(hwind,SW_SHOWNORMAL);
Windows.SetForegroundWindow(hwind);
end;
Halt;
end
end;


Application.Initialize;
Application.Title := 'App title here';
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
program blahblahbla;
uses
Forms,
Windows,
SFV215_Main in 'SFV215_Main.pas' {CartForm1},
DMunit in 'DMunit.pas' {DM: TDataModule},
SFV215_Identify_User_and_Prep in 'SFV215_Identify_User_and_Prep.pas',
logger in '..\SharedCode\logger.pas';


{$R *.res}


var
PreviousHandle : THandle;
begin
PreviousHandle := FindWindow('TCartForm1','Cart-O-Rama'); //allow only 1
//parameters are main form type and main form caption
if PreviousHandle <> 0 then                               //instance to run
SetForegroundWindow(PreviousHandle)                     //at a time
else begin
Application.Initialize;
Application.Title := 'SFV215_PL2_Cart_O_Rama';
Application.CreateForm(TDM, DM);
Application.CreateForm(TCartForm1, CartForm1);
Application.Run;
end;
//to run in IDE the if, SetFore..., else begin and end; statements must be
//commented out
end.

Ennis has the answer, the best solution is a mutex, the runonce registry only means it will be run once on startup, not it can only have 1 copy running at any time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.