The Win32 Network Management APIs 21

 

 

 

 

 

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.

 

NetUserSetInfo() Program Example: Creating new Win32 C++ console application project using Visual C++ .NET

 

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

 

NetUserSetInfo() Program Example: Adding new C++ source file to the existing project

 

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, L"Usage: %s \\\\ServerName UserName\n", argv[0]);

      wprintf(L"Example: %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(L"NetUserSetInfo() is OK!\n");

         fwprintf_s(stderr, L"User account %s has been disabled!\n", argv[2]);

   }

   else

   {

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

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

   }

   return 0;

}

 

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

 

NetUserSetInfo() Program Example: A sample console program output

 

 

 

 

< Win32 Network Management APIs 20 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 22 >