zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    stenciljs 学习四 组件装饰器
    stenciljs 学习三 组件生命周期
    stenciljs 学习二 pwa 简单应用开发
    stenciljs ionic 团队开发的方便web 组件框架
    stenciljs 学习一 web 组件开发
    使用npm init快速创建web 应用
    adnanh webhook 框架 hook rule
    adnanh webhook 框架 hook 定义
    adnanh webhook 框架request values 说明
    adnanh webhook 框架execute-command 以及参数传递处理
  • 原文地址:https://www.cnblogs.com/blogpro/p/11343990.html
Copyright © 2011-2022 走看看