Input process name, output handle to process

Started by
6 comments, last by cache_hit 14 years, 1 month ago
I have completed a program that allows you to input a process name, such as calc.exe and the functions returns a handle to the process. This example in the main function calls the function to get a handle for any process you type in, and demonstrates that it has the handle by calling GetProcessID (HANDLE Process) I am self taught at programming, so I would like an experienced programmer to tell me how I can improve the GetProcHandleFromName function or if there is an easier way to do this. Thanks.

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <tchar.h>
#include <psapi.h>
using namespace std;

#pragma comment (lib, "Psapi.lib")

HANDLE GetProcHandleFromName (char procName[50]);


int main( )
{
	HANDLE handy;
	char str[50];

		cout << "Enter name of process (include .exe extension)";
		cin >> str;

		handy = GetProcHandleFromName (str);
		if (handy == NULL)
			cout << "Could not find proccess\n";
		else
			cout << "Process Id is " << GetProcessId(handy) << "\n";


	



	return 0;
}





HANDLE GetProcHandleFromName (char procName[50])
{
	
	// retrieve a list of running processes
	DWORD IDs[1024]; // array to store process ids
	ZeroMemory (IDs, sizeof(IDs));
	DWORD byteTotal; // amount of data put into array in bytes
	DWORD byteTotalMod;

	if ( !EnumProcesses( IDs, sizeof(IDs), &byteTotal) )
	{	
		cout << "Failed to get list of process IDs";
		return NULL;
	}
	HANDLE proc; // handle for the process itself
	HMODULE mods; // handle to hold each process's modules
	char aProcName[50];

	for (unsigned int i = 0; i < (byteTotal / sizeof(DWORD)); i++)	// go through each process ID
	{
		proc = OpenProcess (PROCESS_ALL_ACCESS, 0, IDs);	// get the handle for the process
		if (proc != NULL)
		{

			if ( !EnumProcessModules (proc, &mods, sizeof(mods), &byteTotalMod) ) // store the modules for the process in the array
			{
				cout << "Failed to enum proccess modules for ID " << IDs;
				return NULL;
			}

			if ( !GetModuleBaseName (proc, mods, aProcName, sizeof(aProcName)/sizeof(char)) )
			{
				cout << "Failed to get moduel base name for " << IDs;
				return NULL;
			}
			
			if ( !strcmp (aProcName, procName) )
			{	
				return proc;	
			}
		}
	}



	return NULL;
}


Advertisement
What happens if the process name entered is greater than 50 chars long ?
Personally, I'd use:
#include <tlhelp32.h>HANDLE GetProcHandleFromName(const char* procName){   HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);   if(!hSnapshot)      return NULL;   PROCESSENTRY32 pe;   pe.dwSize = sizeof(pe);   if(!Process32First(hSnapshot, &pe))   {      CloseHandle(hSnapshot);      return NULL;   }   DWORD dwProcessId = 0;   do {      if(stricmp(pe.szExeFile, procName) == 0)      {         dwProcessId = pe.th32ProcessID;         break;      }      pe.dwSize = sizeof(pe);   } while(Process32Next(hSnapshot, &pe));   CloseHandle(hSnapshot);   if(dwProcessId == 0)      return NULL;   return OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);}
Quote:Original post by Evil Steve
Personally, I'd use:
*** Source Snippet Removed ***


I never knew those functions existed, thanks. By the way, why did you only include that one header and what does it do?
Quote:Original post by neal8929
I never knew those functions existed, thanks. By the way, why did you only include that one header and what does it do?


See MSDN - CreateToolhelp32Snapshot Function

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Quote:Original post by neal8929
Quote:Original post by Evil Steve
Personally, I'd use:
*** Source Snippet Removed ***


I never knew those functions existed, thanks. By the way, why did you only include that one header and what does it do?


Keep in mind that multiple instances of said program may be running. Do you care *which* instance you get a handle to?
Quote:Original post by cache_hit
Quote:Original post by neal8929
Quote:Original post by Evil Steve
Personally, I'd use:
*** Source Snippet Removed ***


I never knew those functions existed, thanks. By the way, why did you only include that one header and what does it do?


Keep in mind that multiple instances of said program may be running. Do you care *which* instance you get a handle to?


It would be cool to be able to choose between which process. I guess I could just make it so that if more than one process with the same name is running you can choose between the two process ids?
Quote:Original post by neal8929
Quote:Original post by cache_hit
Quote:Original post by neal8929
Quote:Original post by Evil Steve
Personally, I'd use:
*** Source Snippet Removed ***


I never knew those functions existed, thanks. By the way, why did you only include that one header and what does it do?


Keep in mind that multiple instances of said program may be running. Do you care *which* instance you get a handle to?


It would be cool to be able to choose between which process. I guess I could just make it so that if more than one process with the same name is running you can choose between the two process ids?


Yea. If you're providing some kind of interface, you might be able to show the title of the main window (if there is one). In the case of calculator that might not help you much, but browsers for example display the title of the webpage, word displays the title of the document, etc.

This topic is closed to new replies.

Advertisement