Delphi process, window handle, file properties, program running status

xiaoxiao2021-03-06  63

Uses Tlhelp32, PSAPI;

(1) Display a list of process: procedure TForm1.Button2Click (Sender: TObject); var lppe: TProcessEntry32; found: boolean; Hand: THandle; P: DWORD; s: string; begin ListBox1.Items.Clear; Hand: = CreateToolhelp32Snapshot ( TH32CS_SNAPALL, 0); found: = Process32First (Hand, lppe); while found do begin s: = StrPas (lppe.szExeFile); if lppe.th32ProcessID> 0 then p: = lppe.th32ProcessID else p: = 0; ListBox1. Items.addObject (s, Pointer (P)); // list all processes. Found: = process32next (hand, lppe); end;

(2) Kill a process: procedure tform1.button3Click (sender: Tobject); var lppe: tprocessentry32; Found: boolean; hand: thandle; p: dword; sexefile, sselect: string; killed: boolean; begin p: = DWORD (ListBox1.Items.Objects [ListBox1.itemindex]); if P <> 0 then begin killed: = TerminateProcess (OpenProcess (PROCESS_TERMINATE, False, P), $ FFFFFFFF); if not killed then messagebox (self.handle, pchar ( Sexefile 'can't kill!'), 'prompt', MB_OK or MB_ICONWARNING ELSE ListBox1.Items.delete (ListBox1.ItemIndex); end;

(3) Get a process EXE path: Procedure TForm1.Button8Click (Sender: Tobject); // Uses psapi; var H: Thandle; FileName: String; Ilen: Integer; hmod: hmodule; cbneeded, p: dWord; begin P: = DWORD (ListBox1.Items.Objects [listbox1.itemindex]); h: = openprocess (process_all_access, false, p); // p For Process ID IF H> 0 Then Begin if EnumprocessModules (h, @HMOD, SIZEOF (hmod ), Cbneeded); Ilen: = getModuleFileNameex (HMOD, PCHAR (FileName), Max_Path); if Ilen <> 0 THEN BEGIN SETLENGTH (FileName, Strlen)); ShowMessage (filename); end; end; closehandle (h); end; end; (4) obtain window list begin listbox1.items.clear; enumwindows (@EnumWindowsProc, 0); end;

(5) Kill window processes procedure tform1.button6click (Sender: TOBJECT); VAR H: THANDLE; P: DWORD; S: String; killed: boolean; begin s: = listbox1.items [listbox1.itemindex]; h: = Findwindow (NIL, PCHAR (S)); if h <> 0 The begin getWindowThreadProcessid (h, @p); if P <> 0 THEN KILED: = TerminateProcess (OpenProcess (Process_Terminate, False, P), $ ffffff); if NOT KILLED THEN MessageBox (self.handle, pchar (s 'can't kill!'), 'prompt', MB_OK or MB_ICONWARNING) else listbox1.items.delete (listbox1.itemindex); end;

(6) Take the window process path: Procedure TFORM1.BUTTON9Click (Sender: TOBJECT); VAR H: THANDE; P, CBNEEDED: DWORD; S, FileName: String; Ilen: Integer; hmod: hmodule; begin s: = listbox1.items [ListBox1.itemindex]; H: = FindWindow (NIL, PCHAR (S));

IF H <> 0 Then Begin getWindowThreadProcessId (h, @p); if P <> 0 Then Begin H: = openprocess (process_all_access, false, p); // p For Process ID IF H> 0 THEN BEGIN 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) file properties: procedure tForm1.button1click (sender: Tobject); var: tsearchrec; V1, V2, V3, V4: Inteder; const DTFMT: String = 'yyyy-mm-dd hh: nn: ss'; begin // =============== Method 1 === ================= // if FindFirst (SFileName, FAANYFILE, SR) = 0 THEN BEGIN Edit1.Text: = INTOSTR (sr.attr); // File Attribute Edit2. Text: = INTOSTR (sr.size); // File size Edit3.Text: = FormatDateTime (dtFmt, CovFileDate (SR.FindData.ftCreationTime)); // Created Edit4.Text: = FormatDateTime (dtFmt, CovFileDate (SR.FindData.ftLastWriteTime)); // last modified Edit5.Text: = FormatDateTime (dtFmt , CoVfileDate (sr.finddata.ftlastaccesstime)); // Last access time

IF sr.attr And Fahidden <> 0 THEN FileSetattr (SFileName, Sr.attr-Fahidden);

FindClose (SR);

IF getFileVersion (SFileName, V1, V2, V3, V4) Then Edit7.text: = INTOSTR (V1) '.' INTOSTR (V3) '.' INTOSTR (V4) ; // ============== Method 2 ============================= // {var attrs: word; f: File Of Byte; // The file size must be defined as "file of byte", so you can take it out bytes size: longint;

// File Attribute Attrs: = filegetattr (sfilename);

Edit1.Text: = INTSTR (Attrs);

// File size assignfile (f, opendialog1.filename); reset (f); try assignfile (f, sfilename); reset (f); size: = filesis (f); edit2.text: = INTOSTOSTR (Size); Finally Closefile (f); end;

(8) The judgment program is running: procedure tform1.button5click (sender: TOBJECT); VAR Previnsthandle: Thandle; AppTitle: Pchar; Begin AppTitle: = PCHAR ('TEST'); Previnsthandle: = FindWindow (nil, apptitle); if Previnsthandle <> 0 THEN BEGINSTHANDE (previnsthandle, sw_restore) else bringwindowtotop (previnsthandle); setForegroundWindow (previnsthandle); end;

转载请注明原文地址:https://www.9cbs.com/read-119059.html

New Post(0)