Click here to Skip to main content
Licence 
First Posted 5 Aug 2002
Views 308,318
Bookmarked 63 times

How to get handle to any running process by its name

By | 24 Aug 2004 | Article
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

About the Author

Dudi Avramov

Web Developer

Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionhelp Pinmemberkatekserv3:26 19 Sep '11  
QuestionHow to get the PID of an out-of-proc server Pinmemberrazvandynalog10:20 27 Jan '09  
GeneralGDI object Pinmemberjinijinu23:29 28 Sep '08  
GeneralHandle Count Pinmemberjinijinu23:26 28 Sep '08  
QuestionHow to get the exe name which is running foreground PinmemberCYRIL K THANKACHAN2:30 23 Oct '06  
AnswerRe: How to get the exe name which is running foreground PinmemberProgramm3r23:10 7 Mar '07  
GeneralThx for the update, re: query by processID PinmemberDalroi11:26 25 Sep '06  
QuestionHow to get Handle of the specified process and kill the process??? PinmemberSupriya Tonape1:22 16 Aug '05  
AnswerRe: How to get Handle of the specified process and kill the process??? PinmemberI. Ameen21:54 3 May '06  
GeneralRe: How to get Handle of the specified process and kill the process??? Pinmembervish_coder22:58 28 May '06  
GeneralRe: How to get Handle of the specified process and kill the process??? Pinmembervish_coder22:58 28 May '06  
will this work on UNIX? if no, can you suggest some code for UNIX platform
 
Thanks in advance
vish
GeneralPlz Help Pinmemberabapaba11:02 30 Apr '05  
QuestionHOw to get the process ID of application which is in foreground Pinmembersonawane ajay23:10 24 Oct '04  
AnswerRe: HOw to get the process ID of application which is in foreground PinmemberDudi Avramov22:38 30 Oct '04  
QuestionWhat about RegCloseKey????? PinmemberRochDrouin8:14 23 Aug '04  
AnswerRe: What about RegCloseKey????? PinmemberRochDrouin7:07 25 Aug '04  
GeneralRe: What about RegCloseKey????? PinmemberDudi Avramov21:27 25 Aug '04  
QuestionHow can I find the owner name & pid of a process PinmemberKUNTAL MONDAL12:56 19 Nov '03  
AnswerRe: How can I find the owner name & pid of a process PinmemberDudi Avramov22:35 23 Nov '03  
QuestionThread safe? PinmemberRiyadh Tanveer23:29 9 Jul '03  
AnswerRe: Thread safe? PinmemberRiyadh Tanveer0:07 10 Jul '03  
QuestionWhy this way better than WMI ? PinmemberVladimir Afanasyev21:00 16 Jun '03  
GeneralSpeed question PinmemberArnaud Brejeon15:03 12 Jun '03  
GeneralRe: Speed question PinmemberDudi Avramov21:49 14 Jun '03  
GeneralWhat's Wrong With My OS Pinmemberfz_zhou3:44 15 May '03  

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.

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