The Win32 Network Management APIs 48

 

 

 

 

 

NetServerDiskEnum() Program Example

 

The NetServerDiskEnum() function retrieves a list of disk drives on a server. The function returns an array of three-character strings (a drive letter, a colon, and a terminating null character). Only members of the Administrators or Server Operators local group can successfully execute the NetServerDiskEnum() function on a remote computer. If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same results you can achieve by calling the network management server functions. The following code sample demonstrates how to call the NetServerDiskEnum() function to retrieve a list of disk drives on a server. The sample calls NetServerDiskEnum(), specifying the information level 0 (required). If there are entries to return, and the user has access to the information, it prints a list of the drives, in the format of a three-character string: a drive letter, a colon, and a terminating null character. The sample also prints the total number of entries that are available and a hint about the number of entries actually enumerated. Finally, the code sample frees the memory allocated for the buffer.

Create a new empty Win32 console application project. Give the project name and change the project location is needed.

 

NetServerDiskEnum() Program Example: Creating new C++ Win32 console application project in Visual C++ .NET

 

Then, add the source file and give it a suitable name.

 

NetServerDiskEnum() Program Example: Adding new C++ source file for the C++ source code

 

Then, add the following source code.

 

#ifndef UNICODE

#define UNICODE

#endif

 

#include <stdio.h>

#include <assert.h>

#include <windows.h>

#include <lm.h>

 

#pragma comment(lib, "netapi32.lib")

 

int wmain(int argc, wchar_t *argv[])

{

   // Drive letter, colon, NULL

   const int ENTRY_SIZE = 3;

   LPTSTR pBuf = NULL;

   // level must be zero

   DWORD dwLevel = 0;

   DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;

   DWORD dwEntriesRead = 0;

   DWORD dwTotalEntries = 0;

   NET_API_STATUS nStatus;

   LPWSTR pszServerName = NULL;

   LPTSTR pTmpBuf;

   DWORD i;

   DWORD dwTotalCount = 0;

 

   if (argc != 2)

   {

      fwprintf_s(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);

      wprintf(L"Example: %s \\\\Bigserver\n", argv[0]);

      exit(1);

   }

   // The server is not the default local computer.

   if (argc == 2)

      pszServerName = (LPTSTR) argv[1];

   // Call the NetServerDiskEnum() function.

   nStatus = NetServerDiskEnum(pszServerName,dwLevel,(LPBYTE *) &pBuf,dwPrefMaxLen, &dwEntriesRead,&dwTotalEntries,NULL);

   // If the call succeeds,

   if (nStatus == NERR_Success)

   {

        wprintf(L"NetServerDiskEnum() is OK!\n");

 

      if ((pTmpBuf = pBuf) != NULL)

      {

         // Loop through the entries.

         for (i = 0; i < dwEntriesRead; i++)

         {

            assert(pTmpBuf != NULL);

 

            if (pTmpBuf == NULL)

            {

               // On a remote computer, only members of the

               //  Administrators or the Server Operators

               //  local group can execute NetServerDiskEnum.

               fwprintf_s(stderr, L"An access violation has occurred\n");

               break;

            }

            // Print drive letter, colon, NULL for each drive;

            //   the number of entries actually enumerated; and

            //   the total number of entries available.

            fwprintf_s(stdout, L"\t%d. Disk: %S\n", i +1, pTmpBuf);

 

            pTmpBuf += ENTRY_SIZE;

            dwTotalCount++;

         }

 

         fwprintf_s(stderr, L"\nEntries enumerated: %d\n", dwTotalCount);

      }

   }

   else

   {

        wprintf(L"NetServerDiskEnum() failed!\n");

        fwprintf_s(stderr, L"A system error has occurred: %d\n", nStatus);

   }

 

   // Free the allocated buffer.

   if (pBuf != NULL)

      NetApiBufferFree(pBuf);

 

   return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

NetServerDiskEnum() Program Example: A sample program output

 

 

 

 

< Win32 Network Management APIs 47 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 49 >