导航:
论坛 -> DELPHI技术
斑竹:liumazi,sephil
作者:
2003/12/21 21:35:08
标题:
急!!! 怎样取得当前运行任一程序的句柄?
浏览:1491
加入我的收藏
楼主:
----------------------------------------------
-
作者:
bios (阿贡)
★☆☆☆☆
-
盒子中级会员
2003/12/21 21:58:48
1楼:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls,PSAPI,tlhelp32; type TForm1 = class(TForm) lvwProcess: TListView; Button1: TButton; lvwthread: TListView; LBItems: TListBox; procedure Button1Click(Sender: TObject); procedure lvwProcessClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; WndList:TStringList; implementation {$R *.dfm} //-----------------------枚举进程对应的窗口------------------------ function EnumChildProc(wnd: HWND; LParam: LPARAM): BOOL; stdcall; var S: string; begin S := Format('%.8x', [wnd]); if wndList.IndexOf(S) < 0 then // wndList is a TStringList instance begin WndList.Add(S); Form1.LBItems.Items.Add(StringOfChar('-', LParam*4) + S); EnumChildWindows(wnd, @EnumChildProc, LParam+1); end; Result := True; end; function EnumTopProc(wnd: HWND; LParam: LPARAM): BOOL; stdcall; var ID: DWORD; begin GetWindowThreadProcessId(wnd, @ID); if ID = LParam then begin Form1.LBItems.Items.Add(Format('%.8x', [wnd])); EnumChildWindows(wnd, @EnumChildProc, 1); end; Result := True; end; //--------------------------------------------------- procedure TForm1.Button1Click(Sender: TObject); var i,j:Longint; hSnapshot,hSnapshot1: THANDLE ; Tmp,Hex:string; ListItem:TListItem; FileName,File1,Path,file1name,file2:String ; pe:PROCESSENTRY32; b,b1:Bool; me:MODULEENTRY32; //------------------ ret:Longint; begin lvwProcess.Items.Clear(); hSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); pe.dwSize:=sizeof(pe); b:=Process32First(hSnapshot,pe); repeat File1:=pe.szExeFile; Tmp:=inttostr(longint(pe.th32ProcessID)); ListItem:= lvwProcess.Items.Add(); ListItem.Caption :=Tmp ; ListItem.SubItems.Add(File1); Tmp:=inttostr(pe.cntThreads); ListItem.SubItems.Add(Tmp); Tmp:=inttostr(longint(pe.th32ProcessID)); Hex:=InttoHex(longint(pe.th32ProcessID),8); ListItem.SubItems.Add(Hex); b:=Process32Next(hSnapshot,pe); until b=false; CloseHandle(hSnapshot); end; procedure TForm1.lvwProcessClick(Sender: TObject); var Handle1, file1Name, file1, Path, Tmp:String ; hProcess :DWORD ; hSnapshot :THANDLE; ListItem:TListItem ; te:tagTHREADENTRY32; b:bool; j:Longint; begin Handle1:=lvwProcess.Selected.Caption ; hProcess:=DWORD( strtoint(Handle1)); LBItems.Clear; WndList.Clear; EnumWindows(@EnumTopProc, hProcess); lvwthread.Items.Clear(); hSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,hProcess); te.dwSize:=sizeof(te); b:=Thread32First(hSnapshot,te); repeat if hProcess=te.th32OwnerProcessID then begin//1 ListItem:= lvwthread.Items.Add(); Tmp:='$'+inttoHex(te.th32ThreadID,8 ); ListItem.Caption :=tmp; // 线程 ID Tmp:='$'+inttoHex(te.tpBasePri ,8); ListItem.SubItems.Add(Tmp); // 线程优先级 end;//1 b:=Thread32Next(hSnapshot,te); until b=false; CloseHandle(hSnapshot); end; procedure TForm1.FormCreate(Sender: TObject); begin WndList:=TStringList.Create ; end; end.
----------------------------------------------
作者:
2003/12/21 22:27:36
2楼:
谢谢bios大哥!!!
----------------------------------------------
-