Saturday, January 22, 2011

Restart Explorer

Trik me-restart proses explorer.exe yang sedang aktif.
Deklarasi procedure :
function RestartExplorer: Boolean;
var
  wHnd: hWnd;
  PID: DWORD;
  hProc: THandle;
  exC: DWORD;
begin
  wHnd:= FindWindow('Progman','Program Manager');
  if wHnd <> 0 then
  begin
    GetWindowThreadProcessID(wHnd, PID);
    hProc := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_TERMINATE, False, PID);
    GetExitCodeProcess(hProc, exC);
    Result := TerminateProcess(hProc, exC);
   {Untuk beberapa kasus, explorer akan secara otomatis mengeksekusi
    dirinya sendiri, namun jika gagal :
    Tambahkan WinExec('explorer',SW_SHOWNORMAL);}

    CloseHandle(hProc);
  end
  else Result:= False;
end;
Contoh implementasi :
procedure TForm1.Button1Click(Sender: TObject);
begin
  if RestartExplorer then ShowMessage('OK') else ShowMessage('GAGAL');
end;


Atau bisa pake cara dibawah ini :

procedure killExplorer;
  var
    Wnd: HWND;
    ProcHandle: THandle;
    Result, ProcessID: Longint;
    ExitCode: DWord;
  begin
    Wnd := FindWindow('Progman', 'Program Manager');
    Result := GetWindowThreadProcessId(Wnd, @ProcessID);
    if Result <> 0 then
    begin
      ProcHandle := OpenProcess(1, false, ProcessID);
      GetExitCodeProcess(Wnd, ExitCode);
      TerminateProcess(ProcHandle, {-9}ExitCode);
    end;
  end;


contoh pakainya:

begin
  killExplorer;

end; 


Note :
It seems like Windows is restarting the explorer automatically. 
I don't know why it is not restarted if you use task manager...
So, I guess you will always get the exception ('Explorer already running'). 

At least that was the case on my Win2000. 

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum