function Killpro(ExeFileName: string): integer; //杀死进程的函数.
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result:= 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile))=UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile)=UpperCase(ExeFileName))) then
showmessage('此程序已经在系统中运行');
application.terminate;
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
=============================
我用上面的函数判断我现要运行的这个EXE有没有运行,如果已经在系统中运行,提示,并终止重新打开的程序。
可是为什么我用这个函数去判断有没有已经存在的这个程序时,不管理这个程序有没有存在系统中,都报是有,并终止呢?
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result:= 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile))=UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile)=UpperCase(ExeFileName))) then
showmessage('此程序已经在系统中运行');
application.terminate;
ContinueLoop := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
=============================
我用上面的函数判断我现要运行的这个EXE有没有运行,如果已经在系统中运行,提示,并终止重新打开的程序。
可是为什么我用这个函数去判断有没有已经存在的这个程序时,不管理这个程序有没有存在系统中,都报是有,并终止呢?
begin
main_handle:=winprocs.findwindow(nil,'程序主窗体名称');
if main_handle>0 then
begin
showmessage('程序已经打开,请检查!'); //target:find the active form ways
close;
exit;
end;
windows,dialogs,forms,
unit1 in 'unit1.pas'{form1};
{$R*.RES}
var
handle:Thandle;
begin
handle:=findwindow('tform1','form1');
if handle=0 then
begin
application.initialize;
application.createform(tform1,form1);
application.run;
end
else
messagedlg('重复运行',mtinformation,[mbok],0);
setforegroundwindow(handle);
end;
WND:Thandle;
begin
WND:=Findwindow('TForm_main','CDMA无线网卡应用管理器');
if WND=0 then begin
Application.Initialize;
Application.CreateForm(TForm_main, Form_main);
Application.Run;
end else begin
messagedlg('重复运行',mtinformation,[mbok],0);
setforegroundwindow(WND);
end;
=================================
结果每次运行都报重复运行了
e1:tevent;
begin
e1 := tevent.Create(nil,false,true,'hellomyproject');
if e1.WaitFor(500)=wrTimeout then halt;
......
随便上网上搜索CreateMutex
if iCode = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, PChar('系统已运行,请不要同时运行多个系统!!!'), '系统信息', 0);
end;
互斥\
关键区\
等等;
可以在delphi6开发人员指南中知道
不是最好的解决方法
IF GetLastError=ERROR_ALREADY_EXISTS THEN
begin
showmessage('已经有一个程序在运行!!');
end else
begin
application.init......
...
application.run;
end;
1。新建一个单位。PAS
写上
Interface
..
Function HaveApp(AppID:string):Boolean;implementationUses windows;var
HMutex:THandle;function HaveApp(AppID:string):Boolean;
Begin
Result := False;
HMutex := OpenMutex(MUTEX_ALL_ACCESS, False, PChar(AppID));
if HMutex = 0 then
HMutex := CreateMutex(nil, False, PChar(AppID))
Else
Result := True;
End;initializationfinalization
if HMutex <>0 then CloseHandle(HMutex);
End.在工程代码里面加上
Uses {上面的单元名称}...
Begin
if not HaveApp('{51991952-44c3-4b25-A936-9FDE05B53AA}') then
Begin
Application.Initialize;
....
Application.Run;
End;
End.
其中使用GUID号的全局唯一性判断程序是否运行。
其中GUID可以按Ctrl + Shift + G自动生成,无需照抄--------------------------------------------------------------------------注:引自 罗小平(CSDN网名:lxpbuaa(桂枝香在故国晚秋)) 的出书《Delphi 精要》我的入门老师 ∶)在这里感谢他。。