How do I restart the "explorer.exe" shell with Delphi?

204 Views Asked by At

I want to restart the "explorer.exe" e.g. the TaskBar process with Delphi.

In a batch file I would do (works!):

taskkill.exe /IM explorer.exe /F
start explorer.exe

In Delphi I am trying to use ShellExecute for both commands. Killing works, however, I do not manage to get the explorer back.

How should I call ShellExecute to restart the explorer including taskbar (not just one single file browser window)?

2

There are 2 best solutions below

5
dexamenos On

If explorer itself is not running, ShellExecute needs the full path, i.e. C:\Windows\explorer.exe in order to work.

2
Andre Ruebel On

It is a difference if you want to close the explorer windows or the taskbar. The explorer windows you can close by iterating through the windowlist using the getwindow function and send a wm_close message to all explorer windows. You can recognize the explorer windows by being 'CabinetWClass' or 'ExploreWClass' classtype.

The taskbar must be closed by finding the window with "Progman" title and killing that one, because it won't respond to wm_close. The taskbar should reopen automatically. Example function for closing the taskbar:

procedure TForm.Button1Click(Sender: TObject);
var
  wnd: hwnd;
  pid: dword;
  processhandle: thandle;
begin
  wnd := findwindow('Progman', NIL);
  IF wnd > 0 THEN
  BEGIN
    GetWindowThreadProcessId(wnd, pid);
    IF (pid > 0) THEN
    BEGIN
      processhandle := OpenProcess(1, false, pid);
      IF (processhandle > 0) THEN
      BEGIN
        TerminateProcess(processhandle, 0);
        CloseHandle(processhandle);
      END;
    END;
  END;
end;