Platform SDK: Network Management

NetSessionGetInfo

The NetSessionGetInfo function retrieves information about a session established between a particular server and workstation.

Security Requirements

Windows NT: Only members of the Administrators or Account Operators local group can successfully execute the NetSessionGetInfo function at level 1 or level 2. No special group membership is required for level 0 or level 10 calls.

Windows 2000: If you call this function at information level 1 or 2 on a Windows 2000 domain controller that is running Active Directory, access is allowed or denied based on the access-control list (ACL) for the securable object. The default ACL permits all authenticated users and members of the "Pre-Windows 2000 compatible access" group to view the information. By default, the "Pre-Windows 2000 compatible access" group includes Everyone as a member. This enables anonymous access to the information if the system allows anonymous access.

If you call this function at information level 1 or 2 on a Windows 2000 member server or workstation, all authenticated users can view the information. Anonymous access is also permitted if the RestrictAnonymous policy setting allows anonymous access.

For more information about restricting anonymous access, see Security Requirements for the Network Management Functions.

Parameter Order

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

NET_API_STATUS NetSessionGetInfo(
  LPWSTR servername,     
  LPWSTR UncClientName,  
  LPWSTR username,       
  DWORD level,           
  LPBYTE *bufptr         
);

Windows 95/98: The calling application must use the cbBuffer parameter to specify the size, in bytes, of the information buffer pointed to by the pbBuffer parameter. If the length of the buffer is not large enough to hold all of the data, the function returns as much as will fit in the buffer. The function returns the total number of bytes of information available in the pcbTotalAvail parameter. The Windows NT/Windows 2000 username parameter is not available on this platform. Therefore, the parameter list is as follows.

extern API_FUNCTION
 NetSessionGetInfo(
  const char FAR * pszServer,        
  const char FAR * pszClientName,    
  short sLevel,                      
  char FAR * pbBuffer,               
  unsigned short  cbBuffer,          
  unsigned short FAR * pcbTotalAvail 
);

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 name of the computer session for which information is to be returned. This parameter cannot be NULL. For more information, see NetSessionEnum.
username
[in] Pointer to a Unicode string specifying the name of the user whose session information is to be returned. This parameter cannot be NULL.
level
[in] Specifies the information level of the data. This parameter can be one of the following values.
Value Meaning
0 Return the name of the computer that established the session.

Windows NT/2000: The bufptr parameter points to a SESSION_INFO_0 structure.

Windows 95/98: The pbBuffer parameter points to a session_info_0 structure.

1 Return the name of the computer, name of the user, and open files, pipes, and devices on the computer.

Windows NT/2000: The bufptr parameter points to a SESSION_INFO_1 structure.

Windows 95/98: The pbBuffer parameter points to a session_info_1 structure.

2 In addition to the information indicated for level 1, return the type of client and how the user established the session.

Windows NT/2000: The bufptr parameter points to a SESSION_INFO_2 structure.

Windows 95/98: The pbBuffer parameter points to a session_info_2 structure.

10 Return the name of the computer; name of the user; and active and idle times for the session.

Windows NT/2000: The bufptr parameter points to a SESSION_INFO_10 structure.

Windows 95/98: The pbBuffer parameter points to a session_info_10 structure.

50 Return the name of the computer, name of the user, open files on the computer, and the name of the transport protocol the client is using.

Windows 95/98: The pbBuffer parameter points to a session_info_50 structure.


bufptr
[out] Pointer to the buffer that receives the data. The format of this data depends on the value of the level parameter.

Windows NT/2000: This buffer is allocated by the system and must be freed using the NetApiBufferFree function.

Windows 95/98: The caller must allocate and deallocate this buffer.

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_LEVEL The value specified for the level parameter is invalid.
ERROR_INVALID_PARAMETER The specified parameter is invalid.
ERROR_NOT_ENOUGH_MEMORY Insufficient memory is available.
NERR_ClientNameNotFound A session does not exist with the computer name.
NERR_InvalidComputer The computer name is invalid.
NERR_UserNotFound The user name could not be found.

Remarks

Windows 95/98: See the NetSessionGetInfo Sample (Windows 95/98) topic to view a code sample that demonstrates how to use the NetSessionGetInfo function.

Windows NT/2000: The following code sample demonstrates how to retrieve information about a session using a call to the NetSessionGetInfo function. The sample calls NetSessionGetInfo, specifying information level 10 (SESSION_INFO_10). If the call succeeds, the code prints information about the session. Finally, the sample frees the memory allocated for the information buffer.

#ifndef UNICODE
#define UNICODE
#endif

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

int wmain(int argc, wchar_t *argv[])
{
   DWORD dwLevel = 10;
   LPSESSION_INFO_10 pBuf = NULL;
   LPTSTR pszServerName = NULL;
   LPTSTR pszUNCClientName = NULL;
   LPTSTR pszUserName = NULL;
   NET_API_STATUS nStatus;
   //
   // Check command line arguments.
   //
   if (argc == 3)
   {
      pszUNCClientName = argv[1];
      pszUserName = argv[2];
   }
   else if (argc == 4)
   {
      pszServerName = argv[1];
      pszUNCClientName = argv[2];
      pszUserName = argv[3];
   }
   else
   {
      wprintf(L"Usage: %s [\\\\ServerName] \\\\ClientName UserName\n", argv[0]);
      exit(1);
   }
   //
   // Call the NetSessionGetInfo function, specifying level 10.
   //
   nStatus = NetSessionGetInfo(pszServerName,
                               pszUNCClientName,
                               pszUserName,
                               dwLevel,
                               (LPBYTE *)&pBuf);
   //
   // If the call succeeds,
   //
   if (nStatus == NERR_Success)
   {
      if (pBuf != NULL)
      {
         //
         // Print the session information. 
         //
         wprintf(L"\n\tClient: %s\n", pBuf->sesi10_cname);
         wprintf(L"\tUser:   %s\n", pBuf->sesi10_username);
         printf("\tActive: %d\n", pBuf->sesi10_time);
         printf("\tIdle:   %d\n", pBuf->sesi10_idle_time);
      }
   }
   //
   // Otherwise, indicate a system error.
   //
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Free the allocated memory.
   //
   if (pBuf != NULL)
      NetApiBufferFree(pBuf);

   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, NetSessionDel, NetSessionEnum, SESSION_INFO_0, SESSION_INFO_1, SESSION_INFO_2, SESSION_INFO_10, session_info_0, session_info_1, session_info_2, session_info_10, session_info_50