Get names of all current drives / media (discs, USB devices) connected to the computer.
I wrote a nice function that detects all currently connected drives.
The function will then find out the names (letters) of the connected units.
The outup of the function is datetype ::std::vektor<::std::string>, which returns the stored units in the vector data type.
Function design:
::vector<::std::string>GetDriverLabel(void)
{
::std::vector <::std::string> volumelabels;
::HANDLE hDevice;
char drv;
const char *charDrv;
drv='A';
while(drv!='[')
{
::std::stringstream Str;
::std::string drvStr;
Str<<drv;
Str>>drvStr;
::std::string drvSpc=drvStr+":";
::std::string fCheck="\\\\.\\";
::std::string fhCheck=fCheck+drvStr+":";
charDrv=drvSpc.c_str();
::std::wstring stemp = ::std::wstring(fhCheck.begin(), fhCheck.end());
::LPCWSTR sw = stemp.c_str();
hDevice=::CreateFile(sw, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if(hDevice!=INVALID_HANDLE_VALUE)
{
::std::wstring stemp = ::std::wstring(drvSpc.begin(), drvSpc.end());
::LPCWSTR sw = stemp.c_str();
switch(GetDriveType(sw))
{
case DRIVE_FIXED:
{
volumelabels.push_back(charDrv);
break;
}
case DRIVE_REMOVABLE:
{
volumelabels.push_back(charDrv);
break;
}
case DRIVE_NO_ROOT_DIR:
{
volumelabels.push_back(charDrv);
break;
}
case DRIVE_REMOTE:
{
volumelabels.push_back(charDrv);
break;
}
case DRIVE_CDROM:
{
break;
}
case DRIVE_RAMDISK:
{
break;
}
case DRIVE_UNKNOWN:
{
break;
}
}
}
drv++;
}
return volumelabels;
}
-----------------------------------------------------------------
The whole function is built into header WinAPI.h ►WinAPI.h◄
#include "WinAPI.h" //https://www.netbot.sk/en/14-blog-headers/44-winapi-en
int _cdecl main (void)
{
::Diall_WinApi::WinApi::GetInstance()->SystemIntegrity(::Diall_WinApi::Privilege::ENABLE);
::std::vector<::std::string> label = ::Diall_WinApi::WinApi::GetInstance()->GetDriverLabel();
//Zobrazenie pripojenych jednotiek odpredu.//
for (std::vector<::std::string>::iterator it = label.begin() ; it != label.end(); ++it)
{
::std::cout << *it << "\n";
}
//Zobrazenie pripojenych jednotiek odzadu. Nutne pouzit Release Mod!//
for (vector::iterator it = label.end()-1; it != label.begin()-1; it--)
{
::std::cout << *it << "\n";
}
return 0;
}