uses
windows,SysUtils,PsAPI,TLHelp32;.............function GetProcessID(ProcessName:string):DWORD;
var
lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
P:DWORD;
s:string;
begin
result:=0;
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do
begin
s := string(lppe.szExeFile);
if lppe.th32ProcessID>0 then
p := lppe.th32ProcessID
else
p := 0;
if UpperCase(ExtractFileName(s))=UpperCase(ExtractFileName(ProcessName)) then
begin
result:=p;
break;
end;
found := Process32Next(Hand,lppe);
end;
end;
windows,SysUtils,PsAPI,TLHelp32;.............function GetProcessID(ProcessName:string):DWORD;
var
lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
P:DWORD;
s:string;
begin
result:=0;
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do
begin
s := string(lppe.szExeFile);
if lppe.th32ProcessID>0 then
p := lppe.th32ProcessID
else
p := 0;
if UpperCase(ExtractFileName(s))=UpperCase(ExtractFileName(ProcessName)) then
begin
result:=p;
break;
end;
found := Process32Next(Hand,lppe);
end;
end;
进程数据是一个链表的数据结构
用Process32First和Process32Next循环操作就可以对这个链表进行查找定位操作,
查找中会返回一个TProcessEntry32的数据
这个数据里面就详细描述了进程的信息,包括路径(就是szExeFile),比对路径就可以找到。
出现这个一般是因为你没有USE相关单元主要就是这个
lppe: TProcessEntry32
LPPE里包含进程的N多个信息
看这
uses
windows,SysUtils,PsAPI,TLHelp32;
这种问题,
百度输入关键字:进程ID 程序名 delphi 仔细看就能找到了
是指里面所有字段吗?TLHelp32里面定义好了:
tagPROCESSENTRY32 = packed record
dwSize: DWORD;
cntUsage: DWORD;
th32ProcessID: DWORD; // this process
th32DefaultHeapID: DWORD;
th32ModuleID: DWORD; // associated exe
cntThreads: DWORD;
th32ParentProcessID: DWORD; // this process's parent process
pcPriClassBase: Longint; // Base priority of process's threads
dwFlags: DWORD;
szExeFile: array[0..MAX_PATH - 1] of Char;// Path
end;...........
用这个看一下
showmessage
(
Format('Lppe.dwSize:%d'+#10#13+
'Lppe.cntUsage:%d'+#10#13+
'Lppe.th32ProcessID:%d'+#10#13+
'Lppe.th32DefaultHeapID:%d'+#10#13+
'Lppe.th32ModuleID:%d'+#10#13+
'Lppe.cntThreads:%d'+#10#13+
'Lppe.th32ParentProcessID:%d'+#10#13+
'Lppe.pcPriClassBase:%d'+#10#13+
'Lppe.dwFlags:%d'+#10#13+
'Lppe.szExeFile:'+string(Lppe.szExeFile),
[
Lppe.dwSize,
Lppe.cntUsage,
Lppe.th32ProcessID,
Lppe.th32DefaultHeapID,
Lppe.th32ModuleID,
Lppe.cntThreads,
Lppe.th32ParentProcessID,
Lppe.pcPriClassBase,
Lppe.dwFlags
])
)
能把Lppe里面的字段找出来但是我添加进去了 不知道怎么用.
我先把那个贴子的100分结了
我贴出来给你看 如下---unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,PsAPI,TLHelp32, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function GetProcessID(ProcessName:string):DWORD;
var
lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
P:DWORD;
s:string;
begin
result:=0;
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do
begin
s := string(lppe.szExeFile);
if lppe.th32ProcessID>0 then
p := lppe.th32ProcessID
else
p := 0;
if UpperCase(ExtractFileName(s))=UpperCase(ExtractFileName(ProcessName)) then
begin
result:=p;
break;
end;
found := Process32Next(Hand,lppe);
end;
showmessage
(
Format('Lppe.dwSize:%d'+#10#13+
'Lppe.cntUsage:%d'+#10#13+
'Lppe.th32ProcessID:%d'+#10#13+
'Lppe.th32DefaultHeapID:%d'+#10#13+
'Lppe.th32ModuleID:%d'+#10#13+
'Lppe.cntThreads:%d'+#10#13+
'Lppe.th32ParentProcessID:%d'+#10#13+
'Lppe.pcPriClassBase:%d'+#10#13+
'Lppe.dwFlags:%d'+#10#13+
'Lppe.szExeFile:'+string(Lppe.szExeFile),
[
Lppe.dwSize,
Lppe.cntUsage,
Lppe.th32ProcessID,
Lppe.th32DefaultHeapID,
Lppe.th32ModuleID,
Lppe.cntThreads,
Lppe.th32ParentProcessID,
Lppe.pcPriClassBase,
Lppe.dwFlags
])
)end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,PsAPI,TLHelp32, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;function GetProcessID(ProcessName:string):TProcessEntry32;implementation{$R *.dfm}function GetProcessID(ProcessName:string):TProcessEntry32;
var
lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
P:DWORD;
s:string;
begin
result:=0;
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do
begin
s := string(lppe.szExeFile);
if lppe.th32ProcessID>0 then
p := lppe.th32ProcessID
else
p := 0;
if UpperCase(ExtractFileName(s))=UpperCase(ExtractFileName(ProcessName)) then
begin
GetProcessID:=lppe;
break;
end;
found := Process32Next(Hand,lppe);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var Retlppe:TProcessEntry32;
begin
Retlppe := GetProcessID('1.exe', Retlppe);//传入'1.exe', 这样就能查找'1.exe'进程IDshowmessage
(
Format('Retlppe.dwSize:%d'+#10#13+
'Retlppe.cntUsage:%d'+#10#13+
'Retlppe.th32ProcessID:%d'+#10#13+//这个就是进程ID
'Retlppe.th32DefaultHeapID:%d'+#10#13+
'Retlppe.th32ModuleID:%d'+#10#13+
'Retlppe.cntThreads:%d'+#10#13+
'Retlppe.th32ParentProcessID:%d'+#10#13+
'Retlppe.pcPriClassBase:%d'+#10#13+
'Retlppe.dwFlags:%d'+#10#13+
'Retlppe.szExeFile:'+string(Retlppe.szExeFile),
[
Retlppe.dwSize,
Retlppe.cntUsage,
Retlppe.th32ProcessID,
Retlppe.th32DefaultHeapID,
Retlppe.th32ModuleID,
Retlppe.cntThreads,
Retlppe.th32ParentProcessID,
Retlppe.pcPriClassBase,
Retlppe.dwFlags
])
)
end;end.
function GetProcessID(ProcessName:string):TProcessEntry32;
var
lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
P:DWORD;
s:string;
begin
FillChar(result,SizeOf(TProcessEntry32),0);
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found := Process32First(Hand,lppe);
while found do
begin
s := string(lppe.szExeFile);
if lppe.th32ProcessID>0 then
p := lppe.th32ProcessID
else
p := 0;
if UpperCase(ExtractFileName(s))=UpperCase(ExtractFileName(ProcessName)) then
begin
GetProcessID:=lppe;
break;
end;
found := Process32Next(Hand,lppe);
end;
end;
如图
这个是为什么?
不是已经VAR了吗
改成:
Retlppe := GetProcessID('1.exe');//传入'1.exe', 这样就能查找'1.exe'进程ID,ctrl+v用多了,老是会弄错,,,--!
这一段改成这样就可以了:
FillChar(result,SizeOf(TProcessEntry32),0);
Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
lppe.dwSize := Sizeof(lppe);
found := Process32First(Hand,lppe);
while found do