Windows API NetSessionEnum to list all sessions associated with a server or computer

Chris Kelleher

Administrator
Staff member
<BLOCKQUOTE><font size="1" face="Arial, Verdana">code:</font><HR><pre>
/*------------------------------------------------------------------------

File:

Description: This program will enumerate all sessions assocaiated with
a give server. It can be extended to support a specific
user and/or computer name.

Input Parameters:


Output Parameters:


Author: Todd G. Nist

Created: 6/8/99

------------------------------------------------------------------------*/


/************** SESSION_INFO_10 Structure ****************************
typedef struct _SESSION_INFO_10 {
LPWSTR sesi10_cname;
LPWSTR sesi10_username;
DWORD sesi10_time;
DWORD sesi10_idle_time;
} SESSION_INFO_10, *PSESSION_INFO_10, *LPSESSION_INFO_10;
Members
sesi10_cname
Pointer to a Unicode string specifying the name of the computer
that established the session.
sesi10_username
Pointer to a Unicode string specifying the name of the user who
established the session.
sesi10_time
Specifies the number of seconds a session has been active.
sesi10_idle_time
Specifies the number of seconds a session has been idle.
**********************************************************************/
&GLOB ERROR_MORE_DATA 234
&GLOB MAX_PREFERRED_LENGTH -1


PROCEDURE NetSessionEnum EXTERNAL "Netapi32.dll":
define input parameter lpwServerName as long. /* Pointer to a
Unicode string specifying the name of the remote server.*/
define input parameter lpwUncClientName as long. /* Pointer to a
Unicode string specifing the name of the computer session */
define input parameter lpwUserName as long. /* Pointer to a
Unicode string containing the user name. */
define input parameter dwLevel as long. /* Specifies the
information level of the data. (0,1,2,10) */
define output parameter lpBufPtr as long. /* Pointer to the
buffer that receives the data. Format depends on value of dwLevel */
/* This buffer is
allocated by the OS and must be deallocated via the NetApiFreeBuffer. */
define input parameter dwPerfMaxLen as long.
define output parameter uEntriesRead as long. /* Number of
entries read on this pass. */
define output parameter uTotalEntries as long. /* Total entries
available */
define input-output parameter dwResumeHandle as long. /* handle where
to resume, should be set to 0 initially */
define return parameter iRetCode as long.
END.

PROCEDURE NetApiBufferFree EXTERNAL "Netapi32.dll":
define input parameter lpServerInfo as long. /* pointer to a buffer
that was previously returned by another network mamagement function. */
define return parameter iRetCode as long.
END.

PROCEDURE WideCharToMultiByte EXTERNAL "KERNEL32.dll":
define input parameter uCodePage as long. /* code page
*/
define input parameter dwFlags as long. /* performance and
mapping flags */
define input parameter lpWideCharStr as long. /* address of
wide-character string */
define input parameter cbWideChar as long. /* number of
characters in string, if -1 is calculated on the fly */
define input parameter lpMultiByteStr as long. /* address of buffer
for new string */
define input parameter cbMultiByte as long. /* size of buffer */
define input parameter lpDefaultChar as long. /* address of default
for unmappable characters */
define input parameter lpUsedDefaultChar as long. /* address of flag set
when default char is used */
define return parameter iRetCode as long. /* if successful,
number of bytes written to the lpMultiByteStr buffer, else 0 */
END.

PROCEDURE MultiByteToWideChar EXTERNAL "KERNEL32.dll":
define input parameter uCodePage as long. /* code page
*/
define input parameter dwFlags as long. /* character-type
options */
define input parameter lpMultiByteStr as long. /* address of string
to map */
define input parameter cbMultiByteStr as long. /* number of bytes
in string */
define input parameter lpWideCharStr as long. /* address of
wide-character buffer */
define input parameter cbWideChar as long. /* size of buffer
*/
define return parameter iRetCode as long. /* if successful,
number of bytes written to lpWideCharStr, else 0 */
END.

define variable i as integer no-undo.
define variable iRetCode as integer no-undo.
define variable dwLevel as integer no-undo init 10.
define variable iEntriesRead as integer no-undo.
define variable iTotalEntries as integer no-undo.
define variable dwResumeHandle as integer no-undo init 0.
define variable iOffset as integer no-undo.
define variable iSessionInfo as integer no-undo.
define variable lpSessionInfo as memptr no-undo.
define variable lpSesi10_cname as memptr no-undo.
define variable lpSesi10_username as memptr no-undo.
define variable lpwServerName as memptr no-undo.
define variable lpServerName as memptr no-undo.

set-size(lpwServerName) = 512.
set-size(lpServerName) = 256.

put-string(lpServerName,1) = '\\DbServer'. /* <<<<<- your server name here
*/

/* convert server name to wide character (UNICODE) string */
run MultiByteToWideChar( input 0, /* use ANSI
code page */
input 0, /* flags
that specify the handling of unmapped characters */
input get-pointer-value(lpServerName), /* pointer
to wide-character string (unicode) */
input -1, /* count of
bytes of the wide-character string, if -1 calculate on the fly */
input get-pointer-value(lpwServerName), /* address
of buffer for new string */
input get-size(lpwServerName),
output iRetCode).

Run NetSessionEnum(
input get-pointer-value(lpwServername), /* Pointer to a Unicode string
specifying the name of the remote server.*/
input 0, /* Pointer to a Unicode string
specifing the name of the computer session */
input 0, /* Pointer to a Unicode string
containing the user name. */
input dwLevel, /* Specifies the information
level of the data. (0,1,2,10) */
output iSessionInfo, /* Pointer to the buffer that
receives the data. Format depends on value of dwLevel */
/* This buffer is allocated by
the OS and must be deallocated via the NetApiFreeBuffer. */
input {&MAX_PREFERRED_LENGTH},
output iEntriesRead, /* Number of entries read on
this pass. */
output iTotalEntries, /* Total entries available */
input-output dwResumeHandle, /* handle where to resume,
should be set to 0 initially */
output iRetCode).

message 'iRetCode:' iRetCode skip
'iEntriesRead: ' iEntriesRead skip
'iTotalEntries: ' iTotalEntries
view-as alert-box.


assign
set-pointer-value(lpSessionInfo) = iSessionInfo
iOffSet = 1
set-size(lpSesi10_cname) = 256
set-size(lpSesi10_username) = 256.

do i = 1 to iEntriesRead:

/* get the computer name out of the Session 10 structure */
run WideCharToMultiByte( input 0, /* use ANSI code page */
input 0, /* flags that specify the
handling of unmapped characters */
input get-long(lpSessionInfo, iOffset), /*
pointer to wide-character string (unicode) */
input -1, /* count of bytes of the
wide-character string, if -1 calculate on the fly */
input get-pointer-value(lpSesi10_cname), /*
address of buffer for new string */
input get-size(lpSesi10_cname),
input 0,
input 0,
output iRetCode).


/* get the username now */
run WideCharToMultiByte( input 0, /* use ANSI code page */
input 0, /* flags that specify the
handling of unmapped characters */
input get-long(lpSessionInfo, iOffset + 4), /*
pointer to wide-character string (unicode) */
input -1, /* count of bytes of the
wide-character string, if -1 calculate on the fly */
input get-pointer-value(lpSesi10_username), /*
address of buffer for new string */
input get-size(lpSesi10_username),
input 0,
input 0,
output iRetCode).

if iRetCode <> 0 then
message get-string(lpSesi10_cname,1)
get-string(lpSesi10_username,1)
view-as alert-box.

/* step through the array of session_info10 structures */
iOffset = iOffset + 16.

end.

/* free memory allocated by NetSessionEnum */
RUN NetApiBufferFree( input get-pointer-value(lpSessionInfo),
output iRetCode ).


/* deallocate memory */
assign
set-size(lpwServerName) = 0
set-size(lpServerName) = 0
set-size(lpSesi10_cname) = 0
set-size(lpSesi10_username) = 0.

[/code]
 
Top