请问如何检测WINDOWS进程与终止进程?急需!!!!
解决方案 »
- 中间层怎样定义function啊?我怎么只能添加procedure
- 小问题~~关于变量定义的~~帮忙看看~
- 怎样解决这个问题?
- listbox中有26个选项,怎样在程序运行时得到选择的结果
- 一个非常简单的循环里面查找的问题
- while not eof 的问题:
- ReportBuilder6.0有没有汉化版的?高分征求!!
- 高手请帮忙,在线等待.为何我提交数据时,总是提示"无法为更新行集定位:一些值可能已在最后读取后改变".?急!
- 想在DBGrid展示的查询结果加一列Checkbox用于让用户“选中”或“取消选中”,怎么样才能实现?
- 一个类的小问题。
- 图片的存取问题
- listbox删除记录时遇到的问题
同时注意调用SetEnvironmentVariable()来设置相关的环境变量。
接下来,我们来具体看你的程序代码,其实你可以尝试设定一个变量用来检测进程是否触发,
自动判断后触发进程。
你应该加入OnTimer事件并设定一个Boolean,如processExit:boolean;
并令processExit:=false;
processExit:=GetExitCodeProcess(myprocess_information,&s);
这样通过(processExit and 进程终止时的退出码(即lpExitCode)<>STILL_ACTIVE)方可正确判
断进程的中止。然后在程序结尾再调用CreateProcess重新创建进程。方可正确进行继续的操作。
所以最好的方法是单独定义一个过程(进程调用),每次中止结束进程后再次调用该过程即可!
Windows, Messages, SysUtils, Classes, Graphics, TLHelp32, Controls, Forms, Dialogs,
StdCtrls;type
TProcessInfo = record
ExeFile: string;
ProcessId: DWORD;
end;
ProcessInfo = ^TProcessInfo;
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure ProcessList(var pList: TList);
procedure My_RunFileScan(ListboxRunFile: TListBox);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
Current: TList;
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.ProcessList(var pList: TList);
var
p: ProcessInfo;
ok: Bool;
ProcessListHandle: THandle;
ProcessStruct: TProcessEntry32;
begin
PList := TList.Create;
PList.Clear;
ProcessListHandle := CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
ProcessStruct.dwSize := Sizeof(ProcessStruct);
ok := Process32First(ProcessListHandle, ProcessStruct);
while Integer(ok) <> 0 do
begin
new(p);
p.ExeFile := ProcessStruct.szExeFile;
p.ProcessID := ProcessStruct.th32ProcessID;
PList.Add(p);
ok := Process32Next(ProcessListHandle, ProcessStruct);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
h: THandle;
a: DWORD;
p: PRocessInfo;
begin
if ListBox1.ItemIndex >= 0 then
begin
p := Current.Items[ListBox1.ItemIndex];
h := openProcess(Process_All_Access, true, p.ProcessID);
GetExitCodeProcess(h, a); if Integer(TerminateProcess(h, a)) <> 0 then
begin
My_RunFileScan(ListBox1);
end;
end
else
Application.MessageBox('请先选择一个进程!', '黑洞', MB_ICONERROR + MB_OK);
end;procedure TForm1.My_RunFileScan(ListboxRunFile: TListBox);
var
i: Integer;
p: PRocessInfo;
begin
current := TList.Create;
Current.Clear;
ListboxRunFile.Clear;
ProcessList(Current);
for i := 0 to Current.Count - 1 do
begin
new(p);
p := Current.Items[i];
ListboxRunFile.Items.Add(p.ExeFile);
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
My_RunFileScan(ListBox1);
end;end.
Left = 128
Top = 125
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object ListBox1: TListBox
Left = 16
Top = 8
Width = 281
Height = 305
ImeName = '中文 (简体) - 微软拼音'
ItemHeight = 13
TabOrder = 0
end
object Button1: TButton
Left = 360
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 376
Top = 64
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
end
---------------------------------------
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
Panel1: TPanel;
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
uses psapi;
{$R *.DFM}procedure TForm1.Button2Click(Sender: TObject);
type
integer = DWORD; // different versions of psapi.pas floating around
var
i,j,pidNeeded,modNeeded : Integer;
PIDList : array[0..1000] of Integer; // 1000 should be enough
MODList : array[0..1000] of HInst;
PIDName : array [0..MAX_PATH - 1] of char;
MODName : array [0..MAX_PATH - 1] of char;
PH : THandle;
begin // fill an array with process ids
if not enumprocesses (@PIDList, 1000, pidNeeded) then
begin
ListBox1.Items.Add('Need psapi.dll');
exit;
end; // now open each process and its modules
for i := 0 to (pidNeeded div sizeof (Integer)- 1) do
begin
// get a handle to the process
PH := OpenProcess (PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,
PIDList[i]); if PH <> 0 then
begin
// print the process name
if GetModuleBaseName (PH, 0, PIDName, sizeof (PIDName)) > 0 then
begin
ListBox1.Items.Add('process : ' + PIDName); // fill an array of modules associated with this process
if not EnumProcessModules (PH,@MODList,1000, modNeeded) then modNeeded:= 0; // print the modules in the list
for j := 0 to (modNeeded div sizeof (hInst) - 1) do
if GetModuleFileNameEx (PH, MODList[j], MODName,sizeof(MODName)) > 0 then
ListBox1.Items.Add(' module: ' + MODName); if PH > 0 then CloseHandle(PH);
end;
end;
end;
end;end.
function GetProcessIdFromWindow(var TargetWnd:Hwnd):Dword;stdcall;export;
var
TargetThreadId:Dword;
TargetProcessId:Dword;
begin
if iswindow(targetWnd) then
begin
TargetThreadId:=GetWindowThreadProcessId(TargetWnd,TargetProcessId);
result := TargetProcessId;
end
else
result:=$0;
end;
function KillProcess(var ID:DWORD):boolean;stdcall;
var
h,a:THandle; //ID:是要杀死进程的ID
begin
h:=openprocess(PROCESS_ALL_ACCESS,True,Id);
getExitCodeProcess(h,a);
if terminateProcess(h,a) then
begin
//进程已关闭
result:=true;
end
else
//进程关闭失败
result:=false;
end;