3

我正在使用以下代码终止进程:

function KillTask(ExeFileName: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop: BOOL;
  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;

问题是,当我调用上述函数以永久终止 时explorer.exe,Windows 资源管理器会终止,但之后会重新启动:

KillTask('explorer.exe');

我正在使用 Delphi XE3、Delphi 7 和 Windows 8。

4

1 回答 1

9

基于此Exit Explorer功能和 Luke in 调试的代码,this post您可以尝试使用以下代码:

警告:

这种方式绝对是无证的!所以这篇文章中出现的所有常量和变量都是虚构的。与真实的、记录在案的代码的任何相似之处纯属巧合:-)

function ExitExplorer: Boolean;
var
  TrayHandle: HWND;
const
  WM_EXITEXPLORER = $5B4;
begin
  Result := False;
  TrayHandle := FindWindow('Shell_TrayWnd', nil);
  if TrayHandle <> 0 then
    Result := PostMessage(TrayHandle, WM_EXITEXPLORER, 0, 0);
end;

I've tested it in Windows 7, where it works and doesn't even need the administrator elevation. Don't know how about the other systems (I'd say this won't work at least on Windows XP, but it's just a guess).

于 2012-12-07T20:19:56.283 回答