Introduction to win32 Processes

The OS sees applications that are currently running on the system as process objects. We can interact with process objects via a HANDLE, a system-supplied unique number. Any process running in the system can call GetCurrentProcess() to get a handle to identify itself.

#include <Windows.h>
#include <stdio.h>


int _tmain(void)
{

	HANDLE thisProcess = GetCurrentProcess();
	
	//GetProcessId() takes the HANDLE to a process
	//as its argument
	DWORD processID = GetProcessId(thisProcess);

	printf("The process ID for this process is %u\n", processID);

	return 0;
}

The process handle is often used as a parameter in API calls. We will look at two of these calls, the GetPriorityClass() call, and the GetProcessIoCounters() call. The GetPriorityClass() call takes only one argument, the HANDLE for the process. The GetProcessIoCounters() function takes two arguments. The first argument is the file handle, and the second argument is a pointer to an IO_COUNTERS structure, which holds the return data. The GetProcessIoCounters() call is used to get accounting information for all I/O operations performed by the specified process.

#include <Windows.h>
#include <stdio.h>

void printPriority(DWORD dwPriority);
void printProcessIOCount(const IO_COUNTERS *structIOCounters);

int _tmain(void)
{

	HANDLE hProcess = GetCurrentProcess();

	DWORD dwPID;
	DWORD dwPriority;
	IO_COUNTERS structIOCounters;

	dwPID = GetProcessId(hProcess);
	dwPriority = GetPriorityClass(hProcess);
	GetProcessIoCounters(hProcess, &structIOCounters);

	//present the retrieved data
	printf("Process No. %u\n", dwPID);
	printPriority(dwPriority);
	printProcessIOCount(&structIOCounters);

	return 0;
}//end _tmain()

//display the I/O statistics of the process
void printProcessIOCount(const IO_COUNTERS * structIOCounters){
	printf("\n%u read operations performed,", structIOCounters->ReadOperationCount);
	printf(" %u bytes read.\n", structIOCounters->ReadTransferCount);
	printf("%u write operations performed, ", structIOCounters->WriteOperationCount);
	printf("%u bytes written.\n", structIOCounters->WriteTransferCount);


}//end printProcessIOCount()

//display the priority of the process
void printPriority(DWORD dwPriority)
{
	switch(dwPriority)
	{
		case HIGH_PRIORITY_CLASS:
			printf("High");
			break;
		case NORMAL_PRIORITY_CLASS:
			printf("Normal");
			break;
		case IDLE_PRIORITY_CLASS:
			printf("Idle");
			break;
		case REALTIME_PRIORITY_CLASS:
			printf("Realtime");
			break;
		default:
			printf("Unknown");
			break;
	} //end switch(dwPriority)

	printf(" priority.");
} //end printPriority()

The CreateProcess() function creates a new process. The CreateProcess() function takes ten arguments. The first argument is of type LPCTSTR and indicates the name of the program we wish to run.

#include <Windows.h>
#include <stdio.h>

int _tmain(void)
{
	LPCTSTR prgm = "notepad.exe";
	LPCTSTR prgm2 = "mspaint.exe";

	printf("%s\n", prgm);
	printf("%s\n", prgm2);

	return 0;
}

The name for the first argument is lpFileName. Note that this argument must include the file extension of the program we wish to run. This argument is optional, which means we can specify NULL for it.

We will look more at CreateProcess() at a later date.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s