高手指点:关于进程检测的问题(急啊!) 我怎么捕获到一个进程的运行,打比方说,系统启动时,word并没有运行,我想在word运行时执行一段程序,但我不知道如何得知word的运行。也就是当有进程运行时,如何知道,并如何判断是否是自己需要的进程运行。请高手指点设计的主要过程,最好能给出一定的源码,高分回报。我先谢谢大家了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 API拦截explorer.exe对CreateProcess的调用 每隔一定时间用findwindow查找窗口名 //参考//进程死亡自启动unit UMain;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, RpMemo, ComCtrls,math;const intMax = 255; //设置最大控制进程数type TForm1 = class(TForm) Button1: TButton; Timer1: TTimer; StatusBar1: TStatusBar; memo1: TMemo; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; function funEstablishProcess(strFileName: string): Pointer; //为程序创建子进程 function funInitFileStatus(): boolean; //初始化进程的相关信息 function funGetCurrentDir(const str: string): string; //从字符串取得该程序路径 function funAppIsRunning(const str: string): boolean; //function funTextOut(str: Pointer): Pointer; procedure Button1Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private { Private declarations } public { Public declarations } end;type intFileNameStruct = record //保存进程相关信息 intOrder: integer; strFileName: string; strExeName: string; end;var Form1: TForm1; piProcInfoGPS: array[1..intMax] of PROCESS_INFORMATION; intFileStatus: array[1..intMax] of intFileNameStruct; intNowCount: integer; //目前列表中进程数量implementation{$R *.dfm}{ TForm1 }function TForm1.funEstablishProcess(strFileName: string): Pointer;var siStartupInfo: STARTUPINFO; saProcess, saThread: SECURITY_ATTRIBUTES; fSuccess: boolean; i: integer;begin fSuccess := false; i := 1; ZeroMemory(@siStartupInfo, sizeof(siStartupInfo)); siStartupInfo.cb := sizeof(siStartupInfo); saProcess.nLength := sizeof(saProcess); saProcess.lpSecurityDescriptor := PChar(nil); saProcess.bInheritHandle := true; saThread.nLength := sizeof(saThread); saThread.lpSecurityDescriptor := PChar(nil); saThread.bInheritHandle := true; while (i <= intNowCount) do begin if (intFileStatus[i].strFileName = strFileName) then break; i := i + 1; end; if (i > 1) then Sleep(4000); fSuccess := CreateProcess(PChar(nil), pChar(strFileName), @saProcess, @saThread, false, CREATE_DEFAULT_ERROR_MODE, Pchar(nil), Pchar(funGetCurrentDir(intFileStatus[i].strFileName)), siStartupInfo, piProcInfoGPS[i]); if (not fSuccess) then begin intFileStatus[i].intOrder := 0; Form1.Memo1.Lines.Add('Create Process ' + strFileName + ' fail.'); end else begin intFileStatus[i].intOrder := 1; Form1.Memo1.Lines.Add('Create Process ' + strFileName + ' success.'); end; Result := nil;end;procedure TForm1.Button1Click(Sender: TObject);var i: integer;begin i := 0; for i := 1 to intNowCount do if (funAppIsRunning(intFileStatus[i].strExeName) = False) then funEstablishProcess(intFileStatus[i].strFileName);end;procedure TForm1.Timer1Timer(Sender: TObject);var dwExitCode: DWORD; fprocessExit: boolean; i: integer;begin Application.ProcessMessages; dwExitCode := 0; fprocessExit := false; i := 0; for i := 1 to intNowCount do begin fprocessExit := GetExitCodeProcess(piProcInfoGPS[i].hProcess, dwExitCode); if (fprocessExit and (dwExitCode <> STILL_ACTIVE)) then begin Memo1.Lines.Add(#13 + #10 + DateTimeToStr(Now) + ' : ' + '进程终止'); CloseHandle(piProcInfoGPS[i].hThread); CloseHandle(piProcInfoGPS[i].hProcess); intFileStatus[i].intOrder := 0; funEstablishProcess(intFileStatus[i].strFileName); end; end; statusbar1.Panels[0].Text := '进程管理'; statusbar1.Panels[1].Text := '进程数量:' + inttostr(intNowCount) + '个'; statusbar1.Panels[2].Text := datetimetostr(now);end;function TForm1.funInitFileStatus: boolean;var i: integer; strFileNameList: TstringList;begin i := 0; strFileNameList := TstringList.Create(); strFileNameList.LoadFromFile('FileNameList.txt'); for i := 1 to strFileNameList.Count do begin intFileStatus[i].intOrder := 0; intFileStatus[i].strFileName := strFileNameList.Strings[i - 1]; intFileStatus[i].strExeName := copy(intFileStatus[i].strFileName, length(funGetCurrentDir(intFileStatus[i].strFileName)) + 2, //2 is len('\')+1 length(intFileStatus[i].strFileName) - length(funGetCurrentDir(intFileStatus[i].strFileName)) - 5); //5 is len('.exe')+1 end; intNowCount := strFileNameList.Count; strFileNameList.Free; Result := true;end;procedure TForm1.FormCreate(Sender: TObject);begin funInitFileStatus;end;function TForm1.funGetCurrentDir(const str: string): string;var i: integer;begin for i := length(str) - 1 downto 0 do begin if str[i] = '\' then break; end; Result := copy(str, 0, i - 1);end;procedure TForm1.Button2Click(Sender: TObject);begin Form1.Close;end;function TForm1.funAppIsRunning(const str: string): boolean;var intFileHwnd: hWnd;begin Result := False; intFileHwnd := windows.FindWindow(nil, Pchar(str)); if intFileHwnd <> 0 then Result := true;end;procedure TForm1.Button3Click(Sender: TObject);begin Timer1.Enabled := not Timer1.Enabled; case integer(Timer1.Enabled) of 0: begin Button3.Caption := '继续(&S)'; end; 1: begin Button3.Caption := '停止(&S)'; end; end;end;function MyThreadFunc(P: string): Longint; stdcall;var i: longint; DC: HDC; S: string;begin DC := GetDC(Form1.Handle); for i := 0 to 500000 do begin S := Inttostr(i); Textout(DC, 10, 10, Pchar(s), length(s)); end; ReleaseDC(Form1.Handle, DC); result := 1;end;procedure TForm1.Button4Click(Sender: TObject);var hThread: THandle; dwThreadID: dWord;begin hThread := CreateThread(nil, 0, @MyThreadFunc, nil, 0, dwThreadID); if hThread = 0 then messagebox(handle, '线程创建失败', '报警提示', mb_ok);end;procedure TForm1.Button5Click(Sender: TObject);vari:double;constn=1.5;begini:=1.23;showmessage(floattostr(round(i*n,2)));end;end. 这段代码可以参考,但关于使用多线程,你可以去掉,我后来修改成多线程时,没调试好,好象有错误button5的处理没用的哦,别多想 请教一个报表显示的问题 窗体显示问题 fast report多栏报表 程序优化问题 saien(有问必答) : 倒粪真的没意思,真的,我原来有个帐号在水源成星,可是就是因为其中有300分的粪,我就没用了,要凭本事成星,才有成 关于ADO参考的下载地址! 如何获得字段的描述信息? 快捷方式起始地址的问题??? 如何在B/S结构下,将一电子表格文件(非EXECL文件)以EXECL文件格式保存到本地的A驱里! scktsvr.exe起不来是怎么回事 急急!!!TWwdbgrid控件问题 关于数据还原的问题
//进程死亡自启动
unit UMain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, RpMemo, ComCtrls,math;const intMax = 255; //设置最大控制进程数type
TForm1 = class(TForm)
Button1: TButton;
Timer1: TTimer;
StatusBar1: TStatusBar;
memo1: TMemo;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
function funEstablishProcess(strFileName: string): Pointer;
//为程序创建子进程
function funInitFileStatus(): boolean; //初始化进程的相关信息
function funGetCurrentDir(const str: string): string;
//从字符串取得该程序路径
function funAppIsRunning(const str: string): boolean;
//function funTextOut(str: Pointer): Pointer;
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;type intFileNameStruct = record //保存进程相关信息
intOrder: integer;
strFileName: string;
strExeName: string;
end;var
Form1: TForm1;
piProcInfoGPS: array[1..intMax] of PROCESS_INFORMATION;
intFileStatus: array[1..intMax] of intFileNameStruct;
intNowCount: integer; //目前列表中进程数量
implementation{$R *.dfm}{ TForm1 }function TForm1.funEstablishProcess(strFileName: string): Pointer;
var
siStartupInfo: STARTUPINFO;
saProcess, saThread: SECURITY_ATTRIBUTES;
fSuccess: boolean;
i: integer;
begin
fSuccess := false;
i := 1; ZeroMemory(@siStartupInfo, sizeof(siStartupInfo));
siStartupInfo.cb := sizeof(siStartupInfo);
saProcess.nLength := sizeof(saProcess);
saProcess.lpSecurityDescriptor := PChar(nil);
saProcess.bInheritHandle := true;
saThread.nLength := sizeof(saThread);
saThread.lpSecurityDescriptor := PChar(nil);
saThread.bInheritHandle := true;
while (i <= intNowCount) do
begin
if (intFileStatus[i].strFileName = strFileName) then
break;
i := i + 1;
end;
if (i > 1) then
Sleep(4000);
fSuccess := CreateProcess(PChar(nil), pChar(strFileName), @saProcess,
@saThread, false,
CREATE_DEFAULT_ERROR_MODE, Pchar(nil),
Pchar(funGetCurrentDir(intFileStatus[i].strFileName)),
siStartupInfo, piProcInfoGPS[i]);
if (not fSuccess) then
begin
intFileStatus[i].intOrder := 0;
Form1.Memo1.Lines.Add('Create Process ' + strFileName + ' fail.');
end
else
begin
intFileStatus[i].intOrder := 1;
Form1.Memo1.Lines.Add('Create Process ' + strFileName + ' success.');
end;
Result := nil;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
i := 0; for i := 1 to intNowCount do
if (funAppIsRunning(intFileStatus[i].strExeName) = False) then
funEstablishProcess(intFileStatus[i].strFileName);
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
dwExitCode: DWORD;
fprocessExit: boolean;
i: integer;
begin
Application.ProcessMessages;
dwExitCode := 0;
fprocessExit := false;
i := 0; for i := 1 to intNowCount do
begin
fprocessExit := GetExitCodeProcess(piProcInfoGPS[i].hProcess, dwExitCode);
if (fprocessExit and (dwExitCode <> STILL_ACTIVE)) then
begin
Memo1.Lines.Add(#13 + #10 + DateTimeToStr(Now) + ' : '
+ '进程终止');
CloseHandle(piProcInfoGPS[i].hThread);
CloseHandle(piProcInfoGPS[i].hProcess);
intFileStatus[i].intOrder := 0;
funEstablishProcess(intFileStatus[i].strFileName);
end;
end; statusbar1.Panels[0].Text := '进程管理';
statusbar1.Panels[1].Text := '进程数量:' + inttostr(intNowCount) + '个';
statusbar1.Panels[2].Text := datetimetostr(now);
end;
function TForm1.funInitFileStatus: boolean;
var i: integer;
strFileNameList: TstringList;
begin
i := 0; strFileNameList := TstringList.Create();
strFileNameList.LoadFromFile('FileNameList.txt');
for i := 1 to strFileNameList.Count do
begin
intFileStatus[i].intOrder := 0;
intFileStatus[i].strFileName := strFileNameList.Strings[i - 1];
intFileStatus[i].strExeName :=
copy(intFileStatus[i].strFileName,
length(funGetCurrentDir(intFileStatus[i].strFileName)) + 2,
//2 is len('\')+1
length(intFileStatus[i].strFileName) -
length(funGetCurrentDir(intFileStatus[i].strFileName)) - 5);
//5 is len('.exe')+1
end; intNowCount := strFileNameList.Count;
strFileNameList.Free;
Result := true;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
funInitFileStatus;
end;function TForm1.funGetCurrentDir(const str: string): string;
var i: integer;
begin
for i := length(str) - 1 downto 0 do
begin
if str[i] = '\' then
break;
end;
Result := copy(str, 0, i - 1);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.Close;
end;function TForm1.funAppIsRunning(const str: string): boolean;
var intFileHwnd: hWnd;
begin
Result := False; intFileHwnd := windows.FindWindow(nil, Pchar(str));
if intFileHwnd <> 0 then
Result := true;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Timer1.Enabled := not Timer1.Enabled;
case integer(Timer1.Enabled) of
0:
begin
Button3.Caption := '继续(&S)';
end;
1:
begin
Button3.Caption := '停止(&S)';
end;
end;
end;function MyThreadFunc(P: string): Longint; stdcall;
var
i: longint;
DC: HDC;
S: string;
begin
DC := GetDC(Form1.Handle);
for i := 0 to 500000 do begin
S := Inttostr(i);
Textout(DC, 10, 10, Pchar(s), length(s));
end;
ReleaseDC(Form1.Handle, DC);
result := 1;
end;
procedure TForm1.Button4Click(Sender: TObject);
var hThread: THandle;
dwThreadID: dWord;
begin
hThread := CreateThread(nil, 0, @MyThreadFunc, nil, 0, dwThreadID);
if hThread = 0 then
messagebox(handle, '线程创建失败', '报警提示', mb_ok);
end;procedure TForm1.Button5Click(Sender: TObject);
var
i:double;
const
n=1.5;
begin
i:=1.23;
showmessage(floattostr(round(i*n,2)));
end;
end.
button5的处理没用的哦,别多想