zoukankan      html  css  js  c++  java
  • 关闭进程

      1. unit Tlhelp323;
      2.  
         
      3.  
        interface
      4.  
         
      5.  
        uses
      6.  
        Windows,SysUtils,Tlhelp32;
      7.  
         
      8.  
        function KillTask(ExeFileName: string): Integer; //关闭进程
      9.  
        function EnableDebugPrivilege: Boolean; //提升权限
      10.  
        function FindProcessId(ExeFileName: string):THandle; //查找进程
      11.  
         
      12.  
        implementation
      13.  
         
      14.  
        function FindProcessId(ExeFileName: string):THandle;
      15.  
        var
      16.  
        ContinueLoop:BOOL;
      17.  
        FSnapshotHandle:THandle;
      18.  
        FProcessEntry32:TProcessEntry32;
      19.  
        begin
      20.  
        result:=0;
      21.  
        FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
      22.  
        FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
      23.  
        ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
      24.  
        while integer(ContinueLoop)<>0 do
      25.  
        begin
      26.  
        if UpperCase(FProcessEntry32.szExeFile)=UpperCase(ExeFileName) then
      27.  
        begin
      28.  
        result:=FProcessEntry32.th32ProcessID;
      29.  
        break;
      30.  
        end;
      31.  
        ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
      32.  
        end;
      33.  
        CloseHandle (FSnapshotHandle);
      34.  
        end;
      35.  
         
      36.  
        function KillTask(ExeFileName: string): Integer;
      37.  
        const
      38.  
        PROCESS_TERMINATE = $0001;
      39.  
        var
      40.  
        ContinueLoop: boolean;
      41.  
        FSnapshotHandle: THandle;
      42.  
        FProcessEntry32: TProcessEntry32;
      43.  
        begin
      44.  
        Result := 0;
      45.  
        FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      46.  
        FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
      47.  
        ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
      48.  
         
      49.  
        while Integer(ContinueLoop) <> 0 do
      50.  
        begin
      51.  
        if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
      52.  
        UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
      53.  
        UpperCase(ExeFileName))) then
      54.  
        Result := Integer(TerminateProcess(
      55.  
        OpenProcess(PROCESS_TERMINATE,
      56.  
        BOOL(0),
      57.  
        FProcessEntry32.th32ProcessID),
      58.  
        0));
      59.  
        ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
      60.  
        end;
      61.  
        CloseHandle(FSnapshotHandle);
      62.  
        end;
      63.  
         
      64.  
        //但是对于服务程序,它会提示"拒绝访问".其实只要程序拥有Debug权限即可:
      65.  
        function EnableDebugPrivilege: Boolean;
      66.  
        function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;
      67.  
        var
      68.  
        TP: TOKEN_PRIVILEGES;
      69.  
        Dummy: Cardinal;
      70.  
        begin
      71.  
        TP.PrivilegeCount := 1;
      72.  
        LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);
      73.  
        if bEnable then
      74.  
        TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
      75.  
        else TP.Privileges[0].Attributes := 0;
      76.  
        AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);[Page]
      77.  
        Result := GetLastError = ERROR_SUCCESS;
      78.  
        end;
      79.  
        var
      80.  
        hToken: Cardinal;
      81.  
        begin
      82.  
        OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);
      83.  
        result:=EnablePrivilege(hToken, 'SeDebugPrivilege', True);
      84.  
        CloseHandle(hToken);
      85.  
        end;
      86.  
         
      87.  
        end.
      88.  
  • 相关阅读:
    用wamp配置的环境,想用CMD连接mysql怎么连
    Mysql删除表
    MySQL创建表
    Leetcode 130. Surrounded Regions
    Leetcode 111. Minimum Depth of Binary Tree
    Leetcode 110. Balanced Binary Tree
    Leetcode 98. Validate Binary Search Tree
    Leetcode 99. Recover Binary Search Tree
    Leetcode 108. Convert Sorted Array to Binary Search Tree
    Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/maweiwei/p/11270161.html
Copyright © 2011-2022 走看看