Platform SDK: Network Management |
The NetSessionEnum function provides information about sessions established on a server.
Only members of the Administrators or Account Operators local group can successfully execute the NetSessionEnum function at level 1 or level 2. No special group membership is required for level 0 or level 10 calls.
Windows NT/2000: The parameter order is as follows.
NET_API_STATUS NetSessionEnum( LPWSTR servername, LPWSTR UncClientName, LPWSTR username, DWORD level, LPBYTE *bufptr, DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries, LPDWORD resume_handle );
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. (The cbBuffer parameter replaces the Windows NT/Windows 2000 prefmaxlen parameter.) Neither a user name parameter nor a resume handle parameter is available on this platform. Therefore, the parameter list is as follows.
extern API_FUNCTION NetSessionEnum( const char FAR * pszServer, short sLevel, char FAR * pbBuffer, unsigned short cbBuffer, unsigned short FAR * pcEntriesRead, unsigned short FAR * pcTotalAvail );
Windows NT/2000: The following levels are valid.
Value | Meaning |
---|---|
0 | Return the name of the computer that established the session. The bufptr parameter points to an array of SESSION_INFO_0 structures. |
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 an array of SESSION_INFO_1 structures. |
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 an array of SESSION_INFO_2 structures. |
10 | Return the name of the computer, name of the user, and active and idle times for the session. The bufptr parameter points to an array of SESSION_INFO_10 structures. |
502 | Return the name of the computer; name of the user; open files, pipes, and devices on the computer; and the name of the transport the client is using. The bufptr parameter points to an array of SESSION_INFO_502 structures. |
Windows 95/98: The following level is valid.
Value | Meaning |
---|---|
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. The pbBuffer parameter points to an array of session_info_50 structures. |
Windows NT/2000: This buffer is allocated by the system and must be freed using the NetApiBufferFree function. Note that you must free the buffer even if the function fails with ERROR_MORE_DATA.
Windows 95/98: The caller must allocate and deallocate this buffer.
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_MORE_DATA | More entries are available. Specify a large enough buffer to receive all entries. |
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. |
Windows 95/98: See the NetSessionEnum Sample (Windows 95/98) topic to view a code sample that demonstrates how to use the NetSessionEnum function.
Windows NT/2000: The following code sample demonstrates how to retrieve information about current sessions using a call to the NetSessionEnum function. The sample calls NetSessionEnum, specifying information level 10 (SESSION_INFO_10). The sample loops through the entries and prints the retrieved information. Finally, the code prints the total number of sessions enumerated and frees the memory allocated for the information buffer.
#ifndef UNICODE #define UNICODE #endif #include <stdio.h> #include <assert.h> #include <windows.h> #include <lm.h> int wmain(int argc, wchar_t *argv[]) { LPSESSION_INFO_10 pBuf = NULL; LPSESSION_INFO_10 pTmpBuf; DWORD dwLevel = 10; DWORD dwPrefMaxLen = -1; DWORD dwEntriesRead = 0; DWORD dwTotalEntries = 0; DWORD dwResumeHandle = 0; DWORD i; DWORD dwTotalCount = 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 NetSessionEnum function, specifying level 10. // do // begin do { nStatus = NetSessionEnum(pszServerName, pszClientName, pszUserName, dwLevel, (LPBYTE*)&pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle); // // If the call succeeds, // if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) { if ((pTmpBuf = pBuf) != NULL) { // // Loop through the entries. // for (i = 0; (i < dwEntriesRead); i++) { assert(pTmpBuf != NULL); if (pTmpBuf == NULL) { fprintf(stderr, "An access violation has occurred\n"); break; } // // Print the retrieved data. // wprintf(L"\n\tClient: %s\n", pTmpBuf->sesi10_cname); wprintf(L"\tUser: %s\n", pTmpBuf->sesi10_username); printf("\tActive: %d\n", pTmpBuf->sesi10_time); printf("\tIdle: %d\n", pTmpBuf->sesi10_idle_time); pTmpBuf++; dwTotalCount++; } } } // // 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); pBuf = NULL; } } // // Continue to call NetSessionEnum while // there are more entries. // while (nStatus == ERROR_MORE_DATA); // end do // Check again for an allocated buffer. // if (pBuf != NULL) NetApiBufferFree(pBuf); // // Print the final count of sessions enumerated. // fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount); 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.
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).
Network Management Overview, Network Management Functions, Session Functions, NetSessionGetInfo, SESSION_INFO_0, SESSION_INFO_1, SESSION_INFO_2, SESSION_INFO_10, SESSION_INFO_502, session_info_50