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.

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

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, 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;
}
// 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(LNetUserModalsGet() 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(LNetUserModalsGet() failed!\n);
fwprintf_s(stderr, LA 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.
