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);
2、found := Process32First(Hand,lppe);while found dobegins := StrPas(lppe.szExeFile);if lppe.th32ProcessID0 thenp := lppe.th32ProcessIDelsep := 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 P 0 thenbeginif EnumProcessModules( h, hMod, sizeof(hMod), cbNeeded) thenbeginSetLength(fileName, MAX_PATH);iLen := GetModuleFileNameEx(h, hMod, PCHA
4、R(fileName), MAX_PATH);if iLen 0 thenbeginGetWindowThreadProcessId(H,P);if P0 thenbeginGetWindowThreadProcessId(H,P);if P 0 thenbeginif EnumProcessModules( h, hMod, sizeof(hMod), cbNeeded) thenbeginSetLength(fileName, MAX_PATH);iLen := GetModuleFileNameEx(h, hMod, PCHAR(fileName), MAX_PATH);if iLen
5、0 thenFileSetAttr(sFileName, SR.Attr-faHidden);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 := F
6、ileGetAttr(sFileName);Edit1.Text := intToStr(Attrs);/文件大小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 beginif IsIconic(PrevInstHandle) thenShowWindow(PrevInstHandle, SW_RESTORE)elseBringWindowToTop(PrevInstHandle);SetForegroundWindow(PrevInstHandle);end;end;