Získanie názvov všetkých aktuálnych jednotiek/médií (diskov, USB zariadení) pripojených v počítači.

 

Napísal som peknú funkciu, ktorá zistí všetky aktuálne pripojené jednotky diskov.

Funkcia teda zistí názvy (písmená) pripojených jednotiek.

Výstupom funkcie je dátový typ ::std::vektor<::std::string>, ktorý returnuje uložené jednotky v údajovom type vector.



Návrh funkcie:


::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;
}

 

 

 

-----------------------------------------------------------------

Celá funkcia je zakonpovaná do headeru WinAPI.h WinAPI.h


#include "WinAPI.h" //https://www.netbot.sk/sk/14-blog-headers/31-winapi

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; }