# Example 2.25: Displaying Terminal Server session information.
# ----------------------------------------
# From "Win32 Perl Programming: The Standard Extensions Second Edition" by Dave Roth
# Published by Macmillan Technical Publishing.
# ISBN # 1-57870-216-X
print "From the book 'Win32 Perl Programming: The Standard Extensions, Second Edition'\nby Dave Roth\n\n";
use Win32::Lanman;
my %STATES = (
&WTSActive => "User has logged on",
&WTSConnected => "Connected",
&WTSConnectQuery => "Connecting",
&WTSShadow => "Shadowing",
&WTSDisconnected => "Client disconnected",
&WTSIdle => "Waiting for client to reconnect",
&WTSListen => "Waiting for client to logon",
&WTSReset => "Resetting",
&WTSDown => "Down due to error",
&WTSInit => "Initializing"
);
my @QUERY = (
WTSApplicationName,
WTSClientAddress,
WTSClientBuildNumber,
WTSClientDirectory,
WTSClientDisplay,
WTSClientHardwareId,
WTSClientName,
WTSClientProductId,
WTSConnectState,
WTSDomainName,
WTSInitialProgram,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSWorkingDirectory
);
my $Server = shift @ARGV || "";
my @SessionList;
Win32::Lanman::WTSEnumerateSessions( $Server, \@SessionList ) || die ;
foreach my $Session ( @SessionList )
{
my %Info;
print "\n", "-" x 30, "\n";
print "Session: $Session->{id}\n";
print "State: $STATES{$Session->{state}}\n";
print "WinStation: $Session->{winstationname}\n";
next if( $Session->{state} == WTSListen );
if( Win32::Lanman::WTSQuerySessionInformation( $Server, $Session->{id}, \@QUERY, \%Info ) )
{
my( $Net, $IPStruct ) = unpack( "La*", $Info{clientaddress} );
my( $IP ) = join( ".", (unpack( "C*", $IPStruct ))[2..5] ) if( 2 == $Net );
my( $DisplayX,
$DisplayY,
$DisplayColors ) = unpack( "L3", $Info{clientdisplay} );
$DisplayColors <<= 2;
print "\tClient IP: $IP\n";
print "\tClient Display: $DisplayX x $DisplayY ($DisplayColors bits of color)\n";
foreach my $Key ( sort( keys( %Info ) ) )
{
print "\t$Key: $Info{$Key}\n";
}
}
}
sub Error()
{
print Win32::FormatMessage( Win32::Lanman::GetLastError() );
}