zoukankan      html  css  js  c++  java
  • 通过进程名或模块名来杀进程

    unit KillProcess;

    interface

    uses
    Windows, SysUtils, Forms, Controls, StdCtrls, Classes, TlHelp32;

    function KillByProcessNameOrModuleName(ProcessNameOrModuleName: string):Boolean;
    function KillByProcessName(ProcessName: string):Boolean;

    implementation

    function KillByProcessNameOrModuleName(ProcessNameOrModuleName: string):Boolean;
    function ModuleEnum(processid:Dword):boolean;
    var
    ModuleList :Thandle;
    pm :TMODULEENTRY32;
    foundModule:boolean;
    begin
    Result:
    =False;
    ModuleList :
    = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,processID);
    pm.dwSize:
    =sizeof(TMODULEENTRY32);
    foundModule:
    =module32first(ModuleList,pm);
    while integer(foundModule)<>0 do begin
    if pos(UpperCase(pm.szModule),UpperCase(ProcessNameOrModuleName))>0 then begin
    Result:
    =True;
    exit;
    end;
    foundModule:
    =Module32Next(ModuleList,pm);
    end;
    closehandle(ModuleList);
    end;
    var
    Found: boolean;
    HProcess,HSnapshot: THandle;
    appe: TProcessEntry32;
    begin
    Result :
    = False;
    HSnapshot :
    = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    appe.dwSize :
    = SizeOf(appe);
    Found :
    = Process32First(HSnapshot, appe);
    while Integer(Found) <> 0do begin
    if ModuleEnum(appe.th32ProcessID) then begin
    try
    HProcess :
    = OpenProcess(PROCESS_ALL_ACCESS, True, appe.th32ProcessID);
    TerminateProcess(HProcess,
    0);
    Result :
    = True;
    except
    Result :
    = False;
    end;
    end;
    Found :
    = Process32Next(HSnapshot, appe);
    end;
    CloseHandle(HSnapshot);
    end;

    function KillByProcessName(ProcessName: string):Boolean;
    var
    s:
    string;
    Found: boolean;
    HProcess,HSnapshot: THandle;
    appe: TProcessEntry32;
    begin
    Result :
    = False;
    HSnapshot :
    = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    appe.dwSize :
    = SizeOf(appe);
    Found :
    = Process32First(HSnapshot, appe);
    while Integer(Found) <> 0do begin
    s :
    = appe.szExeFile;
    if UpperCase(s) = UpperCase(ProcessName) then begin
    try
    HProcess :
    = OpenProcess(PROCESS_ALL_ACCESS, True, appe.th32ProcessID);
    TerminateProcess(HProcess,
    0);
    Result :
    = True;
    except
    Result :
    = False;
    end;
    end;
    Found :
    = Process32Next(HSnapshot, appe);
    end;
    CloseHandle(HSnapshot);
    end;

    end.
  • 相关阅读:
    梦断代码第8章总结
    <<梦断代码>>读后感
    站立会议第四篇
    购买一批书的最低价格
    NABCD分析
    首尾相连的二维数组求最大字数组的和
    站立会议第三篇
    站立会议第二篇
    站立会议第一篇
    牛客算法周周练16D Rinne Loves Dynamic Graph(分层图最短路 + 堆优化dijkstra)
  • 原文地址:https://www.cnblogs.com/jxgxy/p/1340570.html
Copyright © 2011-2022 走看看