Getting drive letter and information

Posts 1–3 of 3 · Page 1 of 1
Getting drive letter and information
Had an idea for creating some files and folders to SD cards we use at work, automate everything!
Started working on this, needed the drive letter and drive name in order to determine if it's the proper device.

After much googling

Code:
#include <Windows.h>
#include <iostream>
#include <vector>
#include <tchar.h>
#include <string>
#include <sstream>

// Returns "(letter):\\"
std::string buildPath(const char, const std::string);

// returns the wstring version of string
std::wstring s_t_wstr(const std::string&);

int main()
{
	DWORD szDrives = ::GetLogicalDrives();
	std::vector<std::string> driveLetter;

	TCHAR fileSystemName[MAX_PATH + 1] = { 0 };
	TCHAR volumeName[MAX_PATH + 1] = { 0 };
	DWORD serialNumber = 0;
	DWORD maxComponentLen = 0;
	DWORD fileSystemFlags = 0;

	for (int i = 0; i < 26; i++) {
		if (szDrives & (1 << i)) {
			driveLetter.push_back(buildPath(_T('A') + i, ":\\"));
		}
	}

	for (auto path : driveLetter) {
		std::cout << "Path = " << path << std::endl;
		if (GetVolumeInformation(s_t_wstr(path).c_str(), volumeName, ARRAYSIZE(volumeName), &serialNumber, &maxComponentLen, &fileSystemFlags, fileSystemName, ARRAYSIZE(fileSystemName))) {
			std::wcout << "\tFile System Name: " << std::wstring(fileSystemName) << std::endl;
			std::wcout << "\tVolume Name: " << std::wstring(volumeName) << std::endl;
			std::wcout << "\tSerial Number: " << serialNumber << std::endl;
			std::wcout << "\tMax Component Length: " << maxComponentLen << std::endl << std::endl;
		}
		else
		{
			std::cout << "\tNo Volume Information\n" << std::endl;
		}
	}

	return 0;
}

std::string buildPath(const char c, const std::string str)
{
	std::stringstream sstream(c + str);
	std::string path;

	sstream >> path;

	return path;
}	// end buildPath

std::wstring s_t_wstr(const std::string& s)
{
	int len, slength = (int)s.length() + 1;

	len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);

	wchar_t* buf = new wchar_t[len];

	MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
	std::wstring r(buf);
	delete[] buf;
	return r;
}	// end s_t_wstr


Credits pretty much go to JackOfAllTrades on dreamincode, showed how to print to the screen but did not know how to convert the TCHAR[] to a usable variable (wstring, because doing cout on volumeName will give a buffer value)
Awesome job buddy!
Can defently be usefull.
Hey, thanks for sharing this!
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?