The Win32 Network Management APIs 50






NetServerGetInfo() Program Example


The NetServerGetInfo() function retrieves current configuration information for the specified server. The following code sample demonstrates how to retrieve current configuration information for a server using a call to the NetServerGetInfo() function. The sample calls NetServerGetInfo(), specifying information level 101 (SERVER_INFO_101). If the call succeeds, the code attempts to identify the type of server. Finally, the sample frees the memory allocated for the information buffer.

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


NetServerGetInfo() Program Example: Creating new Win32 C++ console application project


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


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


Then, add the following source code.


#ifndef UNICODE

#define UNICODE



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


#include <stdio.h>

#include <windows.h>

#include <lm.h>


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


   DWORD dwLevel = 101;


   NET_API_STATUS nStatus;

   LPTSTR pszServerName = NULL;


   if (argc != 2)


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

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




   // The server is not the default local computer.

   if (argc == 2)

      pszServerName = (LPTSTR) argv[1];

   // Call the NetServerGetInfo() function, specifying level 101.

   nStatus = NetServerGetInfo(pszServerName,dwLevel,(LPBYTE *)&pBuf);


   // If the call succeeds,

   if (nStatus == NERR_Success)


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

      // Check for the type of server.

      if ((pBuf->sv101_type & SV_TYPE_DOMAIN_CTRL) ||  (pBuf->sv101_type & SV_TYPE_DOMAIN_BAKCTRL) || (pBuf->sv101_type & SV_TYPE_SERVER_NT))            

         wprintf(L"This is a server\n");


         wprintf(L"This is a workstation\n");


   // Otherwise, print the system error.



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

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



   // Free the allocated memory.

   if (pBuf != NULL)



   return 0;



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


NetServerGetInfo() Program Example: A sample program output





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