Kobus Vdwalt
Kobus Vdwalt

Reputation: 357

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.

Upvotes: 3

Views: 2782

Answers (2)

Martincg
Martincg

Reputation: 11

Suprising anyone got this to work because the parameters in findwindow are reversed and wrong. Should be

TrayHandle := FindWindow(nil, pchar('Shell_TrayWnd'));

and

PostMessage(TrayHandle,WM_CLOSE{ WM_EXITEXPLORER}, 0, 0);

works fine.

Upvotes: 0

TLama
TLama

Reputation: 76713

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

Upvotes: 9

Related Questions