zoukankan      html  css  js  c++  java
  • 一个比较方便的关闭进程函数

    原创哦...如果有更好的功能或者BUG修订请通知我, 谢谢

    uses
        TlHelp32, PsAPI;
    
    
    {AFile: 要结束的进程
    AEffectFirst: 是否只结束第一个找到的进程
    
    可以只输入EXE名称, 或者全路径+文件名, 或者只是某个文件路径
    如果输入的只是某个路径, 则关闭属于这个路径下的所有进程}
    function KillProcess(AFile: string; AEffectFirst: Boolean = True): WORD;
    const
      PROCESS_TERMINATE = $0001;
    var
      nContinueLoop: BOOL;
      nSnapShotHandle: THandle;
      nProcessEntry32: TProcessEntry32;
      nSelfID, nPrHandle: Cardinal;
      nMBF: array [0..MAX_PATH] of Char;
      nCMode: Byte; {0:检测全路径文件, 1:仅检测文件 2:仅检测路径}
      nCPath, nCFile, nStr, nFile, nPath: string;
      nDA: DWORD;
    begin
      Result := 0;
      AFile := UpperCase(AFile);
      nCPath := ExtractFilePath(AFile);
      nCFile := ExtractFileName(AFile);
    
      if (nPath <> '') and (nCFile <> '') then
        nCMode := 0
      else if nCFile <> '' then
        nCMode := 1
      else if nCPath <> '' then
        nCMode := 2
      else
        Exit;
    
      nSelfID := GetCurrentProcessID;
      nSnapShotHandle := CreateToolhelp32SnapShot(TH32CS_SNAPPROCESS, 0);
      try
        nProcessEntry32.dwSize := SizeOf(nProcessEntry32);
        nContinueLoop := Process32First(nSnapShotHandle, nProcessEntry32);
        while nContinueLoop do
        begin
          if nProcessEntry32.th32ProcessID <> nSelfID then
          begin
            nStr := UpperCase(nProcessEntry32.szExeFile);
            nFile := ExtractFileName(nStr);
    
            {想运行到XP上需要指定这个值, 因为在高版本Delphi上这个值最后会变成$FFFF
            导致编译的程序在XP系统上获取句柄失败}
            nDA := STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or $FFF;
            nPrHandle := OpenProcess(nDA, False, nProcessEntry32.th32ProcessID);
            try
              if nCMode <> 1 then
              begin
                nPath := ExtractFilePath(nStr);
                if nPath = '' then
                begin
                  {此处据说在64位系统上会执行失败, win10-X64未发现
                  替代方案是, 如果是vista以上的版本使用QueryFullProcessImageName替代
                  KERNEL32.dll QueryFullProcessImageName}
                  if GetModuleFileNameEx(nPrHandle, 0, @nMBF[0], SizeOf(nMBF)) > 0 then
                    nPath := UpperCase(ExtractFilePath(nMBF));
                end;
              end;
    
              if ((nCMode = 0) and (nPath = nCPath) and (nFile = nCFile)) {全路径文件名相同}
                or ((nCMode = 1) and (nFile = nCFile)) {文件名相同}
                or ((nCMode = 2) and (Copy(nPath, 1, Length(nCPath)) = nCPath)) {路径包含} then
              begin
                TerminateProcess(nPrHandle, 0); {强制关闭进程}
                if AEffectFirst then
                  Break;
              end;
            finally
              CloseHandle(nPrHandle);
            end;
          end;
          nContinueLoop := Process32Next(nSnapShotHandle, nProcessEntry32);
        end;
      finally
        CloseHandle(nSnapShotHandle);
      end;
    end;
  • 相关阅读:
    LA 3026 Period
    Touch
    Uva 11762 Race to 1
    清北学堂模拟赛d1t2 火柴棒 (stick)
    清北学堂模拟赛d1t1 位运算1(bit)
    常州模拟赛d8t2 绘画
    常州模拟赛d8t1 友好数对
    常州模拟赛d5t3 appoint
    常州模拟赛d7t1 亲戚
    常州模拟赛d7t3 水管
  • 原文地址:https://www.cnblogs.com/lzl_17948876/p/3712046.html
Copyright © 2011-2022 走看看