Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to permanently terminate Windows Explorer (the "explorer.exe" process)?

I'm using the following code to terminate a process:

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;

The problem is, when I call the above function in order to permanently terminate the explorer.exe, the Windows Explorer terminates though, but it's re-started afterwards:

KillTask('explorer.exe');

I'm using Delphi XE3, Delphi 7 and Windows 8.

like image 648
Kobus Vdwalt Avatar asked Jan 20 '26 11:01

Kobus Vdwalt


1 Answers

Based on this Exit Explorer feature and code debugged by Luke in this post you may try to use the following code:

Warning:

This way is absolutely undocumented! So all constants and variables appearing in this post are fictitious. Any resemblance to real, documented code is purely coincidental :-)

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).

like image 120
TLama Avatar answered Jan 23 '26 00:01

TLama