Sign in to follow this  

Input process name, output handle to process

This topic is 2842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i]);	// 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[i];
				return NULL;
			}

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



	return NULL;
}


Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this