Process Information via win32

The GetCommandLine() function can be used to retrieve the command line that was used to start the program. The GetCommandLine() function returns a pointer to the command line string that started the process.

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

int _tmain(int argc, _TCHAR* argv[])
{
	_TCHAR *szCommandLine = GetCommandLine();

	_tprintf(_T("The command line is: %s\n"), szCommandLine);

	return 0;
}

Note that _TCHAR is the official version of TCHAR; in practice, they both map to the same thing.

The environment strings of the process can be manipulated using four different functions.

The GetEnvironmentStrings() function returns a pointer to the block of environment strings. The return value points to a block of null-terminated strings, each of which follows the pattern key=value. The final string is followed by an extra null.

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

int _tmain(int argc, _TCHAR* argv[])
{
	TCHAR *szEnvStrings = GetEnvironmentStrings();
	int iLen; 
	do{
		_tprintf(_T("%s\n"), szEnvStrings);
		iLen = _tcslen(szEnvStrings);
		//move down the block by the number of 
		//TCHARs in the string, plus one 
		//for the null character
		szEnvStrings+=(iLen+1);
	}while (*szEnvStrings != '');

	return 0;
}

In order to modify individual environment variables, the functions GetEnvironmentVariable() and SetEnvironmentVariable() are available.

The GetEnvironmentVariable() function takes three parameters. The first parameter holds the name, and the second parameter is a pointer to a buffer to hold the value. The third parameter gives the size of the buffer. The GetEnvironmentVariable() function returns 0 if the variable is not found; otherwise, it returns the number of characters written to the buffer.

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

int _tmain(int argc, _TCHAR* argv[])
{
	const int Buffer_Length = 256;

	TCHAR szBuffer[Buffer_Length];

	TCHAR *szEnvVar[] = {_T("USERPROFILE"), _T("COMPUTERNAME"), _T("TEMP")};
	
	if(GetEnvironmentVariable(*szEnvVar, szBuffer, Buffer_Length)){
		_tprintf(_T("Environment variable %s retrieved.\n"), *szEnvVar);
		_tprintf(_T("%s = %s\n"), *szEnvVar, szBuffer);
	}

	if(GetEnvironmentVariable(*(szEnvVar+1), szBuffer, Buffer_Length)==0){
		_tprintf(_T("Error getting variable %s.\n"), *(szEnvVar+1));
	} else {
		_tprintf(_T("Environment variable %s retrieved successfully.\n"), *(szEnvVar+1));
		_tprintf(_T("%s = %s\n"), *(szEnvVar+1), szBuffer);
	}

	if(GetEnvironmentVariable(szEnvVar[2], szBuffer, Buffer_Length)>0){
		_tprintf(_T("Environment variable %s retrieved successfully.\n"), szEnvVar[2]);
		_tprintf(_T("%s = %s\n"), szEnvVar[2], szBuffer);
	}

	return 0;
}

The SetEnvironmentVariable() function is used to change the value of an environment variable or create a new one. The SetEnvironmentVariable() function returns a Boolean value of True if the function call is successful, and False if it is not.

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

int _tmain(int argc, _TCHAR* argv[])
{
	const int Buffer_Size = 256;
	const TCHAR *szCPEnvVar = _T("ALVAL");

	TCHAR szBuffer[Buffer_Size];

	if(SetEnvironmentVariable(szCPEnvVar, _T("42"))){
		_tprintf(_T("%s set.\n"), szCPEnvVar);
	}

	if(GetEnvironmentVariable(szCPEnvVar, szBuffer, Buffer_Size)>0){
		_tprintf(_T("%s = %s\n"), szCPEnvVar, szBuffer);
	}

	return 0;
}

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