⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 在delphi中实现windows核心编程.原书光盘代码核心编程.原书光盘代码
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, clipbrd, tlhelp32;
type

  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    ComboBox1: TComboBox;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FSnapshotHandle: THandle;
    function GetProcessID(var List: TStringList; FileName: string = ''): TProcessEntry32;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  FProcessEntry32: TProcessEntry32;
  ProcessID: DWord;
  ThreadHandle: THandle;
  ThreadStruct: TThreadEntry32;
  List: TStringList;
begin
  Listbox1.items.clear;
  if Combobox1.itemindex = -1 then exit;
  List := TStringList.Create;
  FProcessEntry32 := GetProcessID(List, Combobox1.text);
  ProcessID := FProcessEntry32.th32ProcessID;
  ThreadHandle := CreateToolHelp32Snapshot(TH32CS_SnapThread, Processid);
  try
    ThreadStruct.dwSize := sizeOf(TThreadEntry32);
    if Thread32First(ThreadHandle, ThreadStruct) then
      repeat
        if ThreadStruct.th32OwnerProcessID = ProcessID then
          Listbox1.Items.add(IntTostr(ThreadStruct.th32ThreadID));
      until not Thread32Next(ThreadHandle, ThreadStruct);
  finally
    CloseHandle(ThreadHandle)
  end;
  Label1.Caption := '线程数:' + IntTostr(Listbox1.Items.count);
end;

function TForm1.GetProcessID(var List: TStringList; FileName: string = ''): TProcessEntry32;
var
  Ret: BOOL;
  ProcessID: integer;
  s: string;
  FProcessEntry32: TProcessEntry32;
begin
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
  Ret := Process32First(FSnapshotHandle, FProcessEntry32);
  while Ret do
  begin
    s := ExtractFileName(FProcessEntry32.szExeFile);
    if (FileName = '') then
    begin
      List.Add(Pchar(s));
    end else if (AnsicompareText(Trim(s),Trim(FileName))=0) and (FileName <> '') then
    begin
      ProcessID := FProcessEntry32.th32ProcessID;
      List.Add(Pchar(s));
      result := FProcessEntry32;
      break;
    end;
    Ret := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;


procedure TForm1.FormCreate(Sender: TObject);
var
  List: TStringList;
  i: integer;
begin
  Combobox1.clear;
  List := TStringList.Create;
  GetProcessID(List);
  for i := 0 to List.Count - 1 do
  begin
    Combobox1.items.add(Trim(List.strings[i]));
  end;
  List.Free;
  Combobox1.itemindex := 0;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -