NetSessionGetInfo function (lmshare.h)

Retrieves information about a session established between a particular server and workstation.

Syntax

NET_API_STATUS NET_API_FUNCTION NetSessionGetInfo(
  [in]  LMSTR  servername,
  [in]  LMSTR  UncClientName,
  [in]  LMSTR  username,
  [in]  DWORD  level,
  [out] LPBYTE *bufptr
);

Parameters

[in] servername

Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. If this parameter is NULL, the local computer is used.

[in] UncClientName

Pointer to a string that specifies the name of the computer session for which information is to be returned. This parameter is required and cannot be NULL. For more information, see NetSessionEnum.

[in] username

Pointer to a string that specifies the name of the user whose session information is to be returned. This parameter is required and cannot be NULL.

[in] level

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.

The bufptr 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.

The bufptr 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.

The bufptr 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.

The bufptr parameter points to a SESSION_INFO_10 structure.

[out] bufptr

Pointer to the buffer that receives the data. The format of this data depends on the value of the level parameter. For more information, see Network Management Function Buffers and Network Management Function Buffer Lengths.

This buffer is allocated by the system and must be freed using the NetApiBufferFree function.

Return value

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.

Return code Description
ERROR_ACCESS_DENIED
The user does not have access to the requested information.
ERROR_INVALID_LEVEL
The value specified for the level parameter is not valid.
ERROR_INVALID_PARAMETER
The specified parameter is not valid.
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 not valid.
NERR_UserNotFound
The user name could not be found.

Remarks

Only members of the Administrators or Server 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.

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.

If you call this function at information level 1 or 2 on a member server or workstation, all authenticated users can view the information.

Examples

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
#pragma comment(lib, "Netapi32.lib")

#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;
}

Requirements

Requirement Value
Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header lmshare.h (include Lm.h)
Library Netapi32.lib
DLL Netapi32.dll

See also

NetSessionDel

NetSessionEnum

Network Management Functions

Network Management Overview

SESSION_INFO_0

SESSION_INFO_1

SESSION_INFO_10

SESSION_INFO_2

Session Functions