通过Delphi获得当前所有进程名,进程PID
代码如下
// 获取系统当前进程名和进程ID,
//注:应引用"TLHelp32"单元"use TLHelp32"。
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls,TLHelp32;
type
TForm2 = class(TForm)
ListBox1: TListBox;
ListView1: TListView;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
ContinueLoop:BOOL; //是否继续循环
FSnapshotHandle:THandle; //进程快照句柄
FProcessEntry32:TProcessEntry32; //进程入口的结构体信息
implementation
{$R dfm}
procedure TForm2Button1Click(Sender: TObject);
//在listview中显示进程
var
NewItem: TListItem;
Summ:integer;
begin
ListView1ItemsBeginUpdate;
ListView1ItemsClear;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
//CreateToolhelp32Snapshot函数得到进程快照
FProcessEntry32dwSize := Sizeof(FProcessEntry32); //初始化
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
//Process32First 得到一个系统快照里第一个进程的信息
Summ := 0;
while ContinueLoop do
begin
Summ := Summ + 1;
NewItem := ListView1ItemsAdd; //在ListView1显示
NewItemImageIndex := -1;
NewItemCaption := ExtractFileName(FProcessEntry32szExeFile); //进程名称
//NewItemCaption := ExtractFilePath(FProcessEntry32szExeFile);//进程名称
NewItemsubItemsAdd(FormatFloat('00', Summ));//序号
NewItemsubItemsAdd(IntToStr(FProcessEntry32th32ProcessID));//进程ID
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
ListView1ItemsEndUpdate;
Label1Caption:='进程数:'+inttostr(ListView1ItemsCount);
end;
procedure TForm2Button2Click(Sender: TObject);
//在listbox中显示进程 id
var
ProcessName : string; //进程名
ProcessID : integer; //进程表示符
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //创建一个进程快照
FProcessEntry32dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); //得到系统中第一个进程
//循环例举
while ContinueLoop do
begin
ProcessName := FProcessEntry32szExeFile;
ProcessID := FProcessEntry32th32ProcessID;
Listbox1Itemsadd('应用程序名 :'+ProcessName +'#进程ID:'+ inttostr(ProcessID));
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
procedure TForm2FormCreate(Sender: TObject);
begin
//对ListView1的初始化
ListView1ViewStyle:=vsReport; //设置ListView的显示方式 不设置这种方式不显示列头
ListView1ColumnsAdd; //添加第一列
ListView1Column[0]Caption:='进程名';
ListView1Column[0]AutoSize:=false;
ListView1Column[0]Width:=100;
ListView1Column[0]Alignment:=taLeftJustify;//左对齐
ListView1ColumnsAdd; //添加第二列
ListView1Column[ListView1ColumnsCount-1]Caption:='序号';
ListView1Column[ListView1ColumnsCount-1]AutoSize:=true;
ListView1Column[ListView1ColumnsCount-1]Alignment:=taLeftJustify;//左对齐
ListView1ColumnsAdd; //添加第三列
ListView1Column[ListView1ColumnsCount-1]Caption:='ID';
ListView1Column[ListView1ColumnsCount-1]AutoSize:=true;
ListView1Column[ListView1ColumnsCount-1]Alignment:=taLeftJustify;//左对齐
end;
end
首先你要知道 你要找的是进程的句柄。句柄是 *** 作系统 *** 作的对象,任何一个对象比如一个textbox 也有他的句柄,不一定只要窗口才有句柄。现在你要找到进程的句柄,貌似用C#的话必须要用API 可以用Delphi 试试 这样比较方便 比如 FindWindow 我没有尝试用 这个函数来找 隐藏窗体的句柄 但是 一般 找窗体都没问题的。
HWND hwnd;
CWnd Wnd = FindWindow(NULL, L"1exe");
hwnd = Wnd->GetSafeHwnd();
试一下
这段代码你看下 希望对你有帮助 Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String 1024
End Type
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'查找进程的函数
Private Function fun_FindProcess(ByVal ProcessName As String) As Long
Dim strdata As String
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim mName As String
Dim i As Integer, pid As Long
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
mydwSize = 1060
If (Process32First(l, my)) Then
Do
i = InStr(1, myszExeFile, Chr(0))
mName = LCase(Left(myszExeFile, i - 1))
If mName = LCase(ProcessName) Then
pid = myth32ProcessID
fun_FindProcess = pid
Exit Function
End If
Loop Until (Process32Next(l, my) < 1)
End If
l1 = CloseHandle(l)
End If
fun_FindProcess = 0
End Function
Private Sub Form_Load()
if fun_FindProcess("123exe") <> 0 then
Text1Text="123exe程序已启动"
Else
Text1Text = ""
End If
End Sub
用C++很繁琐。其实直接用DOS命令wmic process ,自己再稍做点工作就可以了,清楚,易懂。
char str[]="wmic process where name=\"notepadexe\" get handle,processid,name";
system(str);
上面命令,列出名字叫 notepadexe 的 所有的 进程 的 handle, processid, name
若用:
wmic process where name=\"notepadexe\" get handle,processid,name > atxt
就能把屏幕输出存入 atxt, 写程序打开,读入,很简单。handle 数值 就是句柄。
用NtQueryInformationProcess可以查询单个进程的句柄数 但是要打开进程
用NtQuerySystemInformation可以获取所有进程的统计信息,而且没有任何权限要求
这个用一个api就可以解决,你用pdh涉及到的更多
以上就是关于delphi7 已知进程PID,怎样通过PID获取进程窗体的句柄,全部的内容,包括:delphi7 已知进程PID,怎样通过PID获取进程窗体的句柄,、C# 根据进程ID获取进程主窗口句柄、C++ 如何用进程名 取 句柄 求教 如取 1.exe等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)