Çalışan tüm processlere ait bilgiler nasıl elde edilir?

Bu post içerisinde bilgisayar üzerinde çalışan processlerin bazı bilgilerinin nasıl ekrana basılacağını göstereceğim. Bu işlemler için Windows.h ve Psapi.h kütüphanelerini projeye eklemek önemlidir. Processe dair şu bilgiler ekrana basılacaktır:

  • Process ID’si (PID),
  • Processin ve tüm modüllerinin adı,
  • Processin versiyonu,
  • Processin modeli (Native veya WoW64),
  • Process ve modüllerinin handle değerleri,
  • Min. ve Max. working set boyutları.

Bu bilgilerin nasıl api yardımıyla alınacağını merak ediyorsanız:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <psapi.h>

DWORD PrintModules(DWORD processID)
{
	HMODULE hMods[1024];
	HANDLE hProcess;
	DWORD cbNeeded;
	unsigned int i;

	// Print the process identifier.

	printf("\nProcess ID: %u\n\n", processID);

	// Get a handle to the process.

	hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
		PROCESS_VM_READ,
		FALSE, processID);
	if (NULL == hProcess)
		return 1;

	// Get a list of all the modules in this process.

	if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
	{
		for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
		{
			TCHAR szModName[MAX_PATH];

			// Get the full path to the module's file.

			if (GetModuleFileNameEx(hProcess, hMods[i], szModName,
				sizeof(szModName) / sizeof(TCHAR)))
			{
				// Print the module name and handle value.

				_tprintf(TEXT("\t%s (0x%08X)\n"), szModName, hMods[i]);
			}
		}
	}
	printf("\n");
	CloseHandle(hProcess);

	return processID;

}

void PrintProcessNameAndID(DWORD processID)
{
	TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
	DWORD processVersion; // GetProcessVersion
	SIZE_T  dwMin, dwMax; // Get Working Set Sizes

	// Get a handle to the process.
	HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
		PROCESS_VM_READ,
		FALSE, processID);

	// Get the process name.

	if (NULL != hProcess)
	{
		HMODULE hMod;
		DWORD cbNeeded;

		if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
			&cbNeeded))
		{
			GetModuleBaseName(hProcess, hMod, szProcessName,
				sizeof(szProcessName) / sizeof(TCHAR));
		}
	}

	processVersion = GetProcessVersion(processID);

	GetProcessWorkingSetSize(hProcess, &dwMin, &dwMax);

	DWORD pdwHandleCount;
	GetProcessHandleCount(hProcess, (&pdwHandleCount));

	BOOL IsWow64 = FALSE;
	IsWow64Process(hProcess, &(IsWow64));
	DWORD currentProcessId;
	currentProcessId = GetCurrentProcessId();

	DWORD a = PrintModules(processID);
	printf("More informations about Process \n");


	if (IsWow64 == TRUE)
	{
		_tprintf(TEXT("Process Name: %s \nProcess Version: %u \nMin. Working Set-Size: %lu KB Max. Working Set-Size: %lu KB \nHandle value of the process: %u \nProcess model is WoW64 \n\n"),
			szProcessName, processVersion, dwMin / 1024, dwMax / 1024, pdwHandleCount);
		if (a == currentProcessId)
		{
			std::cout << "Current processor core that the program runs on it" << GetCurrentProcessorNumber << std::endl;
		}
		for (int i = 5; i < 10; i++) { printf("***** \t ----- \t ***** \n"); }
	}
	else
	{
		_tprintf(TEXT("Process Name: %s \nProcess Version: %u \nMin. Working Set-Size: %lu KB Max. Working Set-Size: %lu KB \nHandle value of the process: %u \nProcess model is Native \n\n"), 
		szProcessName, processVersion,dwMin/1024,dwMax/1024, pdwHandleCount);
		if (a == currentProcessId)
		{
			std::cout << "Current processor core that the program runs on \t" << GetCurrentProcessorNumber << std::endl;
		}
		for (int i = 5; i < 10; i++) {printf("***** \t ----- \t ***** \n");}
	}

	printf("\n");
	//_tprintf(TEXT("Process Name: %s Process Version: %u Min. Working Set-Size: %lu KB Max. Working Set-Size: %lu KB Handle value of the process: %u \n\n"), szProcessName, processVersion,dwMin/1024,dwMax/1024, pdwHandleCount);
	CloseHandle(hProcess);
}

int main()
{
	// Get the list of process identifiers.

	DWORD aProcesses[1024], cbNeeded, cProcesses;
	unsigned int i;

	if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
	{
		return 1;
	}


	// Calculate how many process identifiers were returned.

	cProcesses = cbNeeded / sizeof(DWORD);

	// Print the name and process identifier for each process.

	for (i = 0; i < cProcesses; i++)
	{
		if (aProcesses[i] != 0)
		{
			PrintProcessNameAndID(aProcesses[i]);
		}
	}

	printf("\n ***** / ***** Currently amount of %d processes running on this device ***** \\ *****\n\n\n\n\n",i);

	system("pause");

	return 0;
}
Please follow and like us:
error