5,388
社区成员
发帖
与我相关
我的任务
分享
function TaskExist(ExeFileName: string): Boolean;
const
PROCESS_TERMINATE = $0001;
var
hMutex,hSnap: THandle;
icount: Integer;
g_dwProcessId: DWORD;
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result:= True;
icount:= 0; g_dwProcessId:= 0;
hMutex:=CreateMutex(nil,FALSE,PChar('Global\'+ExeFileName));
if(GetLastError()<>ERROR_ALREADY_EXISTS) then
begin
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 begin
g_dwProcessId:=FProcessEntry32.th32ProcessID;
inc(icount);
Break;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
if((g_dwProcessId<>0) and (icount > 1)) then
begin
Application.MessageBox(PChar(ExeFileName+'程序已经运行,请确认!'),'提示');
Result:= False;
end;
end else begin
Application.MessageBox(PChar(ExeFileName+'程序已经运行,请确认!'),'提示');
Result:= False;
end;
end;