how to get processId of a process

Hi I know this is simple but anyway.
How do i get process Id of for example EXPLORER.exe
I know how to get process id through GetWindowThreadProcessId.
but i want ID of process that i only know processname of. (EXPLORER.EXE)
ssivagnaAsked:
Who is Participating?
 
TheRealLokiSenior DeveloperCommented:
OK, here is a complete example. putting the process id(s) of all "explorer.exe" into a memobox
You're really making me work for those points ;)

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Edit1: TEdit;
    eFileName: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    function ListProcessIdsForFile(ExeFileName: string): integer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

IMPLEMENTATION

{$R *.DFM}

uses tlhelp32;
// note - this will not work on NT4

function TForm1.ListProcessIdsForFile(ExeFileName: string): integer;
    const
        PROCESS_TERMINATE=$0001;
    var
        ContinueLoop: BOOL;
        FSnapshotHandle: THandle;
        FProcessEntry32: TProcessEntry32;
    begin
        result := 0;
        FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        try
            FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
            ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
            while integer(ContinueLoop) <> 0 do
            begin
                if (StrIComp(PChar(ExtractFileName(FProcessEntry32.szExeFile)), PChar(ExeFileName)) = 0)
                or (StrIComp(FProcessEntry32.szExeFile, PChar(ExeFileName)) = 0)  then
                begin
// at this point the process id is in FProcessEntry32.th32ProcessID
//                    edit1.Text := (inttostr(FProcessEntry32.th32ProcessID));
                    memo1.lines.add(inttostr(FProcessEntry32.th32ProcessID));
// to kill it, do the following
(*
    Result := Integer(TerminateProcess(OpenProcess(
    PROCESS_TERMINATE, BOOL(0),
    FProcessEntry32.th32ProcessID), 0));
                *)
                end;
                ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
            end;
        finally
            CloseHandle(FSnapshotHandle);
        end;
    end;

procedure TForm1.Button1Click(Sender: TObject);
    begin
        ListProcessIdsForFile(eFilename.Text);
    end;

end.



********************* DFM FORM FOLLOWS *****************


object Form1: TForm1
  Left = 274
  Top = 168
  Width = 410
  Height = 372
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 32
    Top = 8
    Width = 217
    Height = 217
    TabOrder = 0
  end
  object Button1: TButton
    Left = 32
    Top = 280
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 200
    Top = 240
    Width = 121
    Height = 21
    TabOrder = 2
  end
  object eFileName: TEdit
    Left = 32
    Top = 240
    Width = 121
    Height = 21
    TabOrder = 3
    Text = 'Explorer.exe'
  end
end
0
 
TheRealLokiSenior DeveloperCommented:
this code was found elsewhere on EE, sorry forget where and who.

uses tlhelp32;
// note - this will not work on NT4

function KillTask(ExeFileName: string): integer;
const
    PROCESS_TERMINATE=$0001;
var
    ContinueLoop: BOOL;
    FSnapshotHandle: THandle;
    FProcessEntry32: TProcessEntry32;
begin
    result := 0;
    FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
    ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
    while integer(ContinueLoop) <> 0 do
    begin
        if (StrIComp(PChar(ExtractFileName(FProcessEntry32.szExeFile)), PChar(ExeFileName)) = 0)
        or (StrIComp(FProcessEntry32.szExeFile, PChar(ExeFileName)) = 0)  then
        begin

// at this point the process id is in FProcessEntry32.th32ProcessID
// to kill it, do the following
(*
            Result := Integer(TerminateProcess(OpenProcess(
                              PROCESS_TERMINATE, BOOL(0),
                              FProcessEntry32.th32ProcessID), 0));
*)
        end;
        ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
    end;
    CloseHandle(FSnapshotHandle);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  KillTask('Explorer.exe');
end;
0
 
ssivagnaAuthor Commented:
isnt there a clear code ??
 i want to inject my dll into internet explorer.
and if I do HWND the ie caption won't be constant. thats why i want processID by Iexplor.exe
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
TheRealLokiSenior DeveloperCommented:
umm, that looks pretty clear to me. you can have _multiple_ iexplor.exe's in the tasklist, so the above procedure cycles through all the processes and finds them all. you then have the processid to do what you want with.
0
 
ssivagnaAuthor Commented:
this didnt work

i tried

function KillTask(ExeFileName: string): integer;

var
    ContinueLoop: BOOL;
    FSnapshotHandle: THandle;
    FProcessEntry32: TProcessEntry32;
begin
    result := 0;
    FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
    ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
    while integer(ContinueLoop) <> 0 do
    begin
        if (StrIComp(PChar(ExtractFileName(FProcessEntry32.szExeFile)), PChar(ExeFileName)) = 0)
        or (StrIComp(FProcessEntry32.szExeFile, PChar(ExeFileName)) = 0)  then
        begin
       result :=FProcessEntry32.th32ProcessID;
    end;
      end;
       end;

and tried to get the result strtoint and to edit1.text didnt work.
0
 
HypoviaxCommented:
Simple code:

function FindPID(ExeFileName: string): integer;
const
PROCESS_TERMINATE=$0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot
(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle,
FProcessEntry32);
while integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName))
or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result :=
FProcessEntry32.th32ProcessID
ContinueLoop := Process32Next(FSnapshotHandle,
FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

From my answer at:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21164286.html

Regards,

Hypoviax
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.