0

I having one Form with one button. I need to Restart Windows Explorer on Button1.Click. I Googled it but I have not get any proper solution. All tels that the solution works in WinXP, Vista but not in Win7. Please provide the accurate solution.

Rubi Halder
  • 583
  • 3
  • 12
  • 24

1 Answers1

2

Add uses on TlHelp32

In Windows 7 or above, this function works:

function KillTask(ExeFileName: string): Integer;
const
  PROCESS_TERMINATE = $0001;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  ProcessHandle: Cardinal;
  FProcessEntry32: TProcessEntry32;
begin
  Result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 

  if FSnapshotHandle = INVALID_HANDLE_VALUE then 
    RaiseLastOSError;
  try
    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
      begin 
        ProcessHandle:= OpenProcess(PROCESS_TERMINATE, BOOL(0),     FProcessEntry32.th32ProcessID), 0);
        if ProcessHandle > 0 then
        begin
          try  
            Result := Integer(TerminateProcess(ProcessHandle);
          finally
            CloseHandle(ProcessHandle);
          end; 
        end
        else
          RaiseLastOSError;   
      end;
      ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
    end;
  finally 
    CloseHandle(FSnapshotHandle);
  end; 
end;

It Kills and Start Again!

KillTask('explorer.exe');

In prior versions, it Only Kills!!

EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59