Delphi编程获取系统当前进程1.doc

上传人:新** 文档编号:562962367 上传时间:2022-09-09 格式:DOC 页数:4 大小:44.01KB
返回 下载 相关 举报
Delphi编程获取系统当前进程1.doc_第1页
第1页 / 共4页
Delphi编程获取系统当前进程1.doc_第2页
第2页 / 共4页
Delphi编程获取系统当前进程1.doc_第3页
第3页 / 共4页
Delphi编程获取系统当前进程1.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《Delphi编程获取系统当前进程1.doc》由会员分享,可在线阅读,更多相关《Delphi编程获取系统当前进程1.doc(4页珍藏版)》请在金锄头文库上搜索。

1、Delphi编程获取系统当前进程、窗口句柄、文件属性以及程序运行状态Delphi编程获取系统当前进程、窗口句柄、文件属性以及程序运行状态.uses TLHelp32,PsAPI;(1)显示进程列表:procedure TForm1.Button2Click(Sender: TObject);var lppe: TProcessEntry32;found : boolean;Hand : THandle;P:DWORD;s:string;beginListBox1.Items.Clear ;Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);fo

2、und := Process32First(Hand,lppe);while found dobegins := StrPas(lppe.szExeFile);if lppe.th32ProcessID0 then p := lppe.th32ProcessIDelse p := 0;ListBox1.Items.AddObject(s,pointer(p);/列出所有进程。found := Process32Next(Hand,lppe);end;end;(2)杀死某进程:procedure TForm1.Button3Click(Sender: TObject);var lppe: TPr

3、ocessEntry32;found : boolean;Hand : THandle;P:DWORD;sExeFile,sSelect:string;killed:boolean;beginp :=DWORD(ListBox1.Items.ObjectsListBox1.itemindex);if P0 thenbeginkilled := TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);if not killed then messagebox(self.handle,pchar(sExeFile+无法杀

4、死!),提示,MB_OK or MB_ICONWARNING)else ListBox1.Items.Delete(ListBox1.ItemIndex);end;end;(3)取得某进程EXE路径:procedure TForm1.Button8Click(Sender: TObject); /uses PSAPI;varh:THandle; fileName:string; iLen:integer; hMod:HMODULE;cbNeeded,p:DWORD;beginp :=DWORD(ListBox1.Items.ObjectsListBox1.itemindex);h := Ope

5、nProcess(PROCESS_ALL_ACCESS, false, p); /p 为 进程IDif h 0 thenbeginif EnumProcessModules( h, hMod, sizeof(hMod), cbNeeded) thenbegin SetLength(fileName, MAX_PATH); iLen := GetModuleFileNameEx(h, hMod, PCHAR(fileName), MAX_PATH); if iLen 0 then begin SetLength(fileName, StrLen(PCHAR(fileName); ShowMess

6、age(fileName); end;end;CloseHandle(h);end;end;(4)取得窗口列表:beginListBox1.Items.Clear ;EnumWindows(EnumWindowsProc, 0);end;(5)杀死窗口进程:procedure TForm1.Button6Click(Sender: TObject);varH:THandle;P:DWORD;s:string;killed:boolean;begins := ListBox1.ItemsListBox1.ItemIndex;H:=FindWindow(nil,pchar(s);if H0 the

7、nbeginGetWindowThreadProcessId(H,P);if P0 then killed:=TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);if not killed then messagebox(self.handle,pchar(s+无法杀死!),提示,MB_OK or MB_ICONWARNING)else ListBox1.Items.Delete(ListBox1.ItemIndex);end;end;(6)取得窗口进程路径:procedure TForm1.Button9Cli

8、ck(Sender: TObject);varH:THandle; P,cbNeeded: DWORD; s,fileName:string;iLen:integer; hMod:HMODULE;begins := ListBox1.ItemsListBox1.ItemIndex;H:=FindWindow(nil,pchar(s);if H0 thenbeginGetWindowThreadProcessId(H,P);if P0 thenbegin h := OpenProcess(PROCESS_ALL_ACCESS, false, p); /p 为 进程ID if h 0 then b

9、egin if EnumProcessModules( h, hMod, sizeof(hMod), cbNeeded) then begin SetLength(fileName, MAX_PATH); iLen := GetModuleFileNameEx(h, hMod, PCHAR(fileName), MAX_PATH); if iLen 0 then begin SetLength(fileName, StrLen(PCHAR(fileName); ShowMessage(fileName); end; end; CloseHandle(h); end;end;end;end;(7

10、)文件属性:procedure TForm1.Button1Click(Sender: TObject);varSR: TSearchRec;V1, V2, V3, V4: integer ;constdtFmt:string = YYYY-MM-DD HH:NN:SS;begin/ = 方法一 = /if FindFirst(sFileName, faAnyFile, SR) = 0 thenbeginEdit1.Text := intToStr(SR.Attr); /文件属性Edit2.Text := intToStr(SR.Size); /文件大小Edit3.Text := Format

11、DateTime(dtFmt,CovFileDate(SR.FindData.ftCreationTime); /创建时间Edit4.Text := FormatDateTime(dtFmt,CovFileDate(SR.FindData.ftLastWriteTime); /最后修改时间Edit5.Text := FormatDateTime(dtFmt,CovFileDate(SR.FindData.ftLastAccessTime); /最后访问时间if SR.Attr and faHidden 0 then FileSetAttr(sFileName, SR.Attr-faHidden

12、);FindClose(SR);end;if GetFileVersion(sFileName,V1, V2, V3, V4) thenEdit7.Text := intToStr(v1)+.+intToStr(v2)+.+intToStr(v3)+.+intToStr(v4);/ = 方法二 = /varAttrs: Word;f: file of Byte; / 文件大小 必须要 定义为 file of byte ,这样才能取出 bytessize: Longint;/文件属性Attrs := FileGetAttr(sFileName);Edit1.Text := intToStr(At

13、trs);/文件大小AssignFile(f, OpenDialog1.FileName);Reset(f);tryAssignFile(f, sFileName);Reset(f);size := FileSize(f);Edit2.Text := intToStr(size);finallyCloseFile(f);end;end;(8)判断程序是否在运行:procedure TForm1.Button5Click(Sender: TObject);var PrevInstHandle:Thandle;AppTitle:pchar;beginAppTitle := pchar(test);PrevInstHandle := FindWindow(nil, AppTitle);if PrevInstHandle 0 then begin if IsIconic(PrevInstHandle) then ShowWindow(PrevInstHandle, SW_RESTORE) else BringWindowToTop(PrevInstHandle); SetForegroundWindow(PrevInstHandle);end;end;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号