Process, window handle, file attributes, program running status

Source: Internet
Author: User

Uses tlhelp32, psapi;

(1) display the process list:
Procedure tform1.button2click (Sender: tobject );
VaR lppe: tprocessentry32;
Found: Boolean;
Hand: thandle;
P: DWORD;
S: string;
Begin
Listbox1.items. Clear;
Hand: = createconlhelp32snapshot (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;
End;

(2) Killing 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 + 'cannot be killed! '),' Hint ', mb_ OK or mb_iconwarning)
Else
Listbox1.items. Delete (listbox1.itemindex );
End;
End;

(3) obtain the EXE path of a process:
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 indicates the 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;

(4) retrieve the window list
Begin
Listbox1.items. Clear;
Enumwindows (@ enumwindowsproc, 0 );
End;

(5) Killing 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 then
Begin
Getwindowthreadprocessid (H, @ P );
If P <> 0 then
Killed: = terminateprocess (OpenProcess (process_terminate, false, P), $ ffffffff );
If not killed then
MessageBox (self. Handle, pchar (S + 'cannot be killed! '),' Hint ', mb_ OK or mb_iconwarning)
Else
Listbox1.items. Delete (listbox1.itemindex );
End;
End;

(6) obtain the process path of the window:
Procedure tform1.button9click (Sender: tobject );
VaR
H: thandle; 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 indicates the 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 attributes:
Procedure tform1.button1click (Sender: tobject );
VaR
SR: tsearchrec;
V1, V2, V3, V4: integer;
Const
Dtfmt: String = 'yyyy-MM-DD hh: NN: ss ';
Begin
// ===================== Method 1 ========================================== //
If findfirst (sfilename, faanyfile, Sr) = 0 then
Begin
Edit1.text: = inttostr (Sr. ATTR); // file attributes
Edit2.text: = inttostr (Sr. size); // File Size
Edit3.text: = formatdatetime (dtfmt, covfiledate (Sr. finddata. ftcreationtime); // Creation Time
Edit4.text: = formatdatetime (dtfmt, covfiledate (Sr. finddata. ftlastwritetime); // last modification time
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 );
End;

If getfileversion (sfilename, V1, V2, V3, V4) then
Edit7.text: = inttostr (V1) + '.' + inttostr (V2) + '.' + inttostr (V3) + '.' + inttostr (V4 );

// ===================== Method 2 ========================================== //
{
VaR
Attrs: word;
F: file of byte; // the file size must be defined as "file of Byte ".
Size: longint;

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

Edit1.text: = inttostr (attrs );

// File Size
Assignfile (F, opendialog1.filename );
Reset (f );
Try
Assignfile (F, sfilename );
Reset (f );
Size: = filesize (f );
Edit2.text: = inttostr (size );
Finally
Closefile (f );
End;
}
End;

(8) Determine whether the 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 begin
If isiconic (previnsthandle) then
Showwindow (previnsthandle, sw_restore)
Else
Bringwindowtotop (previnsthandle );
Setforegroundwindow (previnsthandle );
End;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.