Platform SDK: Network Management

NetSessionDel

The NetSessionDel function ends a network session between a server and a workstation.

Security Requirements

Only members of the Administrators or Account Operators local group can successfully execute the NetSessionDel function.

Windows NT/2000: The parameter order is as follows.

NET_API_STATUS NetSessionDel(
  LPWSTR servername,     
  LPWSTR UncClientName,  
  LPWSTR username        
);

Windows 95/98: The sReserved parameter replaces the Windows NT/Windows 2000 username parameter. For more information, see the following Remarks section. The parameter list is as follows.

extern API_FUNCTION
 NetSessionDel(
  const char FAR *pszServer,      
  const char FAR *pszClientName,  
  short  sReserved                
);

Parameters

servername
[in] Pointer to a Unicode (Windows NT/2000) or ANSI (Windows 95/98) string specifying the name of the remote server on which the function is to execute. The string must begin with \\. If this parameter is NULL, the local computer is used.
UncClientName
[in] Pointer to a Unicode (Windows NT/2000) or ANSI (Windows 95/98) string specifying the computer name of the client to disconnect. If UncClientName is NULL, then all the sessions of the user identified by the username parameter will be deleted on the server specified by servername. For more information, see NetSessionEnum.
username
[in] Pointer to a Unicode string specifying the name of the user whose session is to be terminated. If this parameter is NULL, all users' sessions from the client specified by the UncClientName parameter are to be terminated.

Return Values

If the function succeeds, the return value is NERR_Success.

If the function fails, the return value can be one of the following error codes.

Value Meaning
ERROR_ACCESS_DENIED The user does not have access to the requested information.
ERROR_INVALID_PARAMETER The specified parameter is invalid.
ERROR_NOT_ENOUGH_MEMORY Insufficient memory is available.
NERR_ClientNameNotFound A session does not exist with that computer name.

Remarks

Windows 95/98: You must specify the session key in the sReserved parameter when you call NetSessionDel. The session key is returned by the NetSessionEnum function or the NetSessionGetInfo function in the sesi50_key member of the session_info_50 structure. See the NetSessionDel Sample (Windows 95/98) topic to view a code sample that demonstrates how to use the NetSessionDel function.

Windows NT/2000: The following code sample demonstrates how to terminate a session between a server and a workstation using a call to the NetSessionDel function.

#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <windows.h> 
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
   DWORD dwError = 0;
   LPTSTR pszServerName = NULL;
   LPTSTR pszClientName = NULL;
   LPTSTR pszUserName = NULL;
   NET_API_STATUS nStatus;
   //
   // Check command line arguments.
   //
   if (argc > 4)
   {
      wprintf(L"Usage: %s [\\\\ServerName] [\\\\ClientName] [UserName]\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)
      fprintf(stderr, "The specified session(s) has been successfully deleted\n");
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);

   return 0;
}

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. For more information, see IADsSession and IADsFileServiceOperations.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Lmshare.h (Windows NT/2000) or Svrapi.h (Windows 95/98); include Lm.h (Windows NT/2000).
  Library: Use Netapi32.lib (Windows NT/2000) or Svrapi.lib (Windows 95/98).

See Also

Network Management Overview, Network Management Functions, Session Functions, NetSessionEnum, NetSessionGetInfo, session_info_50