Delphi 獲取系統當前進程、窗口句柄、文件屬性以及程序運行狀態



uses
TLHelp32,PsAPI;
(1)顯示進程列表:

 1 procedure TForm1.Button2Click(Sender: TObject);
 2 var 
 3 lppe: TProcessEntry32;
 4 found : boolean;
 5 Hand : THandle;
 6 P:DWORD;
 7 s:string;
 8 begin
 9 ListBox1.Items.Clear ;
10 Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
11 found := Process32First(Hand,lppe);
12 while found do
13 begin
14 s := StrPas(lppe.szExeFile);
15 if lppe.th32ProcessID>0 then
16 p := lppe.th32ProcessID
17 else
18 p := 0;
19 ListBox1.Items.AddObject(s,pointer(p));//列出所有進程。
20 found := Process32Next(Hand,lppe);
21 end;
22 end;
View Code

(2)殺死某進程:

 1 procedure TForm1.Button3Click(Sender: TObject);
 2 var 
 3 lppe: TProcessEntry32;
 4 found : boolean;
 5 Hand : THandle;
 6 P:DWORD;
 7 sExeFile,sSelect:string;
 8 killed:boolean;
 9 begin
10 p :=DWORD(ListBox1.Items.Objects[ListBox1.itemindex]);
11 if P<>0 then
12 begin
13 killed := TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);
14 if not killed then
15 messagebox(self.handle,pchar(sExeFile+'無法殺死!'),'提示',MB_OK or MB_ICONWARNING)
16 else
17 ListBox1.Items.Delete(ListBox1.ItemIndex);
18 end;
19 end;
View Code

(3)取得某進程EXE路徑:

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 為 進程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;
View Code

(4)取得窗口列表:

begin
ListBox1.Items.Clear ;
EnumWindows(@EnumWindowsProc, 0);
end;
View Code

(5)殺死窗口進程:

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+'無法殺死!'),'提示',MB_OK or MB_ICONWARNING)
else
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
end;
View Code

(6)取得窗口進程路徑:

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 為 進程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;
View Code

(7)文件屬性:

procedure TForm1.Button1Click(Sender: TObject);
var
SR: TSearchRec;
V1, V2, V3, V4: integer ;
const
dtFmt:string = 'YYYY-MM-DD HH:NN:SS';
begin
// ============== 方法一 ==================== //
if FindFirst(sFileName, faAnyFile, SR) = 0 then
begin
Edit1.Text := intToStr(SR.Attr); //文件屬性
Edit2.Text := intToStr(SR.Size); //文件大小
Edit3.Text := FormatDateTime(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);
FindClose(SR);
end;
if GetFileVersion(sFileName,V1, V2, V3, V4) then
Edit7.Text := intToStr(v1)+'.'+intToStr(v2)+'.'+intToStr(v3)+'.'+intToStr(v4);
// ============== 方法二 ==================== //
{
var
Attrs: Word;
f: file of Byte; // 文件大小 必須要 定義為" file of byte" ,這樣才能取出 bytes
size: Longint;
//begin
//文件屬性
Attrs := FileGetAttr(sFileName);
Edit1.Text := intToStr(Attrs);
//文件大小
AssignFile(f, OpenDialog1.FileName);
Reset(f);
try
AssignFile(f, sFileName);
Reset(f);
size := FileSize(f);
Edit2.Text := intToStr(size);
finally
CloseFile(f);
end;
}
end;
View Code

(8)判斷程序是否在運行:

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;
View Code

獲取進程列表

ListView1.Items.BeginUpdate;
  ListView1.Items.Clear;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  // CreateToolhelp32Snapshot函數得到進程快照
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32); // 初始化
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  // Process32First 得到一個系統快照里第一個進程的信息
  Summ := 0;
  while ContinueLoop do
  begin
    Summ := Summ + 1;
    NewItem := ListView1.Items.Add; // 在ListView1顯示
    NewItem.ImageIndex := -1;
    NewItem.Caption := ExtractFileName(FProcessEntry32.szExeFile); // 進程名稱
    // ??NewItem.Caption := ExtractFilePath(FProcessEntry32.szExeFile);//進程名稱
    NewItem.subItems.Add(FormatFloat('00', Summ)); // 序號
    NewItem.subItems.Add(IntToStr(FProcessEntry32.th32ProcessID)); // 進程ID
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
  ListView1.Items.EndUpdate;
  Label1.Caption := '進程數:' + IntToStr(ListView1.Items.count);
  ShowMessage(GetPathFileofModule('explorer.exe'));
  ShowMessage(GetProcessPath(7172));
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM