Saturday, July 23, 2022

CPP: Windows: GetLogicalDriveStrings

CPP: Windows: GetLogicalDriveStrings

Fills a buffer with strings that specify valid drives in the system.
#include <iostream>
#include <Windows.h>
#include <vector>
int main()
{
#ifdef UNICODE
typedef std::wstring String;
#else
typedef std::string String;
#endif
const DWORD A2Z = 26;
const DWORD BUFFER = sizeof(TCHAR) * A2Z;
TCHAR lpBuffer[BUFFER];// buffer for drive string storage
GetLogicalDriveStrings(BUFFER, lpBuffer);
std::vector<String> drives_strings;
const TCHAR* current_drive = lpBuffer;
String str;
std::cout << "The logical drives of this machine are:" << std::endl;
while (*current_drive) {
str = current_drive;
drives_strings.push_back(str);
current_drive += drives_strings.back().length() + 1;
}
for (String s : drives_strings) {
#ifdef UNICODE
std::wcout << s << std::endl;
#else
std:cout << s < std::endl;
#endif
}
return 0;
}

Notes:
- Tested using Visual Studio 2022 C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getlogicaldrivestringsw

No comments:

Fedora install screen chronicle

Fedora install screen chronicle Below are links to Fedora installation screens. It is interesting to see how it has evolved over time. Fe...