NetSessionDel() Program Example
This function ends a network session between a server and a workstation. Only members of the Administrators or Server Operators local group can successfully execute the NetSessionDel() function. If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management session functions. The following code sample demonstrates how to terminate a session between a server and a workstation using a call to the NetSessionDel() 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 *argv[])
{
DWORD dwError = 0;
LPTSTR pszServerName = NULL;
LPTSTR pszClientName = NULL;
LPTSTR pszUserName = NULL;
NET_API_STATUS nStatus;
// Check command line arguments.
if (argc > 4 || argc == 1)
{
wprintf(LUsage: %s [\\\\ServerName] [\\\\ClientName] [UserName]\n, argv[0]);
wprintf(LExample: %s \\\\ServerGedik \\\\Gedikclient Mikespoon\n, argv[0]);
exit(1);
}
if (argc >= 2)
pszServerName = argv[1];
if (argc >= 3)
pszClientName = argv[2];
if (argc == 4)
pszUserName = argv[3];
// Call the NetSessionDel() function to delete the session.
nStatus = NetSessionDel(pszServerName,pszClientName,pszUserName);
// Display the result of the call.
if (nStatus == NERR_Success)
{
wprintf(LNetSessionDel() is OK!\n);
fwprintf_s(stderr, LThe specified session(s) has been successfully deleted\n);
}
else
{
wprintf(LNetSessionDel() 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.
