The Win32 Network Management APIs 30






NetUserModalsSet() Program Example


The NetUserModalsSet() function sets 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 set the global information for all users and global groups with a call to the NetUserModalsSet() function. The sample fills in the members of the USER_MODALS_INFO_0 structure and calls NetUserModalsSet(), specifying information level 0.

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


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


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


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


Then, add the following source code.


#ifndef UNICODE

#define UNICODE



#define INFO_BUFFER_SIZE 32767


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


#include <stdio.h>

#include <windows.h>

#include <lm.h>


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


   DWORD dwLevel = 0;


   NET_API_STATUS nStatus;

   LPTSTR pszServerName = NULL;


   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");




   // 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;



   // Fill in the USER_MODALS_INFO_0 structure.

   ui.usrmod0_min_passwd_len = 0;

   ui.usrmod0_max_passwd_age = (86400 * 30);

   ui.usrmod0_min_passwd_age = 0;

   // never force logoff

   ui.usrmod0_force_logoff = TIMEQ_FOREVER;

   ui.usrmod0_password_hist_len = 0;


   // Call the NetUserModalsSet function; specify level 0.

   nStatus = NetUserModalsSet((LPCWSTR) pszServerName,dwLevel,(LPBYTE)&ui, NULL);


   // If the call succeeds, inform the user.

   if (nStatus == NERR_Success)


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

         fwprintf_s(stderr, L"Modals information set successfully on %s\n", pszServerName);



   // Otherwise, print the system error.



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

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



   return 0;



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


NetUserModalsGet() Program Example: A sample console program output





< Win32 Network Management APIs 29 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 31 >