Текущий архив: 2002.11.11;
Скачать: CL | DM;
Внизубить процесс Найти похожие ветки
← →
Sego (2002-10-31 10:53) [0]Можно ли программно, убить процесс, зная имя файла, который его запустил? Если можно токак, подскажите пожалуйста.
← →
Song (2002-10-31 11:00) [1]TerminateProcess(). Надо знать PID. Последний можно получить перебрав все процессы.
← →
Sego (2002-10-31 11:16) [2]2 Song ©
Cпасибо, но я не совсем готов, каким способом можна перебрать процессы, какими свойствами они обладают? И где их перебирать?
← →
Севостьянов Игорь (2002-10-31 11:20) [3]
EnumProcesses
The EnumProcesses function retrieves the process identifier for each process object in the system.
BOOL EnumProcesses(
DWORD *lpidProcess, // array of process identifiers
DWORD cb, // size of array
DWORD *cbNeeded // number of bytes returned
);
Parameters
lpidProcess
[out] Pointer to an array that receives the list of process identifiers.
cb
[in] Specifies the size, in bytes, of the lpidProcess array.
cbNeeded
[out] Receives the number of bytes returned in the lpidProcess array.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
It is a good idea to give EnumProcesses a large array of DWORD values, because it is hard to predict how many processes there will be at the time you call EnumProcesses. To determine how many processes were enumerated by the call to EnumProcesses, divide the resulting value in the cbNeeded parameter by sizeof(DWORD). There is no indication given when the buffer is too small to store all process identifiers.
To obtain process handles for the processes whose identifiers you have just obtained, call the OpenProcess function.
← →
Song (2002-10-31 11:21) [4]2Севостьянов Игорь © (31.10.02 11:20)
EnumProcesses() только для NT систем
← →
Севостьянов Игорь (2002-10-31 11:59) [5]Думаю куски кода тебе будут хоть немного в помощь...
Для Win9X
procedure TWin95Info.FillProcessInfoList(ListView: TListView;
ImageList: TImageList);
var
I: Integer;
ExeFile: string;
PE: TProcessEntry32;
HAppIcon: HIcon;
begin
Refresh;
ListView.Columns.Clear;
ListView.Items.Clear;
for I := Low(ProcessInfoCaptions) to High(ProcessInfoCaptions) do
with ListView.Columns.Add do
begin
if I = 0 then Width := 285
else Width := 75;
Caption := ProcessInfoCaptions[I];
end;
for I := 0 to FProcList.Count - 1 do
begin
PE := PProcessEntry32(FProcList.Items[I])^;
HAppIcon := ExtractIcon(HInstance, PE.szExeFile, 0);
try
if HAppIcon = 0 then HAppIcon := FWinIcon;
ExeFile := PE.szExeFile;
if ListView.ViewStyle = vsList then
ExeFile := ExtractFileName(ExeFile);
// insert new item, set its caption, add subitems
with ListView.Items.Add, SubItems do
begin
Caption := ExeFile;
Data := FProcList.Items[I];
Add(IntToStr(PE.cntThreads));
Add(IntToHex(PE.th32ProcessID, 8));
Add(IntToHex(PE.th32ParentProcessID, 8));
if ImageList <> nil then
ImageIndex := ImageList_AddIcon(ImageList.Handle, HAppIcon);
end;
finally
if HAppIcon <> FWinIcon then DestroyIcon(HAppIcon);
end;
end;
end;
procedure TWin95Info.Refresh;
var
PE: TProcessEntry32;
PPE: PProcessEntry32;
begin
FProcList.Clear;
if FSnap > 0 then CloseHandle(FSnap);
FSnap := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if FSnap = INVALID_HANDLE_VALUE then
raise Exception.Create("CreateToolHelp32Snapshot failed");
PE.dwSize := SizeOf(PE);
if Process32First(FSnap, PE) then // get process
repeat
New(PPE); // create new PPE
PPE^ := PE; // fill it
FProcList.Add(PPE); // add it to list
until not Process32Next(FSnap, PE); // get next process
end;
← →
Севостьянов Игорь (2002-10-31 12:00) [6]Для WinNT
procedure TWinNTInfo.FillDrivers(ListView: TListView;
ImageList: TImageList);
var
I: Integer;
DrvName: array[0..MAX_PATH] of char;
begin
for I := Low(FDrvList) to High(FDrvList) do
if GetDeviceDriverFileName(FDrvList[I], DrvName, SizeOf(DrvName)) > 0 then
with ListView.Items.Add do
begin
Caption := DrvName;
SubItems.Add(SDrvName);
SubItems.Add("$" + IntToHex(Integer(FDrvList[I]), 8));
end;
end;
procedure TWinNTInfo.FillProcesses(ListView: TListView;
ImageList: TImageList);
var
I: Integer;
Count: DWORD;
ProcHand: THandle;
ModHand: HMODULE;
HAppIcon: HICON;
ModName: array[0..MAX_PATH] of char;
begin
for I := Low(FProcList) to High(FProcList) do
begin
ProcHand := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,
FProcList[I]);
if ProcHand > 0 then
try
EnumProcessModules(Prochand, @ModHand, 1, Count);
if GetModuleFileNameEx(Prochand, ModHand, ModName, SizeOf(ModName)) > 0 then
begin
HAppIcon := ExtractIcon(HInstance, ModName, 0);
try
if HAppIcon = 0 then HAppIcon := FWinIcon;
with ListView.Items.Add, SubItems do
begin
Caption := ModName; // file name
Data := Pointer(FProcList[I]); // save ID
Add(SProcName); // "process"
Add(IntToStr(FProcList[I])); // process ID
Add("$" + IntToHex(ProcHand, 8)); // process handle
// priority class
Add(GetPriorityClassString(GetPriorityClass(ProcHand)));
// icon
if ImageList <> nil then
ImageIndex := ImageList_AddIcon(ImageList.Handle, HAppIcon);
end;
finally
if HAppIcon <> FWinIcon then DestroyIcon(HAppIcon);
end;
end;
finally
CloseHandle(ProcHand);
end;
end;
end;
procedure TWinNTInfo.FillProcessInfoList(ListView: TListView;
ImageList: TImageList);
var
I: Integer;
begin
Refresh;
ListView.Columns.Clear;
ListView.Items.Clear;
for I := Low(ProcessInfoCaptions) to High(ProcessInfoCaptions) do
with ListView.Columns.Add do
begin
if I = 0 then Width := 285
else Width := 75;
Caption := ProcessInfoCaptions[I];
end;
FillProcesses(ListView, ImageList); // Add processes to listview
FillDrivers(ListView, ImageList); // Add device drivers to listview
end;
procedure TWinNTInfo.Refresh;
var
Count: DWORD;
BigArray: array[0..$3FFF - 1] of DWORD;
begin
// Get array of process IDs
if not EnumProcesses(@BigArray, SizeOf(BigArray), Count) then
raise Exception.Create(SFailMessage);
SetLength(FProcList, Count div SizeOf(DWORD));
Move(BigArray, FProcList[0], Count);
// Get array of Driver addresses
if not EnumDeviceDrivers(@BigArray, SizeOf(BigArray), Count) then
raise Exception.Create(SFailMessage);
SetLength(FDrvList, Count div SizeOf(DWORD));
Move(BigArray, FDrvList[0], Count);
end;
← →
Севостьянов Игорь (2002-10-31 13:45) [7]Для Win9x смотри http://www.delphikingdom.com/asp/answer.asp?IDAnswer=1911 (procedure KillExcelProcess; - убивает Excel)
← →
Севостьянов Игорь (2002-10-31 14:02) [8]Под NT (писал под W2K)
unit TerminateProcesses;
interface
uses Windows, PSAPI;
function GetAllProcesses: Boolean;
implementation
function GetAllProcesses: Boolean;
Var
Counter, i :LongWord;
ProcHandle :THandle;
PIDs :array[0..1023] Of DWORD;
ModHndls :array[0..1023] Of DWORD;
pbNeeded :DWORD;
mbNeeded :DWORD;
ModulePath :String;
uiExitCode :UINT;
begin
Result := False;
EnumProcesses(@PIDs, Length(PIDs), pbNeeded);
for Counter := 0 To pbNeeded - 1 Do
begin
ProcHandle := OpenProcess({PROCESS_QUERY_INFORMATION or PROCESS_VM_READ} PROCESS_ALL_ACCESS, False, PIDs[Counter]);
if ProcHandle <> 0 then
begin
if EnumProcessModules(ProcHandle, @ModHndls, Length(ModHndls), mbNeeded) then
for i := 0 to (mbNeeded Div 4) - 1 do
begin
SetLength(ModulePath, MAX_PATH);
GetModuleFileNameEx(ProcHandle, ModHndls[i], PChar(ModulePath), MAX_PATH);
// Terminate process
//if Pos(AnsiLowerCase("iexplorer.exe"), AnsiLowerCase(ModulePath)) <> 0 then
// Result := TerminateProcess(ProcHandle, uiExitCode);
end;
CloseHandle(ProcHandle);
end;
if Result then Break;
end;
end;
end.
← →
Sego (2002-10-31 23:01) [9]2 Song
2 Севостьянов Игорь
Большое Вам спасибо!!!
← →
Sego (2002-10-31 23:22) [10]Вот нашел пример, именно тот вариант , который мне был необходим (просто по имени файла), проверено, под В98 работает идеально.
http://www.sources.ru/delphi/system/kill_task.shtml
Еще раз Большое спасибо за конструктивную информацию.
Страницы: 1 вся ветка
Текущий архив: 2002.11.11;
Скачать: CL | DM;
Память: 0.48 MB
Время: 0.007 c