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.
Asked
Active
Viewed 957 times
0
-
2You might try to [`kill it`](http://stackoverflow.com/a/13770395/960757) in a hacky way and start it again. Why do you need this anyway ? – TLama Mar 22 '13 at 16:28
-
Does `taskkill /IM "explorer.exe" /F` (with admin privileges) work? – Andreas Rejbrand Mar 22 '13 at 16:31
-
@TLama Actually the first code in this post http://stackoverflow.com/q/13770128/2087187 is what he needs, because he wants to restart it! – EProgrammerNotFound Mar 22 '13 at 17:13
-
@Matheus, you meant rather something like [`this`](http://stackoverflow.com/a/11954740/960757), don't you ? – TLama Mar 22 '13 at 17:16
-
@TLama No, he said: "...I need to Restart Windows Explorer on...", and the KillTask method in the post does exactly this, it kills and Restart – EProgrammerNotFound Mar 22 '13 at 17:19
-
No..No.. The above " taskkill /IM "explorer.exe" /F " procedure kills only but not restarting explore again. – Rubi Halder Mar 22 '13 at 17:21
-
@Matheus, ah I see. Yes, that might be the option for Vista above systems since there if you kill explorer, it starts again automatically (except that hacky way I've mentioned in my first comment). Rubi, but you know how to start it again, don't you ? – TLama Mar 22 '13 at 17:23
-
@RubiHalder I tested it on windows 7 and restarted – EProgrammerNotFound Mar 22 '13 at 17:23
-
I have tried the first code also but I getting error for "TProcessEntry32", "CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);", "Process32First", "dwSize", "Process32Next", "th32ProcessID". – Rubi Halder Mar 22 '13 at 17:29
-
@RubiHalder uses TlHelp32 – EProgrammerNotFound Mar 22 '13 at 17:30
-
1@Rubi: Well, we all know how to start things using `ShellExecute`. Killing is the hard thing, especially (one might guess) when it comes to critical system processes. – Andreas Rejbrand Mar 22 '13 at 18:18
-
@AndreasRejbrand What you suggest? – EProgrammerNotFound Mar 22 '13 at 19:07
1 Answers
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
-
-
Then I have tried your hacky way also. It really killa explorer without Administrative previlege but never restarts. So... – Rubi Halder Mar 22 '13 at 17:38
-
Check if ShellExecute 'Explorer.exe' works to open without admin previleges – EProgrammerNotFound Mar 22 '13 at 17:46
-
@RubiHalder ShellExecute(0, pchar('open'), pchar('explorer.exe'), nil, nil, SW_HIDE); – EProgrammerNotFound Mar 22 '13 at 18:01
-