Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process names from ID values

Status
Not open for further replies.

adholioshake

Programmer
Feb 9, 2003
136
Hi everyone.

I can't quite get my head around this problem. I can get all the process ID values by using the 'EnumProcesses()' function. However, this isn't very helpful to me because the ID values are just hex numbers and change each time you run the same process. Does anyone know how to get the process names, eg. 'explore.exe', from the process ID or vice-versa ? Any help would be appreciated.

Adonai :)
 
Use a Process ID to get a HANDLE with OpenProcess() then with the HANDLE use GetProcessImageFileName()

-pete
 
Pete - Where is the 'GetProcessImageFileName()' function ?
Do you know what include files/library files this is in as it is not in any of my help files. Thanks.
 
It is part of the PSAPI. Do you know about MSDN online?

Go to: msdn.microsoft.com and type the function in the search box and click the GO button.

-pete
 
I have tried a different approach now, but my problem has been reversed (!) I can get a list of names for all processes running, but now I don't know how to get the process ID's from these. How do you get the process ID from the process name ?
(The code I've made uses the PDH interface).
Code:
// This program only needs the essential windows header files.
#define WIN32_LEAN_AND_MEAN 1

#include <windows.h>
#include <winperf.h>
#include <malloc.h>
#include <stdio.h>
#include <tchar.h>
#include <pdh.h>

int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
{
	PDH_STATUS  pdhStatus          = ERROR_SUCCESS;
	char	      szCounterListBuffer[20000];
	DWORD       dwCounterListSize  = sizeof(szCounterListBuffer);
	char	      szInstanceListBuffer[20000];
	DWORD       dwInstanceListSize = sizeof(szInstanceListBuffer);
	LPTSTR      szThisInstance     = NULL;
	PPDH_COUNTER_INFO ppdhcounterinfo = NULL;

	HANDLE hconsoleInput, hconsoleOutput;
	char string[100];
	DWORD written;
	strcpy(string,&quot;> Console Program started\n&quot;);

	AllocConsole();
	hconsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	hconsoleInput  = GetStdHandle(STD_INPUT_HANDLE);
	WriteConsole(hconsoleOutput,string,strlen(string),&written,0);
	
	if ((szCounterListBuffer != NULL) &&
			(szInstanceListBuffer != NULL)) 
	{
		pdhStatus = PdhEnumObjectItems (
	              NULL,   // reserved
                NULL,   // local machine
                &quot;Process&quot;, // object to enumerate
                szCounterListBuffer,    // pass in NULL buffers
                &dwCounterListSize,     // an 0 length to get
                szInstanceListBuffer,   // required size 
                &dwInstanceListSize,    // of the buffers in chars
                PERF_DETAIL_WIZARD,     // counter detail level
                0);     
		if (pdhStatus == ERROR_SUCCESS) 
		{
			sprintf(string, TEXT(&quot;- Running Processes:&quot;));
			WriteConsole(hconsoleOutput,string,strlen(string),&written,0);
			// Walk the return instance list.
			for (szThisInstance = szInstanceListBuffer;
						*szThisInstance != 0;
						szThisInstance += lstrlen(szThisInstance) + 1) 
			{
				sprintf(string, TEXT(&quot;\n  %s&quot;), szThisInstance);
				WriteConsole(hconsoleOutput,string,strlen(string),&written,0);
			}
		}
	} 
	else 
	{
		sprintf(string, TEXT(&quot;\nPROCLIST: unable to allocate buffers&quot;));
		WriteConsole(hconsoleOutput,string,strlen(string),&written,0);
	}
	sprintf(string, &quot;\n> Console Program Ended.&quot;);
	WriteConsole(hconsoleOutput,string,strlen(string),&written,0);
	sprintf(string, &quot;\nPress return to close.\n&quot;);
	WriteConsole(hconsoleOutput,string,strlen(string),&written,0);
	ReadConsole(hconsoleInput,string,1,&written,0);
	
	return 0;
}
Any help on how to get process ID ? Thanks.

Adonai
 
I've cracked it.
Heres the code I used if anyone is interrested:
Code:
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	HANDLE hconsoleInput, hconsoleOutput;
	char string[100];
	DWORD written;
	HANDLE hSnapShot;
	PROCESSENTRY32 ProcEntry;
	BOOL hRes;

	AllocConsole();
	hconsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	hconsoleInput  = GetStdHandle(STD_INPUT_HANDLE);
	
	strcpy(string,&quot;> Running Processes...\n&quot;);
        WriteConsole(hconsoleOutput,string,strlen(string),&written,0);

	hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
	ProcEntry.dwSize = sizeof(ProcEntry);
	Process32First(hSnapShot, &ProcEntry);
	while(1)
	{
		hRes = Process32Next (hSnapShot, &ProcEntry);
	  if (hRes == FALSE)
			break;
	        sprintf(string,&quot;  %s %x\n&quot;, ProcEntry.szExeFile, ProcEntry.th32ProcessID);
		WriteConsole(hconsoleOutput,string,strlen(string),&written,0);
	}
	// Pause until return pressed.
        ReadConsole(hconsoleInput,string,1,&written,0);
	return (0);
}
Adonai :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top