怎样用delphi关闭并重新启动 explorer.exe进程

uses
Tlhelp32;

function KillTask(ExeFileName:string):integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOLean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
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
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

调用的时候只需要 KillTask('explorer.exe');
例如
if KillTask('explorer.exe') <> 0 then
  showmessage('结束成功')
else
  showmessage('无法结束');

//================================

以下是C++代码

void __fastcall TfrmMain::CloseProcess(AnsiString ProcessPath) {  HANDLE hProcessSnap;  PROCESSENTRY32 pe32;

hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  if (hProcessSnap == (HANDLE)-1) return;

memset(&pe32, 0, sizeof(pe32));  pe32.dwSize = sizeof(PROCESSENTRY32);  if (Process32First(hProcessSnap, &pe32))  {   do   {    if (ProcessPath.Pos((AnsiString)pe32.szExeFile))    {     HANDLE hAndle = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, false, pe32.th32ProcessID);     OutputDebugString(SysErrorMessage(GetLastError()).c_str());     TerminateProcess(hAndle, 0);     OutputDebugString(SysErrorMessage(GetLastError()).c_str());    }   }   while(Process32Next(hProcessSnap,&pe32));  }  CloseHandle(hProcessSnap); }

上一篇:[daily][grub2] grub2修改内核选项


下一篇:SQL基础篇---函数及其函数配套使用的关键字