CPP: Windows: GetLogicalDriveStrings
Fills a buffer with strings that specify valid drives in the system.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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:
Post a Comment