Delphi与进程、窗口句柄、文件属性、程序运行状态

来源:互联网 发布:cn域名可以不备案吗 编辑:程序博客网 时间:2024/06/06 07:50
 

uses TLHelp32,PsAPI;

Delphi显示进程列表

procedure TForm1.Button2Click(Sender: TObject);var lppe: TProcessEntry32;  found : boolean;  Hand : THandle;  P:DWORD;  s:string;beginListBox1.Items.Clear ;Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);found := Process32First(Hand,lppe);while found dobegin  s := StrPas(lppe.szExeFile);  if lppe.th32ProcessID>0 then    p := lppe.th32ProcessID  else    p := 0;  ListBox1.Items.AddObject(s,pointer(p));//列出所有进程。  found := Process32Next(Hand,lppe);end;end;


Delphi杀死某进程

procedure TForm1.Button3Click(Sender: TObject);var lppe: TProcessEntry32;  found : boolean;  Hand : THandle;  P:DWORD;  sExeFile,sSelect:string;  killed:boolean;beginp :=DWORD(ListBox1.Items.Objects[ListBox1.itemindex]);if P<>0 thenbegin  killed := TerminateProcess(OpenProcess(PROCESS_TERMINATE,False,P),$FFFFFFFF);  if not killed then    messagebox(self.handle,pchar(sExeFile+'无法杀死!'),'提示',MB_OK or MB_ICONWARNING)  else    ListBox1.Items.Delete(ListBox1.ItemIndex);end;end;

Delphi取得某进程EXE路径:

procedure TForm1.Button8Click(Sender: TObject); //uses PSAPI;varh:THandle; fileName:string; iLen:integer; hMod:HMODULE;cbNeeded,p:DWORD;beginp :=DWORD(ListBox1.Items.Objects[ListBox1.itemindex]);h := OpenProcess(PROCESS_ALL_ACCESS, false, p);     //p 为 进程IDif h > 0 thenbegin  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;

Delphi取得窗口列表

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

Delphi杀死窗口进程

procedure TForm1.Button6Click(Sender: TObject);varH:THandle;P:DWORD;s:string;killed:boolean;begins := ListBox1.Items[ListBox1.ItemIndex];H:=FindWindow(nil,pchar(s));if H<>0 thenbegin  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;

Delphi取得窗口进程路径

procedure TForm1.Button9Click(Sender: TObject);varH:THandle; P,cbNeeded: DWORD; s,fileName:string;iLen:integer;   hMod:HMODULE;begins := ListBox1.Items[ListBox1.ItemIndex];H:=FindWindow(nil,pchar(s));if H<>0 thenbegin  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;

Delphi获取文件属性

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 thenbegin  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);// ============== 方法二 ==================== //{varAttrs: Word;f: file of Byte;   // 文件大小 必须要 定义为" file of byte" ,这样才能取出 bytessize: Longint;//文件属性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;

Delphi判断程序是否在运行

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;