这样吧,我先给你一个自己写的函数吧:先保证 uses Tlhelp32function FindProcess(exeName:string):integer;
var
SnapshotHandle:Thandle;
MyProcessEntry:TProcessEntry32;
ContinueLoop:Boolean;
begin
Result:=0;
MyProcessEntry.dwSize:=SizeOf(MyProcessEntry);
SnapshotHandle:=CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS,0);
ContinueLoop:=Process32First(SnapshotHandle,MyProcessEntry);
while ContinueLoop=True do
begin
if UpperCase(ExtractFileName(MyProcessEntry.szExeFile))=UpperCase(exeName) then
begin
Result:=1;
break;
end;
ContinueLoop:=Process32Next(SnapshotHandle,MyProcessEntry);
end;
end;说明:
唯一的参数是可执行文件名.如果找到,返回1,否则返回0. OK?
var
SnapshotHandle:Thandle;
MyProcessEntry:TProcessEntry32;
ContinueLoop:Boolean;
begin
Result:=0;
MyProcessEntry.dwSize:=SizeOf(MyProcessEntry);
SnapshotHandle:=CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS,0);
ContinueLoop:=Process32First(SnapshotHandle,MyProcessEntry);
while ContinueLoop=True do
begin
if UpperCase(ExtractFileName(MyProcessEntry.szExeFile))=UpperCase(exeName) then
begin
Result:=1;
break;
end;
ContinueLoop:=Process32Next(SnapshotHandle,MyProcessEntry);
end;
end;说明:
唯一的参数是可执行文件名.如果找到,返回1,否则返回0. OK?
找了段代码,你看看吧
--------------------------------------------
unit Unit1;interfaceuses
Windows, Messages, TlHelp32,SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;type
TForm1 = class(TForm)
lvProcs: TListView;
btnRefresh: TButton;
Button2: TButton;
lvThreads: TListView;
procedure btnRefreshClick(Sender: TObject);
procedure lvProcsSelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
private
{ Private declarations }
procedure FillThreadList(ProcID:Integer);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}{ TForm1 }procedure TForm1.FillThreadList(ProcID: Integer);
var
hSnapshot:THandle;
ThreadEntry:TThreadEntry32;
RetVal:Bool;
LvItem:TListItem;
begin
lvThreads.Items.Clear;
hSnapshot:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS or TH32CS_SNAPTHREAD,GetCurrentProcessId);
ThreadEntry.dwSize := Sizeof(ThreadEntry);
RetVal := Thread32First(hSnapshot,ThreadEntry);
while RetVal do
begin
if ThreadEntry.th32OwnerProcessID = Integer(ProcID) then
begin
LvItem := lvThreads.Items.Add;
LvItem.Data := Pointer((ThreadEntry.th32ThreadID));
LvItem.Caption := IntToStr(ThreadEntry.th32ThreadID);
end;
RetVal := Thread32Next(hSnapshot,ThreadEntry);
end;
CloseHandle(hSnapshot);
end;procedure TForm1.btnRefreshClick(Sender: TObject);
var
hSnapshot:THandle;
ProcEntry:TProcessEntry32;
RetVal:Bool;
LvItem:TListItem;
// TvNode:TTreeNode;
// ThreadList:TList;
begin
lvProcs.Items.Clear;
hSnapshot:= CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS or TH32CS_SNAPTHREAD,GetCurrentProcessId);
ProcEntry.dwSize := Sizeof(ProcEntry);
RetVal := Process32First(hSnapshot,ProcEntry);
while RetVal do
begin
LvItem :=lvProcs.Items.Add;
LvItem.Caption :=(ProcEntry.szExeFile);
LvItem.SubItems.Add(IntToStr(ProcEntry.th32ProcessID));
LvItem.SubItems.Add(IntToStr(ProcEntry.th32ParentProcessID));
LvItem.SubItems.Add(IntToStr(ProcEntry.cntThreads));
LvItem.Data:=(Pointer(ProcEntry.th32ProcessID));
RetVal := Process32Next(hSnapshot,ProcEntry);
FillThreadList(ProcEntry.th32ProcessID);
end;
CloseHandle(hSnapShot);
end;procedure TForm1.lvProcsSelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
begin
if Selected then
begin
FillThreadList(Integer(Item.Data));
end;
end;end.
ContinueLoop:=Process32First(SnapshotHandle,MyProcessEntry);
*********
while ContinueLoop=True do
begin
if UpperCase(ExtractFileName(MyProcessEntry.szExeFile))=UpperCase(exeName) then
begin
*****************这条语句就执行之后ContinueLoop为false; while 语句就没有执行.
var
lpProcess: TProcessEntry32;
lpModule: TModuleEntry32;
lpThread: TThreadEntry32;
foundProcess,FoundModule,FoundThread : boolean;
MyHand,HandThread,HandModule : THandle;
tmpstr,tmpadd: string;
tmpid,curid: string;
MyID: int64;
i,iPriority,j: integer;
begin
lpProcess.dwSize:=sizeof(PROCESSENTRY32);
tmpstr:=trim(combobox1.Text);
i:=pos('[',tmpstr);
tmpstr:=copy(tmpstr,i+3,length(combobox1.text)-i-3);
tmpid:=tmpstr;
MyID:=hextoint64(tmpid); MyHand:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FoundProcess:=Process32First(Myhand,lpProcess);
RichEdit1.Lines.Clear; while FoundProcess do
begin
if lpProcess.th32ProcessID=MyID then
begin
tmpstr:=StrPas(lpProcess.szExeFile);
curid:=format('%08x',[lpprocess.th32ProcessID]);
if curid=tmpid then
begin
Richedit1.Lines.Add('FileName: '+lpProcess.szExeFile);
RichEdit1.Lines.Add(' PID='+format('%08X',[lpProcess.th32ProcessID])+', ParentPID='+Format('%08X',[lpProcess.th32ParentProcessID]));
RichEdit1.Lines.Add(' PriorityClass='+format('%d',[lpProcess.pcpriclassbase])+', Threads='+format('%d',[lpProcess.cntThreads])); HandModule:=CreateToolhelp32Snapshot(TH32CS_SNAPModule,lpProcess.th32ProcessID);
lpModule.dwSize:=SizeOf(TModuleEntry32);
foundModule:=Module32First(HandModule,lpModule);
RichEdit1.Lines.Add('');
RichEdit1.Lines.Add('Modules Information:');
RichEdit1.Lines.Add(format(' Usage BaseAddr(Image) %8s Module', ['Size']));
while foundModule do
begin
Tmpadd:='';
if lpModule.ProccntUsage=65535 then
TmpAdd:=TmpAdd+' Fixed'
else
TmpAdd:=TmpAdd+' '+format('%5s',[inttostr(lpModule.ProccntUsage)]);
Tmpadd:=TmpAdd+format(' %08x',[int64(lpModule.modBaseAddr)])+' ';
TmpAdd:=TmpAdd+format(' %10u',[lpModule.modBaseSize]);
TmpAdd:=TmpAdd+Format(' %s',[lpModule.szExePath]);
RichEdit1.Lines.Add(TmpAdd);
foundModule:=Module32Next(HandModule,lpModule);
end;
break;
end;
end;
foundProcess := Process32Next(MyHand,lpProcess);
end; HandThread:=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
lpThread.dwSize:=SizeOf(TThreadEntry32);
FoundThread := Thread32First(HandThread,lpThread);
RichEdit1.Lines.Add('');
RichEdit1.Lines.Add('Thread Information:');
RichEdit1.Lines.Add(format(' %8s %8s', ['TID','Priority']));
while FoundThread do
begin
if lpThread.th32OwnerProcessID=MyID then
begin
tmpstr:=Format('%08x',[lpThread.th32OwnerProcessID]);
if tmpstr=tmpid then
iPriority:=lpThread.tpBasePri+lpThread.tpDeltaPri;
if (lpThread.tpBasePri<16) and (iPriority>15) then iPriority:=15;
if (lpThread.tpBasePri>15) and (iPriority>31) then iPriority:=31;
if (lpThread.tpBasePri<16) and (iPriority<1) then iPriority:=1;
if (lpThread.tpBasePri>15) and (iPriority>16) then iPriority:=16;
RichEdit1.Lines.Add(format(' %08x %d',[lpThread.th32OwnerProcessID,iPriority]));
end;
FoundThread := Thread32Next(HandThread,lpThread);
end; CloseHandle(HandThread);
CloseHandle(HandModule);
closeHandle(MyHand);
end;delphi5+win2000通过,看一下window核心编程,好像第二章就是讲这个
ProcEntry.dwSize := Sizeof(ProcEntry);
RetVal := Process32First(hSnapshot,ProcEntry);在 CreateToolhelp32Snapshot后hSnapshot的值为0;在
Process32First(hSnapshot,ProcEntry)后RetVal的值为False;
我的环境是NT4.0,delphi 6