[C++][Windows] Process lister

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C++][Windows] Process lister

Post by ayu »

There, I made a simple one without the .NET framework as well :)

Code: Select all

//This is a small test on how to get system process information
//The code follows a non-tolerant design
//which means that it can not handle errors (or simply that it wont)

#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <iostream>

using namespace std;

int main()
{
	//Get a snapshot of all the processes
	HANDLE			hProcessSnap	= CreateToolhelp32Snapshot(0x00000002, 0);
	PROCESSENTRY32	pe32;

	//Set the size of the structure
	pe32.dwSize = sizeof(PROCESSENTRY32);

	//Grab the information about the first process
	Process32First(hProcessSnap, &pe32);

	//Print the first process, continue if more
	do
	{
		cout << "----------------------\n";
		//Use _tprintf to print the WCHAR, and TEXT to format the string appropriately and interpret the result
		_tprintf(TEXT("Process Name: %s\n"), pe32.szExeFile);
		cout << "Process ID: " << pe32.th32ProcessID << "\n";
		cout << "----------------------\n";

	} while(Process32Next(hProcessSnap, &pe32));

	//Close the handle
	CloseHandle(hProcessSnap);

	//Pause the execution, we want to see what it printed
	system("PAUSE");

	return 0;
}
"The best place to hide a tree, is in a forest"

Post Reply