본문 바로가기

Delphi/꿀팁

파일명으로 프로세스 종료하기

uses

  TlHelp32;


function TaskKill(sFileName: string): Boolean;

const

  PROCESS_TERMINATE = $0001;

var

  bLoop: Boolean;

  sTmpFileName: string;

  SnapshotHandle: THandle;

  ProcessEntry32: TProcessEntry32;

begin

  Result := False;


  SnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

  ProcessEntry32.dwSize := SizeOf(ProcessEntry32);


  bLoop := Process32First(SnapshotHandle, ProcessEntry32);


  while bLoop do

  begin

    sTmpFileName := UpperCase(ProcessEntry32.szExeFile);

    if (ExtractFileName(sTmpFileName) = UpperCase(sFileName)) or

      (sTmpFileName = UpperCase(sFileName)) then

    begin

      Result := TerminateProcess(

                   OpenProcess(PROCESS_TERMINATE,

                                      False,

                                      ProcessEntry32.th32ProcessID)0);

    end;


    bLoop := Process32Next(SnapshotHandle, ProcessEntry32);

  end;


  CloseHandle(SnapshotHandle);

end;


반응형