5,426,531 members and growing! (14,910 online)
Email Password   helpLost your password?
General Programming » Threads, Processes & IPC » Processes     Intermediate

How to get handle to any running process by its name

By Dudi Avramov

Get handle to any running process (by its name) by using performance data
VC6, C++Windows, NT4, Win2K, STL, VS6, Visual Studio, Dev

Posted: 5 Aug 2002
Updated: 24 Aug 2004
Views: 185,339
Bookmarked: 50 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
28 votes for this Article.
Popularity: 4.57 Rating: 3.16 out of 5
5 votes, 19.2%
1
3 votes, 11.5%
2
3 votes, 11.5%
3
5 votes, 19.2%
4
10 votes, 38.5%
5

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



Occupation: Web Developer
Location: Israel Israel

Other popular Threads, Processes & IPC articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 38 (Total in Forum: 38) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralHow to get the exe name which is running foregroundmemberCYRIL K THANKACHAN3:30 23 Oct '06  
AnswerRe: How to get the exe name which is running foregroundmemberProgramm3r0:10 8 Mar '07  
GeneralThx for the update, re: query by processIDmemberDalroi12:26 25 Sep '06  
GeneralHow to get Handle of the specified process and kill the process???memberSupriya Tonape2:22 16 Aug '05  
AnswerRe: How to get Handle of the specified process and kill the process???memberI. Ameen22:54 3 May '06  
GeneralRe: How to get Handle of the specified process and kill the process???membervish_coder23:58 28 May '06  
GeneralRe: How to get Handle of the specified process and kill the process???membervish_coder23:58 28 May '06  
GeneralPlz Helpmemberabapaba12:02 30 Apr '05  
GeneralHOw to get the process ID of application which is in foregroundmembersonawane ajay0:10 25 Oct '04  
GeneralRe: HOw to get the process ID of application which is in foregroundmemberDudi Avramov23:38 30 Oct '04  
GeneralWhat about RegCloseKey?????memberRochDrouin9:14 23 Aug '04  
GeneralRe: What about RegCloseKey?????memberRochDrouin8:07 25 Aug '04  
GeneralRe: What about RegCloseKey?????memberDudi Avramov22:27 25 Aug '04  
GeneralHow can I find the owner name & pid of a processmemberKUNTAL MONDAL13:56 19 Nov '03  
GeneralRe: How can I find the owner name & pid of a processmemberDudi Avramov23:35 23 Nov '03  
GeneralThread safe?memberRiyadh Tanveer0:29 10 Jul '03  
GeneralRe: Thread safe?memberRiyadh Tanveer1:07 10 Jul '03  
GeneralWhy this way better than WMI ?memberVladimir Afanasyev22:00 16 Jun '03  
GeneralSpeed questionmemberArnaud Brejeon16:03 12 Jun '03  
GeneralRe: Speed questionmemberDudi Avramov22:49 14 Jun '03  
GeneralWhat's Wrong With My OSmemberfz_zhou4:44 15 May '03  
GeneralRe: What's Wrong With My OSmemberDudi Avramov6:11 15 May '03  
GeneralRe: What's Wrong With My OSmemberfz_zhou22:42 15 May '03  
Generalwhether it is WIN9x compactiblememberManjit3:28 10 Apr '03  
GeneralGet Process Id from handlememberravi_shingote5:34 29 Mar '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Aug 2004
Editor: Nishant Sivakumar
Copyright 2002 by Dudi Avramov
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project