The Win32 Network Management APIs 23






NetQueryDisplayInformation() Program Example


The NetQueryDisplayInformation() function returns user account, computer, or group account information. Call this function to quickly enumerate account information for display in user interfaces. The following code sample demonstrates how to return group account information using a call to the NetQueryDisplayInformation() function. If the user specifies a server name, the sample first calls the MultiByteToWideChar() function to convert the name to Unicode. The sample calls NetQueryDisplayInformation(), specifying information level 3 (NET_DISPLAY_GROUP) to retrieve group account information. If there are entries to return, the sample returns the data and prints the group information. Finally, the code 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.


NetQueryDisplayInformation() Program Example: Creating new Win32 console application project


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


NetQueryDisplayInformation() Program Example: Adding new C++ source file


Then, add the following source code.


#include <windows.h>

#include <stdio.h>

#include <lm.h>


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


void wmain(int argc, WCHAR *argv[ ])



   DWORD res, dwRec, i = 0;


   // You can pass a NULL or empty string

   //  to retrieve the local information as in this example

   WCHAR szServer[255]=L"";


   if(argc > 1)     

      // Check to see if a server name was passed;

      //  if so, convert it to Unicode. However, this is redundant...

      MultiByteToWideChar(CP_ACP, 0, (LPCSTR)argv[1], -1, szServer, 255);


   do // begin do


      // Call the NetQueryDisplayInformation() function;

      //   specify information level 3 (group account information).

      res = NetQueryDisplayInformation(szServer, 3, i, 1000, MAX_PREFERRED_LENGTH, &dwRec, (PVOID*) &pBuff);


      // If the call succeeds,

      if((res==ERROR_SUCCESS) || (res==ERROR_MORE_DATA))


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

         p = pBuff;



            // Print the retrieved group information.

            wprintf(L"Name:      %s\n"

                  L"Comment:   %s\n"

                  L"Group ID:  %u\n"

                  L"Attributes: %u\n"







            // If there is more data, set the index.

            i = p->grpi3_next_index;



         // Free the allocated memory.




              wprintf(L"NetQueryDisplayInformation() failed, error: %u\n", res);


        // Continue while there is more data.

   } while (res==ERROR_MORE_DATA); // end do




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


NetQueryDisplayInformation() Program Example: A sample console program output





< Win32 Network Management APIs 22 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 24 >