Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to get processId of a process

Posted on 2004-10-28
6
Medium Priority
?
5,740 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
Comment
Question by:ssivagna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 12437086
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
ID: 12437488
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
ID: 12437647
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ssivagna
ID: 12439392
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:
TheRealLoki earned 750 total points
ID: 12439559
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
ID: 12439734
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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