NetUserDel() Program Example
The NetUserDel() function deletes a user account from a server. The following code sample demonstrates how to delete a user account with a call to the NetUserDel() function.
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
#pragma comment(lib, netapi32.lib)
#include <stdio.h>
#include <windows.h>
#include <lm.h>
int wmain(int argc, wchar_t *argv[])
{
DWORD dwError = 0;
NET_API_STATUS nStatus;
// All parameters are required.
if (argc != 3)
{
fwprintf_s(stderr, LUsage: %s \\\\ServerName UserName\n, argv[0]);
wprintf(LExample: %s \\\\BigBos \Mike Tyson\\n, argv[0]);
exit(1);
}
// Call the NetUserDel() function to delete the share.
nStatus = NetUserDel(argv[1], argv[2]);
// Display the result of the call.
if (nStatus == NERR_Success)
{
wprintf(LNetUserDel() is fine!\n);
fwprintf_s(stderr, LUser %s has been successfully deleted on %s\n, argv[2], argv[1]);
}
else
{
wprintf(LNetUserDel() 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.
Verify the action through Computer Management snap-in.