Click here to Skip to main content
11,500,844 members (72,984 online)
Click here to Skip to main content

How to get handle to any running process by its name

, 24 Aug 2004 342.5K 7K 63
Rate this:
Please Sign up or sign in to vote.
Get handle to any running process (by its name) by using performance data

Introduction

There are several ways to get the process id (and its handle afterwards) for any running process. One of them is by using functions that are available in PSAPI.DLL, which is distributed in the Microsoft® Platform Software Development Kit (SDK).The same information is generally available through the performance data.

One of the performance objects is 'Process' whose index is 230. The 'Process' object has several counters, one of them is 'ID Process' whose index is 784. A function called GetProcessID runs through the performance objects and counters and retrieves the process id for the required process name.

The following sample shows you how to retrieve the process handle by performance data.

The Sample

std::vector<DWORD> SetOfPID;
GetProcessID("Rundll32",SetOfPID); 
     // get all process id's of Rundll32

if (SetOfPID.empty())   // Process is not running
{
    printf("Process is not running\n");
}
else    // Process is running
{
    for (int i=0;i < SetOfPID.size(); i++)
    {
        printf("Process ID is %d\n", SetOfPID[i]);
        HANDLE hProcess = OpenProcess(
             PROCESS_ALL_ACCESS,FALSE,SetOfPID[i]);
        // some code...
        CloseHandle(hProcess);
    }
}

More of my articles using performance counters

Updates

My first version returned only the first running instance. Since the process name is not unique I changed my code so it returns set of process id's.

  • 13.1.03 - I release the allocation of PERF_DATA_BLOCK at the end of the function
  • 19.3.03 - I add call to RegCloseKey(HKEY_PERFORMANCE_DATA) at the end of the function
  • 15.6.03 - Using none case-sensitive comparison for checking process existance.
  • 24.5.04 - Changed the first argument to RegQueryValueEx from "Global" (which retrieves the whole counters) to a string that specifies the specific process object and process-id counter. This change enables fast query.
  • 25.8.04 - Source and demo updated

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Dudi Avramov
Web Developer
Israel Israel
No Biography provided

Comments and Discussions

 
Questionhelp Pin
katekserv19-Sep-11 3:26
memberkatekserv19-Sep-11 3:26 
QuestionHow to get the PID of an out-of-proc server Pin
razvandynalog27-Jan-09 10:20
memberrazvandynalog27-Jan-09 10:20 
GeneralGDI object Pin
jinijinu28-Sep-08 23:29
memberjinijinu28-Sep-08 23:29 
GeneralHandle Count Pin
jinijinu28-Sep-08 23:26
memberjinijinu28-Sep-08 23:26 
QuestionHow to get the exe name which is running foreground Pin
CYRIL K THANKACHAN23-Oct-06 2:30
memberCYRIL K THANKACHAN23-Oct-06 2:30 
AnswerRe: How to get the exe name which is running foreground Pin
Programm3r7-Mar-07 23:10
memberProgramm3r7-Mar-07 23:10 
GeneralThx for the update, re: query by processID Pin
Dalroi25-Sep-06 11:26
memberDalroi25-Sep-06 11:26 
QuestionHow to get Handle of the specified process and kill the process??? Pin
Supriya Tonape16-Aug-05 1:22
memberSupriya Tonape16-Aug-05 1:22 
AnswerRe: How to get Handle of the specified process and kill the process??? Pin
I. Ameen3-May-06 21:54
memberI. Ameen3-May-06 21:54 
GeneralRe: How to get Handle of the specified process and kill the process??? Pin
vish_coder28-May-06 22:58
membervish_coder28-May-06 22:58 
GeneralRe: How to get Handle of the specified process and kill the process??? Pin
vish_coder28-May-06 22:58
membervish_coder28-May-06 22:58 
GeneralPlz Help Pin
abapaba30-Apr-05 11:02
memberabapaba30-Apr-05 11:02 
QuestionHOw to get the process ID of application which is in foreground Pin
sonawane ajay24-Oct-04 23:10
membersonawane ajay24-Oct-04 23:10 
AnswerRe: HOw to get the process ID of application which is in foreground Pin
Dudi Avramov30-Oct-04 22:38
memberDudi Avramov30-Oct-04 22:38 
QuestionWhat about RegCloseKey????? Pin
RochDrouin23-Aug-04 8:14
memberRochDrouin23-Aug-04 8:14 
AnswerRe: What about RegCloseKey????? Pin
RochDrouin25-Aug-04 7:07
memberRochDrouin25-Aug-04 7:07 
GeneralRe: What about RegCloseKey????? Pin
Dudi Avramov25-Aug-04 21:27
memberDudi Avramov25-Aug-04 21:27 
QuestionHow can I find the owner name &amp; pid of a process Pin
KUNTAL MONDAL19-Nov-03 12:56
memberKUNTAL MONDAL19-Nov-03 12:56 
AnswerRe: How can I find the owner name &amp; pid of a process Pin
Dudi Avramov23-Nov-03 22:35
memberDudi Avramov23-Nov-03 22:35 
QuestionThread safe? Pin
Riyadh Tanveer9-Jul-03 23:29
memberRiyadh Tanveer9-Jul-03 23:29 
AnswerRe: Thread safe? Pin
Riyadh Tanveer10-Jul-03 0:07
memberRiyadh Tanveer10-Jul-03 0:07 
QuestionWhy this way better than WMI ? Pin
Vladimir Afanasyev16-Jun-03 21:00
memberVladimir Afanasyev16-Jun-03 21:00 
GeneralSpeed question Pin
Arnaud Brejeon12-Jun-03 15:03
memberArnaud Brejeon12-Jun-03 15:03 
GeneralRe: Speed question Pin
Dudi Avramov14-Jun-03 21:49
memberDudi Avramov14-Jun-03 21:49 
GeneralWhat's Wrong With My OS Pin
fz_zhou15-May-03 3:44
memberfz_zhou15-May-03 3:44 
GeneralRe: What's Wrong With My OS Pin
Dudi Avramov15-May-03 5:11
memberDudi Avramov15-May-03 5:11 
GeneralRe: What's Wrong With My OS Pin
fz_zhou15-May-03 21:42
memberfz_zhou15-May-03 21:42 
Generalwhether it is WIN9x compactible Pin
Manjit10-Apr-03 2:28
memberManjit10-Apr-03 2:28 
GeneralGet Process Id from handle Pin
ravi_shingote29-Mar-03 4:34
memberravi_shingote29-Mar-03 4:34 
GeneralRe: Get Process Id from handle Pin
Dudi Avramov30-Mar-03 0:08
memberDudi Avramov30-Mar-03 0:08 
GeneralRe: Get Process Id from handle Pin
Ravi Shingote31-Mar-03 3:32
sussRavi Shingote31-Mar-03 3:32 
GeneralRe: Get Process Id from handle Pin
Dudi Avramov1-Apr-03 20:26
memberDudi Avramov1-Apr-03 20:26 
GeneralQuick fix Pin
Peter Donahue17-Mar-03 10:51
memberPeter Donahue17-Mar-03 10:51 
GeneralRe: Quick fix Pin
Dudi Avramov19-Mar-03 4:31
memberDudi Avramov19-Mar-03 4:31 
Generali want same sort in vb Pin
pawankumar6-Feb-03 0:11
memberpawankumar6-Feb-03 0:11 
GeneralRe: i want same sort in vb Pin
Dudi Avramov6-Feb-03 3:54
memberDudi Avramov6-Feb-03 3:54 
GeneralRe: i want same sort in vb Pin
Anonymous6-Feb-03 4:46
sussAnonymous6-Feb-03 4:46 
GeneralRe: i want same sort in vb Pin
Vladimir Afanasyev20-Jun-03 0:10
memberVladimir Afanasyev20-Jun-03 0:10 
GeneralDuh Pin
Andreas Saurwein13-Jan-03 0:03
memberAndreas Saurwein13-Jan-03 0:03 
GeneralRe: Duh Pin
John M. Drescher13-Jan-03 3:53
memberJohn M. Drescher13-Jan-03 3:53 
GeneralRe: Duh Pin
Dudi Avramov13-Jan-03 5:27
memberDudi Avramov13-Jan-03 5:27 
GeneralRe: Duh Pin
????19-Mar-03 11:29
suss????19-Mar-03 11:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 25 Aug 2004
Article Copyright 2002 by Dudi Avramov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid