• 怎样用delphi关闭并重新启动 explorer.exe进程


    uses
    Tlhelp32;

    function KillTask(ExeFileName:string):integer;
    const
    PROCESS_TERMINATE = $0001;
    var
    ContinueLoop: BOOLean;
    FSnapshotHandle: THandle;
    FProcessEntry32: TProcessEntry32;
    begin
    Result := 0;
    FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
    ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);

    while Integer(ContinueLoop) <> 0 do
    begin
    if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
    UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
    UpperCase(ExeFileName))) then
    Result := Integer(TerminateProcess(
    OpenProcess(PROCESS_TERMINATE,
    BOOL(0),
    FProcessEntry32.th32ProcessID),
    0));
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
    end;
    CloseHandle(FSnapshotHandle);
    end;

    调用的时候只需要 KillTask('explorer.exe');
    例如
    if KillTask('explorer.exe') <> 0 then
      showmessage('结束成功')
    else
      showmessage('无法结束');

    //================================

    以下是C++代码

    void __fastcall TfrmMain::CloseProcess(AnsiString ProcessPath) {  HANDLE hProcessSnap;  PROCESSENTRY32 pe32;

     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  if (hProcessSnap == (HANDLE)-1) return;

     memset(&pe32, 0, sizeof(pe32));  pe32.dwSize = sizeof(PROCESSENTRY32);  if (Process32First(hProcessSnap, &pe32))  {   do   {    if (ProcessPath.Pos((AnsiString)pe32.szExeFile))    {     HANDLE hAndle = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, false, pe32.th32ProcessID);     OutputDebugString(SysErrorMessage(GetLastError()).c_str());     TerminateProcess(hAndle, 0);     OutputDebugString(SysErrorMessage(GetLastError()).c_str());    }   }   while(Process32Next(hProcessSnap,&pe32));  }  CloseHandle(hProcessSnap); }

  • 相关阅读:
    实例
    LR接口测试---webservices
    LR常用函数整理
    Codeforces Round #639 (Div. 2) A. Puzzle Pieces
    Codeforces Round #640 (Div. 4)全部七题
    POJ3177 Redundant Paths(e-DCC+缩点)
    洛谷P3469 [POI2008]BLO-Blockade(割点)
    洛谷P3275 [SCOI2011]糖果(缩点+拓扑序DP)
    POJ1236 Network of Schools(强连通分量)
    P3387 【模板】缩点(Tarjan求强连通分量)
  • 原文地址:https://www.cnblogs.com/mingdep/p/4462946.html
Copyright © 2020-2023  润新知