Directory Management in win32

Creating and deleting directories involves a pair of thankfully simple functions, CreateDirectory() and RemoveDirectory(). The CreateDirectory() function takes two arguments, the first is the pathname to the directory to create, and the second is a pointer to a SECURITY_ATTRIBUTES structure. For the second argument, we can simply put NULL.  The RemoveDirectory() function has only one parameter, the name of the directory to be removed. Both functions return a Boolean value stating the success or failure of the function call.

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

int _tmain(int argc, char argv[]){

	if((CreateDirectory("exampleDir", NULL))==FALSE){
		printf("Error creating directory.\n");
		exit(EXIT_FAILURE);
	} else {
		printf("Directory created.\n");
	}

	if((RemoveDirectory("exampleDir"))==FALSE){
		printf("Error removing directory.\n");
		exit(EXIT_FAILURE);
	} else {
		printf("Directory deleted.\n");
	}

	return 0;

}

A process has a working directory. We can both get and set the working directory. The GetCurrentDirectory() function takes two arguments, the size of the buffer in characters, and the buffer to write the full pathname of the directory to. The SetCurrentDirectory() function has one parameter, the name of the directory we wish to set as the current working directory.

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

int _tmain(int argc, char argv[]){

	TCHAR szBuffer[MAX_PATH + 1];
	TCHAR szNewDir[] = _T("testdir");
	DWORD dwRValue = 0;
	CreateDirectory(szNewDir, NULL);

	dwRValue = GetCurrentDirectory(MAX_PATH, szBuffer);

	if(dwRValue != 0){
		_tprintf(_T("Current directory is %s\n"), szBuffer);
	} else {
		printf("Error getting current directory.\n");
		exit(EXIT_FAILURE);
	}

	if(!(SetCurrentDirectory(szNewDir))){
		printf("Could not change directory.\n");
		exit(EXIT_FAILURE);
	} else {
		printf("Directory changed.\n");
	}

	if(!(GetCurrentDirectory(MAX_PATH, szBuffer))){
		printf("Could not get working directory.\n");
		exit(EXIT_FAILURE);
	} else {
		_tprintf(_T("Current directory is %s\n"), szBuffer);
	}

	return 0;

}

The ReadConsole() and WriteConsole() functions are used to read and write from the console. Both the ReadConsole() and WriteConsole() functions take five arguments; the first argument is a HANDLE to standard input or standard output.  We get a standard handle using the GetStdHandle() function, which takes a single parameter that can be one of three DWORD values, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, and STD_ERROR_HANDLE .

#include <Window.h>
#include <stdlib.h>
#include <stdio.h>

int _tmain(int argc, char argv[]){

	//create a handle to standard output
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	TCHAR *szMsg = "Nice boat!";
	DWORD dwCount;
	BOOLEAN bRValue;

	if(hOut==INVALID_HANDLE_VALUE){
		printf("Could not open a file handle to standard output.\n");
		exit(EXIT_FAILURE);
	}

	WriteConsole(hOut, szMsg, _tcslen(szMsg),  &dwCount, NULL);

	if(dwCount == _tcslen(szMsg)){
		printf("\nThe full message has been printed.\n");
	} else {
		printf("\nError! The full message has not printed.\n");
	}

	CloseHandle(hOut);

	return 0;

}

Note that a process can only have one console at a time.

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