uses
  TLHelp32;
var
  lppe: TProcessEntry32;
  found : boolean;
  Hand : THandle;
begin
  Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
  lppe.dwSize:=sizeof(PROCESSENTRY32);
  found := Process32First(Hand,lppe);
  while found do
  begin
    if StrPas(lppe.szExeFile) = 'QQ.exe' then //查找QQ进程。
    begin
      .....................
    end;
    found := Process32Next(Hand,lppe);
  end;
end;因为不同QQ号运行时在任务管理器的任务名不同(是其QQ号),我就是想得到这个名字,如果做???

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ComCtrls,PSAPI,tlhelp32;type
      TForm1 = class(TForm)
        lvwProcess: TListView;
        Button1: TButton;
        lvwthread: TListView;
        LBItems: TListBox;
        procedure Button1Click(Sender: TObject);
        procedure lvwProcessClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      WndList:TStringList;
    implementation{$R *.dfm}
    //-----------------------枚举进程对应的窗口------------------------
    function EnumChildProc(wnd: HWND; LParam: LPARAM): BOOL; stdcall;
    var
      S: string;
    begin
      S := Format('%.8x', [wnd]);
      if wndList.IndexOf(S) < 0 then // wndList is a TStringList instance
      begin
        WndList.Add(S);
        Form1.LBItems.Items.Add(StringOfChar('-', LParam*4) + S);
        EnumChildWindows(wnd, @EnumChildProc, LParam+1);
      end;
      Result := True;
    end;function EnumTopProc(wnd: HWND; LParam: LPARAM): BOOL; stdcall;
    var
      ID: DWORD;
    begin  GetWindowThreadProcessId(wnd, @ID);
      if ID = LParam then
      begin
        Form1.LBItems.Items.Add(Format('%.8x', [wnd]));
        EnumChildWindows(wnd, @EnumChildProc, 1);
      end;
      Result := True;
    end;
    //---------------------------------------------------
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i,j:Longint;
      hSnapshot,hSnapshot1: THANDLE ;
      Tmp,Hex:string;
      ListItem:TListItem;
      FileName,File1,Path,file1name,file2:String ;
      pe:PROCESSENTRY32;
      b,b1:Bool;
      me:MODULEENTRY32;
      //------------------
      ret:Longint;begin
        lvwProcess.Items.Clear();
        hSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
        pe.dwSize:=sizeof(pe);
        b:=Process32First(hSnapshot,pe);
        repeat
            File1:=pe.szExeFile;
            Tmp:=inttostr(longint(pe.th32ProcessID));
                ListItem:= lvwProcess.Items.Add();            ListItem.Caption :=Tmp ;
                ListItem.SubItems.Add(File1);            Tmp:=inttostr(pe.cntThreads);
                ListItem.SubItems.Add(Tmp);            Tmp:=inttostr(longint(pe.th32ProcessID));
                Hex:=InttoHex(longint(pe.th32ProcessID),8);
                ListItem.SubItems.Add(Hex);    b:=Process32Next(hSnapshot,pe);
        until b=false;
        CloseHandle(hSnapshot);
    end;procedure TForm1.lvwProcessClick(Sender: TObject);
    var
      Handle1,
      file1Name,
      file1,
      Path,
      Tmp:String ;
      hProcess :DWORD ;
      hSnapshot :THANDLE;
      ListItem:TListItem ;
      te:tagTHREADENTRY32;
      b:bool;
      j:Longint;
    begin
        Handle1:=lvwProcess.Selected.Caption ;
        hProcess:=DWORD( strtoint(Handle1));    LBItems.Clear;
       WndList.Clear;
       EnumWindows(@EnumTopProc, hProcess);     lvwthread.Items.Clear();
         hSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,hProcess);
        te.dwSize:=sizeof(te);
        b:=Thread32First(hSnapshot,te);
        repeat
           if hProcess=te.th32OwnerProcessID then
           begin//1
            ListItem:= lvwthread.Items.Add();
            Tmp:='$'+inttoHex(te.th32ThreadID,8 );
            ListItem.Caption :=tmp;       // 线程 ID
            Tmp:='$'+inttoHex(te.tpBasePri ,8);
            ListItem.SubItems.Add(Tmp);       // 线程优先级
           end;//1
            b:=Thread32Next(hSnapshot,te);
          
        until b=false;
        CloseHandle(hSnapshot);
     
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
    WndList:=TStringList.Create ;
    end;end.