[C++.NET][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++.NET][Windows] Process lister

Post by ayu »

Todays little experiment was rather small, but still fun.
I made a small application that will grab all the process names and their PIDs, and list them.
Half of the fun was to read a bunch of articles and reference material from MSDN to get all the material that I needed for the experiment.


Anyway here it is:

Code: Select all

#using <System.dll>

#include <iostream>
#include <string>

using namespace std;
using namespace System;
using namespace System::Diagnostics;
using namespace Runtime::InteropServices;

int main()
{
	array<Process^>^pLocal = Process::GetProcesses();
	char* chars;
	string sName;

	for(int i = 0; i < pLocal->Length; ++i)
	{
		chars = (char*)(Marshal::StringToHGlobalAnsi(pLocal[i]->ProcessName)).ToPointer();
		sName = chars;
		Marshal::FreeHGlobal(IntPtr((void*)chars));

		cout << "---------------\n";
		cout << "Name: " << sName << "\n" << "PID: " << pLocal[i]->Id << "\n";
		cout << "---------------\n\n";
	}

	system("PAUSE");
	return 0;
}
Last edited by ayu on 06 Dec 2009, 10:39, edited 1 time in total.
"The best place to hide a tree, is in a forest"

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Re: [C++][Windows] Process lister

Post by IceDane »

cats wrote:Todays little experiment was rather small, but still fun.
I made a small application that will grab all the process names and their PIDs, and list them.
Half of the fun was to read a bunch of articles and reference material from MSDN to get all the material that I needed for the experiment.


Anyway here it is:

Code: Select all

#using <System.dll>

#include <iostream>
#include <string>

using namespace std;
using namespace System;
using namespace System::Diagnostics;
using namespace Runtime::InteropServices;

int main()
{
	array<Process^>^pLocal = Process::GetProcesses();
	char* chars;
	string sName;

	for(int i = 0; i < pLocal->Length; ++i)
	{
		chars = (char*)(Marshal::StringToHGlobalAnsi(pLocal[i]->ProcessName)).ToPointer();
		sName = chars;
		Marshal::FreeHGlobal(IntPtr((void*)chars));

		cout << "---------------\n";
		cout << "Name: " << sName << "\n" << "PID: " << pLocal[i]->Id << "\n";
		cout << "---------------\n\n";
	}

	system("PAUSE");
	return 0;
}
It should be added that this isn't standard C++ by any means. This is "C++.NET", or "managed C++". You want the toolhelp32 family of functions for standard C/++ way of displaying processes.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: [C++][Windows] Process lister

Post by ayu »

IceDane wrote: It should be added that this isn't standard C++ by any means. This is "C++.NET", or "managed C++". You want the toolhelp32 family of functions for standard C/++ way of displaying processes.
Good point, changed the subject.
I'll create another one using the Tool Help Library =)
"The best place to hide a tree, is in a forest"

Post Reply