The Win32 Network Management APIs 33






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.


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


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


NetUserDel() Program Example: Adding new C++ source file for C++ source code


Then, add the following source code.


#ifndef UNICODE

#define UNICODE



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

      wprintf(L"Example: %s \\\\BigBos \"Mike Tyson\"\n", argv[0]);




   // 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(L"NetUserDel() is fine!\n");

         fwprintf_s(stderr, L"User %s has been successfully deleted on %s\n", argv[2], argv[1]);




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

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



   return 0;



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


NetUserDel() Program Example: A sample console program output, deleting user account


Verify the action through Computer Management snap-in.


NetUserDel() Program Example: Confirming the user account was deleted using Computer Management snap-in





< Win32 Network Management APIs 32 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 34 >