Working with Drives with win32

We can obtain information about any volume available locally on our machine or mounted over the network via the GetVolumeInformation() function. The GetVolumeInformation() function returns the volume name and serial number, along with the type of file system used on the volume and the length of the maximum file name for the file system in use on that volume.

The GetVolumeInformation() function has eight parameters.

The first parameter is a pointer to a string that contains the root directory of the volume. If we pass NULL as the argument the root of the current directory is used. The second argument is a pointer to a buffer that gets the name of a specified volume. The third argument is the length of this buffer, which can be a maximum of MAX_PATH + 1. The fourth argument is a pointer to a variable that receives the volume serial number; we can specify NULL if we do not care about the volume’s serial number. The fifth argument is optional; we will specify NULL for this argument.

The sixth argument is a pointer to a variable that stores system flags. There are a number of interesting system flags. For example, FILE_SUPPORTS_EXTENDED_ATTRIBUTES denotes that the specified volume supports extended attributes, chunks of application-specific metadata that an application can associate with a file. The FILE_PERSISTENT_ACLS flag denotes that the specified volume preserves and enforces Access Control Lists. The FILE_SUPPORTS_USN_JOURNAL flag tells us that the volume supports update sequence number journals. The update sequence number is the offset for a particular record in the change journal record stream. The FILE_SUPPORTS_TRANSACTIONS flag denotes that the specified volume supports transactions.

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


int _tmain(int argc, _TCHAR* argv[])
{
	printf("%08x FILE_SUPPORTS_EXTENDED_ATTRIBUTES\n", FILE_SUPPORTS_EXTENDED_ATTRIBUTES);
	printf("%08x FILE_PERSISTENT_ACLS\n", FILE_PERSISTENT_ACLS);
	printf("%08x FILE_SUPPORTS_USN_JOURNAL\n", FILE_SUPPORTS_USN_JOURNAL);
	printf("%08x FILE_SUPPORTS_TRANSACTIONS\n", FILE_SUPPORTS_TRANSACTIONS);
	printf("%08x FILE_SUPPORTS_ENCRYPTION\n", FILE_SUPPORTS_ENCRYPTION);

	return 0;
}

The second to last parameter is a pointer to a buffer to store the name of the file system, and the final parameter is the length of that buffer. The GetVolumeInformation() function returns True on success and False on failure.

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


int _tmain(int argc, _TCHAR* argv[])
{
	BOOL boolRValue;
	TCHAR wszVolumeName[MAX_PATH + 1];
	DWORD dwVolumeSerialNumber;
	DWORD dwFileSystemFlags;
	TCHAR wszSystemName[MAX_PATH + 1];

	boolRValue = GetVolumeInformation(
		NULL, //default to volume for current working directory
		wszVolumeName,
		MAX_PATH,
		&dwVolumeSerialNumber,
		NULL, //component max length
		&dwFileSystemFlags,
		wszSystemName,
		MAX_PATH
		);

	//output information
	//note the %ls format specifier is for printing wide strings
	printf("Volume name: %ls\n", wszVolumeName);
	printf("Volume serial number: %d\n", dwVolumeSerialNumber);
	printf("File system type: %ls\n", wszSystemName);

	printf("File system characteristics:\n");
	if(dwFileSystemFlags & FILE_SUPPORTS_ENCRYPTION){
		printf("The file system supports encryption.\n");
	} else {
		printf("The file system does not support encryption.\n");
	}
	if(dwFileSystemFlags & FILE_PERSISTENT_ACLS){
		printf("The volume preserves and enforces ACLs.\n");
	} else {
		printf("The volume does not preserve and enforce ACLs.\n");
	}
	if(dwFileSystemFlags & FILE_SUPPORTS_USN_JOURNAL){
		printf("The volume supports update sequence number journals.\n");
	} else {
		printf("The volume does not support update sequence number journals.\n");
	}
	if(dwFileSystemFlags & FILE_SUPPORTS_EXTENDED_ATTRIBUTES){
		printf("The volume supports extended attributes.\n");
	} else {
		printf("The volume does not support extended attributes.\n");
	}

	return 0;

}

The GetDriveType() function helps us determine the type of a drive connected to our system. The function takes one argument, a string denoting the root directory of the drive. If we pass NULL as an argument, the GetDriveType() function will display the drive type for the current working directory.

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

int _tmain(int argc, _TCHAR* argv[])
{
	
	switch(GetDriveType(NULL)){
	case 0:
		printf("Error.\n");
		break;
	case 1:
		printf("Drive does not exist.\n");
		break;
	case 2:
		printf("Removable media.\n");
		break;
	case 3:
		printf("Fixed disk.\n");
		break;
	case 4:
		printf("Network drive.\n");
		break;
	case 5:
		printf("CD-ROM drive.\n");
		break;
	}
	
	return 0;

}

That’s all for today. If you can, please take a look at my book on C programming: http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/

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