Solved

how to get processId of a process

Posted on 2004-10-28
5,717 Views
Last Modified: 2010-05-19
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)
0
Question by:ssivagna
    6 Comments
     
    LVL 17

    Expert Comment

    by:TheRealLoki
    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
     

    Author Comment

    by:ssivagna
    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
     
    LVL 17

    Expert Comment

    by:TheRealLoki
    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
     

    Author Comment

    by:ssivagna
    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
     
    LVL 17

    Accepted Solution

    by:
    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
     
    LVL 5

    Expert Comment

    by:Hypoviax
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

     Java Android Coding Bundle

    Whether you're an Apple user or Android addict, learning to code for the Android platform is an extremely valuable, in-demand skill. It all starts with Java, the language behind the apps and games that make Android the top platform it is today.

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now