NetUserSetInfo() Program Example
The NetUserSetInfo() function sets the parameters of a user account. The following code sample demonstrates how to disable a user account with a call to the NetUserSetInfo() function. The code sample fills in the usri1008_flags member of the USER_INFO_1008 structure, specifying the value UF_ACCOUNTDISABLE. Then the sample calls NetUserSetInfo(), 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.
/* Unicode setting if needed
#ifndef UNICODE
#define UNICODE
#endif
*/
// Include the following library when linking
#pragma comment(lib, netapi32.lib)
#include <stdio.h>
#include <windows.h>
#include <lm.h>
int wmain(int argc, wchar_t *argv[])
{
DWORD dwLevel = 1008;
USER_INFO_1008 ui;
NET_API_STATUS nStatus;
if (argc != 3)
{
fwprintf_s(stderr, LUsage: %s \\\\ServerName UserName\n, argv[0]);
wprintf(LExample: %s \\\\UntaGedik Jbones\n, argv[0]);
exit(1);
}
// Fill in the USER_INFO_1008 structure member.
// UF_SCRIPT: required.
ui.usri1008_flags = UF_SCRIPT | UF_ACCOUNTDISABLE;
// Call the NetUserSetInfo function
// to disable the account, specifying level 1008.
nStatus = NetUserSetInfo(argv[1], argv[2], dwLevel, (LPBYTE)&ui, NULL);
// Display the result of the call.
if (nStatus == NERR_Success)
{
wprintf(LNetUserSetInfo() is OK!\n);
fwprintf_s(stderr, LUser account %s has been disabled!\n, argv[2]);
}
else
{
wprintf(LNetUserSetInfo() failed!\n);
fwprintf_s(stderr, LA system error has occurred, error %d\n, nStatus);
}
return 0;
}
Build and run the project. The following screenshot is a sample output.
