procedure TForm4.Timer1Timer(Sender: TObject);
var
hsnapshot:thandle; //用于获得进程列表
lppe:TProcessEntry32; //用于查找进程
found:boolean; //用于判断进程遍历是否完成
killhandle:thandle; //用于杀死进程
myinifile:tinifile;
filename:string;
setions:tstringlist;
i:integer;
strs:string;
counts:integer;
begin
counts:=0;
myinifile:=tinifile.Create(paramstr(0)+'waitexe.ini');
myinifile.ReadSections(setions);
hsnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); ////////////////////提示出错!
lppe.dwSize:=SizeOf(TProcessEntry32);//在调用process32first api之前,需要初始化lppe记录的大小
Found:=process32first(hsnapshot,lppe);//将进程列表的第一个进程信息读入到ppe记录中
if setions.Count>=0 then
begin
for i := 0 to setions.Count - 1 do
begin
strs:=trim(myinifile.ReadString(setions[i],'filesrc',''));
if strs<>'' then
begin
while found do
begin
if (Uppercase(ExtractFileName(lppe.szExeFile))=uppercase(strs)) or (uppercase(lppe.szExeFile)=uppercase(strs)) then
begin
counts:=counts+1;
end;//if (upper.....)
found:=Process32Next(hSnapshot,lppe);
end;//while found do
end;// if strs<>'' then
end;//for i=0 to setions.count -1 do
end;//if setions.count>0=then
setions.Destroy ;
myinifile.Destroy;
hSnapshot:=0;
//if counts=0 then
//begin
//label1.caption:=intTostr(counts);
// form4.close;
//end;
end;运行时出现如下错,请各位高手帮帮忙,谢谢了!
first chance exception at $7C812A5B.exception class EAccessVionlation with message 'Access violation at address 004152D3 in module 'project2.exe'.read of address 00000010'.process project2.exe(2120)
myinifile.ReadSections(setions);
而不是
hsnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
因此,在ReadSections之前加上下面一行代碼即可。
Setions:=TStringList.Create;