我们如何确定程序已经在delphi中运行

时间:2017-02-03 02:37:59

标签: delphi

我想从我的delphi应用程序运行程序(exe)。我可以做到,但我想在运行之前检查程序是否已经运行。有没有办法做到这一点..提前谢谢。

1 个答案:

答案 0 :(得分:3)

以下函数检查进程是否正在运行。添加" TlHelp32"使用条款。

function ProcessRunning (sExeName: String) : Boolean;
{ -> sExeName : Name of the EXE without path. Does not have to be the full EXE name. }

var
    hSnapShot : THandle;
    ProcessEntry32 : TProcessEntry32;

begin
    Result := false;

    hSnapShot := CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
    Win32Check (hSnapShot <> INVALID_HANDLE_VALUE);

    sExeName := LowerCase (sExeName);

    FillChar (ProcessEntry32, SizeOf (TProcessEntry32), #0);
    ProcessEntry32.dwSize := SizeOf (TProcessEntry32);

    if (Process32First (hSnapShot, ProcessEntry32)) then
        repeat
            if (Pos (sExeName,
                     LowerCase (ProcessEntry32.szExeFile)) = 1) then
            begin
                Result := true;
                Break;
            end; { if }
        until (Process32Next (hSnapShot, ProcessEntry32) = false);

    CloseHandle (hSnapShot);
end; { ProcessRunning }