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.

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

Then, add the following source code.
#ifndef UNICODE
#define UNICODE
#endif
#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;
USER_MODALS_INFO_0 ui;
NET_API_STATUS nStatus;
LPTSTR pszServerName = NULL;
WCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;
if (argc > 2)
{
fwprintf_s(stderr, LUsage: %s [\\\\ServerName]\n, argv[0]);
wprintf(LExample: %s \\\\MyServerName\n, argv[0]);
wprintf(LDefault 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(LRemote server is \%s\\n, pszServerName);
}
else if(argc == 1)// default to local
{
GetComputerName(infoBuf, &bufCharCount);
wprintf(LUsing 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(LNetUserModalsSet() should be fine!\n);
fwprintf_s(stderr, LModals information set successfully on %s\n, pszServerName);
}
// Otherwise, print the system error.
else
{
wprintf(LNetUserModalsSet() failed!\n);
fwprintf_s(stderr, LA system error has occurred: %d\n, nStatus);
}
return 0;
}
Build and run the project. The following screenshot is a sample output.
