0

我有一个带有一个按钮的表单。我需要在 Button1.Click 上重新启动 Windows 资源管理器。我用谷歌搜索了它,但我没有得到任何适当的解决方案。所有的电话,该解决方案适用于 WinXP、Vista 但不适用于 Win7。请提供准确的解决方案。

4

1 回答 1

2

在 TlHelp32 上添加使用

在 Windows 7 或更高版本中,此功能有效:

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;

它杀死并重新开始!

KillTask('explorer.exe');

在以前的版本中,它只会杀人!!

于 2013-03-22T17:28:43.907 回答