怎么检查一个进程是否存在?

xxxxxxxxx 2005-07-17 12:40:21
各位大哥,怎么检查一个进程是否存在呀。比如c:\ok.exe我想在进程里检查这个程序是否启动,如果没有那么就启动他。
...全文
381 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiao2004 2005-07-17
  • 打赏
  • 举报
回复
可以利用createtoolhelp32snapshot进行枚举,然后再比较,这是最简单的方法.
通用win9x 和 winnt
僵哥 2005-07-17
  • 打赏
  • 举报
回复
uses
PsAPI, TlHelp32;
// portions by Project Jedi www.delphi-jedi.org/
const
RsSystemIdleProcess = 'System Idle Process';
RsSystemProcess = 'System Process';

function IsWinXP: Boolean;
begin
Result := (Win32Platform = VER_PLATFORM_WIN32_NT) and
(Win32MajorVersion = 5) and (Win32MinorVersion = 1);
end;

function IsWin2k: Boolean;
begin
Result := (Win32MajorVersion >= 5) and
(Win32Platform = VER_PLATFORM_WIN32_NT);
end;

function IsWinNT4: Boolean;
begin
Result := Win32Platform = VER_PLATFORM_WIN32_NT;
Result := Result and (Win32MajorVersion = 4);
end;

function IsWin3X: Boolean;
begin
Result := Win32Platform = VER_PLATFORM_WIN32_NT;
Result := Result and (Win32MajorVersion = 3) and
((Win32MinorVersion = 1) or (Win32MinorVersion = 5) or
(Win32MinorVersion = 51));
end;

function InRunningProcesses(const FileName:String ; FullPath: Boolean): Boolean;

function ProcessFileName(PID: DWORD): string;
var
Handle: THandle;
begin
Result := '';
Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
if Handle <> 0 then
try
SetLength(Result, MAX_PATH);
if FullPath then
begin
if GetModuleFileNameEx(Handle, 0, PChar(Result), MAX_PATH) > 0 then
SetLength(Result, StrLen(PChar(Result)))
else
Result := '';
end
else
begin
if GetModuleBaseNameA(Handle, 0, PChar(Result), MAX_PATH) > 0 then
SetLength(Result, StrLen(PChar(Result)))
else
Result := '';
end;
finally
CloseHandle(Handle);
end;
end;

function InListTH: Boolean;
var
SnapProcHandle: THandle;
ProcEntry: TProcessEntry32;
NextProc: Boolean;
LocalFileName: string;
LocalResult:boolean;
begin
Result:=false;
SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
LocalResult := (SnapProcHandle <> INVALID_HANDLE_VALUE);
if LocalResult then
try
ProcEntry.dwSize := SizeOf(ProcEntry);
NextProc := Process32First(SnapProcHandle, ProcEntry);
while NextProc do
begin
if ProcEntry.th32ProcessID = 0 then
begin
// PID 0 is always the "System Idle Process" but this name cannot be
// retrieved from the system and has to be fabricated.
LocalFileName := RsSystemIdleProcess;
end
else
begin
if IsWin2k or IsWinXP then
begin
LocalFileName := ProcessFileName(ProcEntry.th32ProcessID);
if LocalFileName = '' then
LocalFileName := ProcEntry.szExeFile;
end
else
begin
LocalFileName := ProcEntry.szExeFile;
if not FullPath then
LocalFileName := ExtractFileName(LocalFileName);
end;
end;
if UpperCase(LocalFileName)=UpperCase(FileName) then
begin
Result:=true;
Exit;
end;
NextProc := Process32Next(SnapProcHandle, ProcEntry);
end;
finally
CloseHandle(SnapProcHandle);
end;
end;

function InListPS: Boolean;
var
PIDs: array [0..1024] of DWORD;
Needed: DWORD;
I: Integer;
LocalFileName: string;
EnumResult:Boolean;
begin
Result:=false;
EnumResult := EnumProcesses(@PIDs, SizeOf(PIDs), Needed);
if EnumResult then
begin
for I := 0 to (Needed div SizeOf(DWORD)) - 1 do
begin
case PIDs[I] of
0:
// PID 0 is always the "System Idle Process" but this name cannot be
// retrieved from the system and has to be fabricated.
LocalFileName := RsSystemIdleProcess;
2:
// On NT 4 PID 2 is the "System Process" but this name cannot be
// retrieved from the system and has to be fabricated.
if IsWinNT4 then
LocalFileName := RsSystemProcess
else
LocalFileName := ProcessFileName(PIDs[I]);
8:
// On Win2K PID 8 is the "System Process" but this name cannot be
// retrieved from the system and has to be fabricated.
if IsWin2k or IsWinXP then
LocalFileName := RsSystemProcess
else
LocalFileName := ProcessFileName(PIDs[I]);
else
LocalFileName := ProcessFileName(PIDs[I]);
end;
if UpperCase(LocalFileName) = UpperCase(FileName) then
begin
Result:=true;
Exit;
end;
end;
end;
end;
begin
if IsWin3X or IsWinNT4 then
Result := InListPS
else
Result := InListTH;
end;

procedure TForm1.Button1Click(Sender: TObject);

begin

if InRunningProcesses('c:\ok.exe',true) then
Caption:='True'
else
Caption:='false';
end;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧