[WinAPI] Cum enumar 'network drives'?

Folosire Windows API in programe C/C++ (forum moderat)
Post Reply
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

[WinAPI] Cum enumar 'network drives'?

Post by Ovidiu Cucu » 13 Dec 2009, 13:42

Intrebare
Cum enumar folderele din retea asociate unui drive local la care e conectat computerul (network drives)?

Raspuns
O metoda simpla este cu ajutorul functiei WNetGetConnection.

Exemplu

Code: Select all

#include <Windows.h>
#include <Winnetwk.h>
#include <tchar.h>
#include <iostream>
#pragma comment(lib, "Mpr.lib")
// ...
void ListNetworkDrives()
{
   DWORD dwLength = 256;
   TCHAR szLocalName[3] = {'\0', ':', '\0'};
   TCHAR szRemoteName[256] = {0};

   for(TCHAR chDrive = _T('a'); chDrive <= _T('z'); chDrive++)
   {
      szLocalName[0] = chDrive;
      DWORD dwRet = ::WNetGetConnection(szLocalName, szRemoteName, &dwLength);
      if(NO_ERROR == dwRet)
      {
         std::cout << "Local name: '" << szLocalName 
            << "'\tremote name: '" << szRemoteName << "'" << std::endl; 
      }
      else
      {
         // handle error; see WNetGetConnection documentation.
      }
   }
}
Vezi si
<< Back to Windows API Index



Post Reply