The Win32 Network Management APIs 29

 

 

 

 

 

NetUserModalsGet() Program Example

 

The NetUserModalsGet() function retrieves global information for all users and global groups in the security database, which is the security accounts manager (SAM) database or, in the case of domain controllers, the Active Directory. The following code sample demonstrates how to retrieve global information for all users and global groups with a call to the NetUserModalsGet() function. The sample calls NetUserModalsGet(), specifying information level 0 (USER_MODALS_INFO_0). If the call succeeds, the sample prints global password 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.

 

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

Then, add the source file. Give it a name.

 

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

 

Then, add the following source code.

 

#ifndef UNICODE

#define UNICODE

#endif

 

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

 

#include <stdio.h>

#include <windows.h>

#include <lm.h>

 

#define INFO_BUFFER_SIZE 32767

 

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

{

   DWORD dwLevel = 0;

   USER_MODALS_INFO_0 *pBuf = NULL;

   NET_API_STATUS nStatus;

   LPTSTR pszServerName = NULL;

   WCHAR  infoBuf[INFO_BUFFER_SIZE];

   DWORD  bufCharCount = INFO_BUFFER_SIZE;

  

   if (argc > 2)

   {

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

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

         wprintf(L"Default to local if server name not supplied..\n");

         exit(1);

   }

 

   // The server is not the default local computer.

   if (argc == 2)

   {

         pszServerName = (LPTSTR) argv[1];

         wprintf(L"Remote server is \"%s\"\n", pszServerName);

   }

   else if(argc == 1)// default to local

   {

         GetComputerName(infoBuf, &bufCharCount);

         wprintf(L"Using local, \"%s\"\n", infoBuf);

         pszServerName = infoBuf;

   }

 

   // Call the NetUserModalsGet function; specify level 0.

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

 

   // If the call succeeds, print the global information.

   if (nStatus == NERR_Success)

   {

         wprintf(L"NetUserModalsGet() should be fine!\n");

        

         if (pBuf != NULL)

         {

               wprintf(L"\tMinimum password length:  %d\n", pBuf->usrmod0_min_passwd_len);

               wprintf(L"\tMaximum password age (d): %d\n", pBuf->usrmod0_max_passwd_age/86400);

               wprintf(L"\tMinimum password age (d): %d\n", pBuf->usrmod0_min_passwd_age/86400);

               wprintf(L"\tForced log off time (s):  %d\n", pBuf->usrmod0_force_logoff);

               wprintf(L"\tPassword history length:  %d\n", pBuf->usrmod0_password_hist_len);

      }

   }

   // Otherwise, print the system error.

   else

   {

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

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

   }

 

   // Free the allocated memory.

   if (pBuf != NULL)

      NetApiBufferFree(pBuf);

 

   return 0;

}

 

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

 

 

NetUserModalsGet() Program Example: A sample program output

 

 

 

 

< Win32 Network Management APIs 28 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 30 >