TSExplorer.wbt

; TSExplorer.wbt
;
; Define some important constants
Title01 = 'Terminal Server Explorer'
Title01_About = 'About the "Terminal Server Explorer"'
; Find out who we are, where we are located and what our version is
AppFileSpec = IntControl(1004,0,0,0,0)
AppPath = FilePath(AppFileSpec)
AppRoot = FileRoot(AppFileSpec)
AppIniSpec = StrCat(AppPath,AppRoot,'.INI')
AppProductVer = FileVerInfo(AppFileSpec,'','ProductVersion')
AppFileVer = FileVerInfo(AppFileSpec,'','FileVersion')
; Get some configuration information from our .INI file
bDebug = IniReadPvt('CONFIG','DEBUG','0',AppIniSpec)
bDebugTrace = IniReadPvt('CONFIG','DEBUGTRACE','0',AppIniSpec)
TraceSpec = IniReadPvt('CONFIG','TRACEFILE',StrCat(AppPath,AppRoot,'.TRC'),AppIniSpec)
; Turn on debugging if necessary
If (bDebug) Then Debug(@ON)
; Turn on debug tracing if necessary
If (bDebugTrace) Then DebugTrace(@ON,TraceSpec)
;Load 32-bit or 64-bit extender
AddExtender('WWWNT34I.DLL' , 0, 'WWWNT64I.DLL' )
AddExtender('WWWTS44I.DLL' , 0, 'WWWTS64I.DLL' )
hVerBuf = wtsVersion(3)
nMajorVer = BinaryPeek4(hVerBuf,0)
nMinorVer = BinaryPeek4(hVerBuf,4)
nRelease = BinaryPeek4(hVerBuf,8)
nBuild = BinaryPeek4(hVerBuf,12)
hVerBuf = BinaryFree(hVerBuf)
AboutMsg = ''
AboutMsg = StrCat(AboutMsg,@CRLF,'wntVersion() = ',wntVersion())
AboutMsg = StrCat(AboutMsg,@CRLF,'wtsVersion(0) = ',wtsVersion(0))
AboutMsg = StrCat(AboutMsg,@CRLF,'wtsVersion(3) = ','[',nMajorVer,'][',nMinorVer,'][',nRelease,'][',nBuild,']')
AboutMsg = StrCat(AboutMsg,@CRLF,@CRLF,'Script File Spec = "',AppFileSpec,'"')
AboutMsg = StrCat(AboutMsg,@CRLF,'Script Product Version = ',AppProductVer)
AboutMsg = StrCat(AboutMsg,@CRLF,'Script File Version = ',AppFileVer)
AboutMsg = StrCat(AboutMsg,@CRLF,@CRLF,'TS API Present? = ',wtsIsTSEnabled(0))
AboutMsg = StrCat(AboutMsg,@CRLF,'TS Product Present? = ',wtsIsTSEnabled(1))
AboutMsg = StrCat(AboutMsg,@CRLF,'TS Virtual Channel Present? = ',wtsIsTSEnabled(2))
AboutMsg = StrCat(AboutMsg,@CRLF,'Citrix WFAPI Present? = ',wtsIsCitrixEnabled(0))
AboutMsg = StrCat(AboutMsg,@CRLF,'Citrix APSDK API Present? = ',wtsIsCitrixEnabled(1))
AboutMsg = StrCat(AboutMsg,@CRLF,'Citrix SMC API Present? = ',wtsIsCitrixEnabled(2))
AboutMsg = StrCat(AboutMsg,@CRLF,'Citrix Virtual Channel Present? = ',wtsIsCitrixEnabled(3))
;Message(Title01,AboutMsg)
; Determine if we can even be allowed to run on this system
bTSAPIPresent = wtsIsTSEnabled(0)
; Determine what our domain name is.  If we are in a work group, then set the
; domain to be an empty string.
WSDomainSID = wntLsaPolGet('','PrimaryDomain',2)
If (WSDomainSID == '')
  WSDomain = ''
Else
  WSDomain = wntLsaPolGet('','PrimaryDomain',1)
EndIf
SelectedServer = ''
PrevServer = ''
; Set our cancel handler...
IntControl(72,2,0,0,0)
; Only allow single item selections in list boxes...
IntControl(33,0,0,0,0)
; Declare the subroutine that processes our main dialog box.
;
; Also declare other constant values that are important for dialog processing...
PROCOPT_INITIALIZE = 0
PROCOPT_BUTTON_PUSH = 2
PROCOPT_EDITBOX_CHANGE = 5
PROCOPT_ITEM_SELECT = 7
PROCOPT_DROPLISTBOX_CHANGE = 8
BUTTON_CLOSE = 1
DROPLISTBOX_TSSERVERLIST = 2
BUTTON_REFRESH_SERVERS = 3
ITEMBOX_SESSLIST = 6
BUTTON_REFRESH_SESSIONS = 7
BUTTON_REFRESH_PROCESSES = 9
MULTILINEBOX_PROCLIST = 10
MULTILINEBOX_SESSINFO = 12
BUTTON_SEND_MESSAGE = 13
BUTTON_LOGOFF_SESSION = 14
BUTTON_DISCONNECT_SESSION = 15
EDITBOX_CONNSESSID = 17
EDITBOX_DOMAIN = 18
BUTTON_ABOUT = 20
EDITBOX_SERVER = 22
#DefineSubRoutine MainDlgProc(DialogHandle, EventCode, ControlNum, Res4, Res5)
If (bDebugTrace) Then DebugTrace(@ON,TraceSpec)
IntControl(72,2,0,0,0)
Switch (EventCode)
  Case PROCOPT_INITIALIZE
    DialogProcOptions(DialogHandle, PROCOPT_BUTTON_PUSH, 1)
    If (bTSAPIPresent)
      DialogProcOptions(DialogHandle, PROCOPT_DROPLISTBOX_CHANGE, 1)
      DialogProcOptions(DialogHandle, PROCOPT_ITEM_SELECT, 1)
;      if (!bAddGroups) then DialogControlState(DialogHandle,BUTTON_ADD_GROUPS,3,2)
      DialogControlSet(DialogHandle, EDITBOX_DOMAIN, 3, WSDomain)
      TSServerList = ListTSServers(WSDomain)
      DialogControlSet(DialogHandle, DROPLISTBOX_TSSERVERLIST, 5, TSServerList)
      SessList = ''
      DialogControlSet(DialogHandle, ITEMBOX_SESSLIST, 5, SessList)
      ProcList = ''
      DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
      SessInfo = ''
      DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
      ConnSessId = ''
      DialogControlSet(DialogHandle, EDITBOX_CONNSESSID, 3, ConnSessId)
      DialogControlSet(DialogHandle, EDITBOX_SERVER, 3, '')
    Else
      DialogControlSet(DialogHandle, EDITBOX_DOMAIN, 3, '')
      TSServerList = ''
      DialogControlSet(DialogHandle, DROPLISTBOX_TSSERVERLIST, 5, TSServerList)
      SessList = ''
      DialogControlSet(DialogHandle, ITEMBOX_SESSLIST, 5, SessList)
      ProcList = ''
      DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
      SessInfo = 'Error!  Terminal Services API is not present.'
      DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
      DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 13, '255|0|0')
      ConnSessId = ''
      DialogControlSet(DialogHandle, EDITBOX_CONNSESSID, 3, ConnSessId)
      DialogControlSet(DialogHandle, EDITBOX_SERVER, 3, '')
      DialogControlState(DialogHandle, DROPLISTBOX_TSSERVERLIST, 3, 2)
      DialogControlState(DialogHandle, ITEMBOX_SESSLIST, 3, 2)
      DialogControlState(DialogHandle, BUTTON_REFRESH_SERVERS, 3, 2)
      DialogControlState(DialogHandle, BUTTON_REFRESH_SESSIONS, 3, 2)
      DialogControlState(DialogHandle, BUTTON_REFRESH_PROCESSES, 3, 2)
      DialogControlState(DialogHandle, MULTILINEBOX_PROCLIST, 3, 2)
      DialogControlState(DialogHandle, BUTTON_SEND_MESSAGE, 3, 2)
      DialogControlState(DialogHandle, BUTTON_LOGOFF_SESSION, 3, 2)
      DialogControlState(DialogHandle, BUTTON_DISCONNECT_SESSION, 3, 2)
      DialogControlState(DialogHandle, EDITBOX_CONNSESSID, 3, 2)
      DialogControlState(DialogHandle, EDITBOX_DOMAIN, 3, 2)
      DialogControlState(DialogHandle, EDITBOX_SERVER, 3, 2)
    EndIf
    Break
  Case PROCOPT_ITEM_SELECT
    If (ControlNum == ITEMBOX_SESSLIST)
      DialogProcOptions(DialogHandle, 1000, 2) ; Put dialog into busy state...
      TempServer = DialogControlGet(DialogHandle, EDITBOX_SERVER, 3)
      TempSess = DialogControlGet(DialogHandle, ITEMBOX_SESSLIST, 6)
      If (TempServer != '')
        ErrorMode(@OFF)
        TempServerType = wntServerType(TempServer)
        ErrorMode(@CANCEL)
        If (TempServerType != 4)
          TempServer = ''
        EndIf
      EndIf
      If (TempServer == PrevServer)
        If (TempServer != '')
          SessInfo = GetTSSessInfo(TempServer,TempSess)
        Else
          TempSess = ''
        EndIf
        DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
        If ((TempServer != '') && (TempSess != ''))
          ProcList = ListTSProcesses(TempServer,TempSess)
        Else
          ProcList = ''
        EndIf
        DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
      Else
        GoSub RefreshServers
      EndIf
      DialogProcOptions(DialogHandle, 1000, 0) ; Restore dialog to active state...
      Return -2 ; Don't terminate the dialog
    EndIf
    Break
  Case PROCOPT_DROPLISTBOX_CHANGE
    If (ControlNum == DROPLISTBOX_TSSERVERLIST)
      TempServer = DialogControlGet(DialogHandle, DROPLISTBOX_TSSERVERLIST, 6)
      DialogControlSet(DialogHandle, EDITBOX_SERVER, 3, TempServer)
      PrevServer = TempServer
      DialogProcOptions(DialogHandle, 1000, 0) ; Restore dialog to active state...
      If (TempServer != '')
        DialogProcOptions(DialogHandle, 1000, 2) ; Put dialog into busy state...
        SessList = ListTSSessions(TempServer)
        DialogControlSet(DialogHandle, ITEMBOX_SESSLIST, 5, SessList)
        ProcList = ''
        DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
        SessInfo = ''
        DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
        ConnSessId = GetTSConnSessId(TempServer)
        DialogControlSet(DialogHandle, EDITBOX_CONNSESSID, 3, ConnSessId)
      Else
        SessList = ''
        DialogControlSet(DialogHandle, ITEMBOX_SESSLIST, 5, SessList)
        ProcList = ''
        DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
        SessInfo = ''
        DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
        ConnSessId = ''
        DialogControlSet(DialogHandle, EDITBOX_CONNSESSID, 3, ConnSessId)
      EndIf
      DialogControlState(DialogHandle, DROPLISTBOX_TSSERVERLIST, 1, 0)
      Return -2 ; Don't terminate the dialog
    EndIf
    Break
  Case PROCOPT_BUTTON_PUSH
    If  (ControlNum == BUTTON_CLOSE)
      Return -1
    EndIf
    If (ControlNum == BUTTON_ABOUT)
      Message(Title01_About,AboutMsg)
      Return -2 ; Don't terminate the dialog
    EndIf
    If (ControlNum == BUTTON_REFRESH_SERVERS)
      DialogProcOptions(DialogHandle, 1000, 2) ; Put dialog into busy state...
      GoSub RefreshServers
      DialogProcOptions(DialogHandle, 1000, 0) ; Restore dialog to active state...
      Return -2 ; Don't terminate the dialog
    EndIf
    If (ControlNum == BUTTON_REFRESH_SESSIONS)
      DialogProcOptions(DialogHandle, 1000, 2) ; Put dialog into busy state...
      TempServer = DialogControlGet(DialogHandle, EDITBOX_SERVER, 3)
      TempServer2 = DialogControlGet(DialogHandle, DROPLISTBOX_TSSERVERLIST, 6)
      If (TempServer2 != TempServer)
        DialogControlSet(DialogHandle, DROPLISTBOX_TSSERVERLIST, 6, '')
      EndIf
      ; Make sure that the server name is for a server that really exists
      ; before we try to get information about the sessions on it.  This
      ; test is necessary because the user might have manually typed in a
      ; server name into an editbox instead of using a server name that
      ; was selected from the list of known terminal server systems.
      If (TempServer != '')
        ErrorMode(@OFF)
        TempServerType = wntServerType(TempServer)
        ErrorMode(@CANCEL)
        If (TempServerType == 4)
          SessList = ListTSSessions(TempServer)
          PrevServer = TempServer
        Else
          SessList = ''
        EndIf
      Else
        SessList = ''
      EndIf
      DialogControlSet(DialogHandle, ITEMBOX_SESSLIST, 5, SessList)
      ProcList = ''
      DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
      SessInfo = ''
      DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
      ConnSessId = GetTSConnSessId(TempServer)
      DialogControlSet(DialogHandle, EDITBOX_CONNSESSID, 3, ConnSessId)
      DialogProcOptions(DialogHandle, 1000, 0) ; Restore dialog to active state...
      Return -2 ; Don't terminate the dialog
    EndIf
    If (ControlNum == BUTTON_REFRESH_PROCESSES)
      DialogProcOptions(DialogHandle, 1000, 2) ; Put dialog into busy state...
      TempServer = DialogControlGet(DialogHandle, EDITBOX_SERVER, 3)
      TempSess = DialogControlGet(DialogHandle, ITEMBOX_SESSLIST, 6)
      ; Make sure that the server name is for a server that really exists
      ; before we try to get information about the sessions on it.  This
      ; test is necessary because the user might have manually typed in a
      ; server name into an editbox instead of using a server name that
      ; was selected from the list of known terminal server systems.
      If (TempServer != '')
        ErrorMode(@OFF)
        TempServerType = wntServerType(TempServer)
        ErrorMode(@CANCEL)
        If (TempServerType != 4)
          TempServer = ''
        EndIf
      EndIf
      If (TempServer == PrevServer)
        If ((TempServer != '') && (TempSess != ''))
          ProcList = ListTSProcesses(TempServer,TempSess)
        Else
          ProcList = ''
        EndIf
        DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
      Else
        GoSub RefreshServers
      EndIf
      DialogProcOptions(DialogHandle, 1000, 0) ; Restore dialog to active state...
      Return -2 ; Don't terminate the dialog
    EndIf
    If (ControlNum == BUTTON_SEND_MESSAGE)
      DialogProcOptions(DialogHandle, 1000, 2) ; Put dialog into busy state...
      TempServer = DialogControlGet(DialogHandle, EDITBOX_SERVER, 3)
      TempSess = DialogControlGet(DialogHandle, ITEMBOX_SESSLIST, 6)
      If (TempServer != '')
        ErrorMode(@OFF)
        TempServerType = wntServerType(TempServer)
        ErrorMode(@CANCEL)
        If (TempServerType != 4)
          TempServer = ''
        EndIf
      EndIf
      If (TempServer == PrevServer)
        If ((TempServer != '') && (TempSess != ''))
          SendMsgButtons = 1
          SendMsgIcon = 65536
          SendMsgDefBtn = 65536
          If (Dialog('SendMsgDlg') != 0)
            SendMsgButtons = SendMsgButtons - 1
            If (SendMsgIcon == 65536) Then SendMsgIcon = 0
            If (SendMsgDefBtn == 65536) Then SendMsgDefBtn = 0
            SendMsgStyle = (SendMsgButtons | SendMsgIcon | SendMsgDefBtn)
            ErrorMode(@OFF)
            Result = wtsSendMessage(TempServer,TempSess,SendMsgTitle,SendMsgText,SendMsgStyle,SendMsgTimeout,SendMsgWaitFlag)
            RC = LastError()
            ErrorMode(@CANCEL)
            TempMsg = StrCat('wtsSendMessage(...)  RC = ',RC)
            TempMsg = StrCat(TempMsg,@CRLF,@CRLF,'Result = ',Result)
            Message(Title01,TempMsg)
          EndIf
        EndIf
      Else
        GoSub RefreshServers
      EndIf
      DialogProcOptions(DialogHandle, 1000, 0) ; Restore dialog to active state...
      Return -2 ; Don't terminate the dialog
    EndIf
    If (ControlNum == BUTTON_LOGOFF_SESSION)
      DialogProcOptions(DialogHandle, 1000, 2) ; Put dialog into busy state...
      TempServer = DialogControlGet(DialogHandle, EDITBOX_SERVER, 3)
      TempSess = DialogControlGet(DialogHandle, ITEMBOX_SESSLIST, 6)
      If (TempServer == PrevServer)
        If (TempServer != '')
          ErrorMode(@OFF)
          TempServerType = wntServerType(TempServer)
          ErrorMode(@CANCEL)
          If (TempServerType != 4)
            TempServer = ''
          EndIf
        EndIf
        If ((TempServer != '') && (TempSess != ''))
          ErrorMode(@OFF)
          Result = wtsLogoffSession(TempServer,TempSess,@TRUE)
          RC = LastError()
          ErrorMode(@CANCEL)
          TempMsg = StrCat('wtsLogoffSession("',TempServer,'",',TempSess,',@TRUE) RC = ',RC)
          TempMsg = StrCat(TempMsg,@CRLF,@CRLF,'Result = ',Result)
          Message(Title01,TempMsg)
          SessList = ListTSSessions(TempServer)
          DialogControlSet(DialogHandle, ITEMBOX_SESSLIST, 5, SessList)
          ProcList = ''
          DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
          SessInfo = ''
          DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
          ConnSessId = GetTSConnSessId(TempServer)
          DialogControlSet(DialogHandle, EDITBOX_CONNSESSID, 3, ConnSessId)
        EndIf
      Else
        GoSub RefreshServers
      EndIf
      DialogProcOptions(DialogHandle, 1000, 0) ; Restore dialog to active state...
      Return -2 ; Don't terminate the dialog
    EndIf
    If (ControlNum == BUTTON_DISCONNECT_SESSION)
      DialogProcOptions(DialogHandle, 1000, 2) ; Put dialog into busy state...
      TempServer = DialogControlGet(DialogHandle, DROPLISTBOX_TSSERVERLIST, 6)
      TempSess = DialogControlGet(DialogHandle, ITEMBOX_SESSLIST, 6)
      If (TempServer != '')
        ErrorMode(@OFF)
        TempServerType = wntServerType(TempServer)
        ErrorMode(@CANCEL)
        If (TempServerType != 4)
          TempServer = ''
        EndIf
      EndIf
      If (TempServer == PrevServer)
        If ((TempServer != '') && (TempSess != ''))
          ErrorMode(@OFF)
          Result = wtsDisconnectSession(TempServer,TempSess,@TRUE)
          RC = LastError()
          ErrorMode(@CANCEL)
          TempMsg = StrCat('wtsDisconnectSession("',TempServer,'",',TempSess,',@TRUE) RC = ',RC)
          TempMsg = StrCat(TempMsg,@CRLF,@CRLF,'Result = ',Result)
          Message(Title01,TempMsg)
          SessInfo = GetTSSessInfo(TempServer,TempSess)
          DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
          ProcList = ListTSProcesses(TempServer,TempSess)
          DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
          ConnSessId = GetTSConnSessId(TempServer)
          DialogControlSet(DialogHandle, EDITBOX_CONNSESSID, 3, ConnSessId)
        EndIf
      Else
        GoSub RefreshServers
      EndIf
      DialogProcOptions(DialogHandle, 1000, 0) ; Restore dialog to active state...
      Return -2 ; Don't terminate the dialog
    EndIf
    Break
EndSwitch
Return -1
:CANCEL
IntControl(72,2,0,0,0)
Return
:RefreshServers
WSDomain = DialogControlGet(DialogHandle, EDITBOX_DOMAIN, 3)
TSServerList = ListTSServers(WSDomain)
DialogControlSet(DialogHandle, DROPLISTBOX_TSSERVERLIST, 5, TSServerList)
SessList = ''
DialogControlSet(DialogHandle, ITEMBOX_SESSLIST, 5, SessList)
ProcList = ''
DialogControlSet(DialogHandle, MULTILINEBOX_PROCLIST, 3, ProcList)
SessInfo = ''
DialogControlSet(DialogHandle, MULTILINEBOX_SESSINFO, 3, SessInfo)
ConnSessId = ''
DialogControlSet(DialogHandle, EDITBOX_CONNSESSID, 3, ConnSessId)
DialogControlSet(DialogHandle, EDITBOX_SERVER, 3, '')
Return
#EndSubRoutine
; Define the main dialog box
TSExpMainDlgFormat=`WWWDLGED,6.1`
TSExpMainDlgCaption=`Terminal Server Explorer`
TSExpMainDlgX=-01
TSExpMainDlgY=-01
TSExpMainDlgWidth=368
TSExpMainDlgHeight=212
TSExpMainDlgNumControls=022
TSExpMainDlgProcedure=`MainDlgProc`
TSExpMainDlgFont=`DEFAULT`
TSExpMainDlgTextColor=`DEFAULT`
TSExpMainDlgBackground=`DEFAULT,DEFAULT`
TSExpMainDlgConfig=2
TSExpMainDlg001=`004,196,065,011,PUSHBUTTON,DEFAULT,"&Close",1,1,32,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg002=`004,034,065,074,DROPLISTBOX,TSServerList,DEFAULT,DEFAULT,3,4,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg003=`004,154,065,011,PUSHBUTTON,DEFAULT,"&Refresh Servers",2,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg004=`004,018,065,011,STATICTEXT,DEFAULT,"Terminal Server List:",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg005=`004,050,065,011,STATICTEXT,DEFAULT,"Session List",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg006=`004,066,065,043,ITEMBOX,SessList,DEFAULT,DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg007=`004,167,065,011,PUSHBUTTON,DEFAULT,"Refresh &Sessions",3,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg008=`105,106,030,011,STATICTEXT,DEFAULT,"Process List",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg009=`004,182,065,009,PUSHBUTTON,DEFAULT,"Refresh &Processes",4,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg010=`143,106,216,101,MULTILINEBOX,ProcList,DEFAULT,DEFAULT,14,8,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg011=`105,004,030,011,STATICTEXT,DEFAULT,"Session Info",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg012=`143,004,216,099,MULTILINEBOX,SessInfo,DEFAULT,DEFAULT,13,8,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg013=`071,154,064,011,PUSHBUTTON,DEFAULT,"Send &Message",5,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg014=`071,167,064,011,PUSHBUTTON,DEFAULT,"&Logoff Session",6,9,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg015=`071,182,064,009,PUSHBUTTON,DEFAULT,"&Disconnect Session",7,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg016=`004,114,048,011,STATICTEXT,DEFAULT,"Console Session Id",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg017=`006,127,034,011,EDITBOX,ConnSessId,DEFAULT,DEFAULT,12,8,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg018=`042,004,058,011,EDITBOX,WSDomain,DEFAULT,DEFAULT,2,0,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg019=`004,004,035,011,STATICTEXT,DEFAULT,"Domain Name",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg020=`071,196,064,011,PUSHBUTTON,DEFAULT,"&About",8,11,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg021=`073,018,041,011,STATICTEXT,DEFAULT,"Selected Server",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TSExpMainDlg022=`074,034,062,011,EDITBOX,SelectedServer,DEFAULT,DEFAULT,22,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
ButtonPushed=Dialog("TSExpMainDlg",0) ; WinBatch will not display this dialog.
; Define the dialog box used to send messages...
SendMsgDlgFormat=`WWWDLGED,6.1`
SendMsgDlgCaption=`Send a message to a terminal server session`
SendMsgDlgX=-01
SendMsgDlgY=-01
SendMsgDlgWidth=312
SendMsgDlgHeight=224
SendMsgDlgNumControls=026
SendMsgDlgProcedure=`DEFAULT`
SendMsgDlgFont=`DEFAULT`
SendMsgDlgTextColor=`DEFAULT`
SendMsgDlgBackground=`DEFAULT,DEFAULT`
SendMsgDlg001=`003,147,036,012,PUSHBUTTON,DEFAULT,"&Send",1,1,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg002=`003,161,036,012,PUSHBUTTON,DEFAULT,"Cancel",0,23,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg003=`003,003,044,012,STATICTEXT,DEFAULT,"Message Title",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg004=`003,021,044,012,STATICTEXT,DEFAULT,"Message Text",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg005=`051,003,256,012,EDITBOX,SendMsgTitle,DEFAULT,DEFAULT,2,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg006=`051,021,256,060,MULTILINEBOX,SendMsgText,DEFAULT,DEFAULT,3,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg007=`053,087,076,086,GROUPBOX,DEFAULT,"Buttons",DEFAULT,4,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg008=`133,087,076,086,GROUPBOX,DEFAULT,"Icons",DEFAULT,11,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg009=`213,087,074,086,GROUPBOX,DEFAULT,"Default Button",DEFAULT,17,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg010=`059,097,018,012,RADIOBUTTON,SendMsgButtons,"OK",1,5,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg011=`059,109,048,012,RADIOBUTTON,SendMsgButtons,"OK / Cancel",2,6,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg012=`059,121,066,012,RADIOBUTTON,SendMsgButtons,"Aborty / Retry / Ignore",3,7,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg013=`059,133,056,012,RADIOBUTTON,SendMsgButtons,"Yes / No / Cancel",4,8,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg014=`059,145,034,012,RADIOBUTTON,SendMsgButtons,"Yes / No",5,9,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg015=`059,157,046,012,RADIOBUTTON,SendMsgButtons,"Retry / Cancel",6,10,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg016=`139,097,044,012,RADIOBUTTON,SendMsgIcon,"None",65536,12,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg017=`139,109,044,012,RADIOBUTTON,SendMsgIcon,"Hand",16,13,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg018=`139,121,044,012,RADIOBUTTON,SendMsgIcon,"Question mark",32,14,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg019=`139,133,052,012,RADIOBUTTON,SendMsgIcon,"Exclamation point",48,15,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg020=`139,145,044,012,RADIOBUTTON,SendMsgIcon,"Asterisk",64,16,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg021=`219,109,044,012,RADIOBUTTON,SendMsgDefBtn,"Button 2",256,19,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg022=`219,097,044,012,RADIOBUTTON,SendMsgDefBtn,"Button 1",65536,18,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg023=`219,121,044,012,RADIOBUTTON,SendMsgDefBtn,"Button 3",512,20,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg024=`053,181,036,008,STATICTEXT,DEFAULT,"Timeout Value",DEFAULT,99,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg025=`093,179,024,012,SPINNER,SendMsgTimeout,"0",DEFAULT,21,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
SendMsgDlg026=`053,195,066,012,CHECKBOX,SendMsgWaitFlag,"Wait for user response?",1,22,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
ButtonPushed=Dialog("SendMsgDlg",0) ; WinBatch will not display this dialog.
; Declare other important subroutines...
#DefineFunction ListTSServers(DomainName)
  ErrorMode(@OFF)
  ServerList = wntServerList('',DomainName,33554432)
  RC = LastError()
  ErrorMode(@CANCEL)
  If (RC != 0)
    TempMsg = StrCat('wntServerList("","',DomainName,'",33554432)  RC = ',RC)
    TempMsg = StrCat(TempMsg,@CRLF,@CRLF,'Error!  Unable to obtain a list of terminal server systems.')
    Message('ListTSServers()',TempMsg)
    Return ''
  EndIf
  Return ServerList
#EndFunction
#DefineFunction ListTSSessions(ServerSpec)
  ErrorMode(@OFF)
  SessionArr = wtsEnumSessions(ServerSpec)
  RC = LastError()
  ErrorMode(@CANCEL)
  If (RC != 0)
    TempMsg = StrCat('wntEnumSessions("',ServerSpec,'")  RC = ',RC)
    TempMsg = StrCat(TempMsg,@CRLF,@CRLF,'Error!  Unable to obtain a list of terminal server sessions.')
    Message('ListTSSessions',TempMsg)
    Return ''
  EndIf
  Return SessionArr[1]
#EndFunction
#DefineFunction ListTSProcesses(ServerSpec,SessId)
  ErrorMode(@OFF)
  ProcessArr = wtsEnumProcesses(ServerSpec,SessId)
  RC = LastError()
  ErrorMode(@CANCEL)
  If (RC != 0)
    TempMsg = StrCat('wntEnumProcesses("',ServerSpec,'",',SessId,')  RC = ',RC)
    TempMsg = StrCat(TempMsg,@CRLF,@CRLF,'Error!  Unable to obtain a list of terminal server processes.')
    Message('ListTSProcesses()',TempMsg)
    Return ''
  EndIf
;  TempMsg = StrCat('Total processes for Session #',SessId,' is ',ProcessArr[0],'.')
;  Message('ListTSProcesses()',TempMsg)
  ProcList = ''
  ValueDelim = ''
  FieldDelim = StrCat(@TAB,@TAB)
  For i = 1 To ProcessArr[0]
    If (i > 1) Then ValueDelim = @CRLF
    ProcList = StrCat(ProcList,ValueDelim,ItemExtract(i,ProcessArr[1],@TAB),FieldDelim,'"',ItemExtract(i,ProcessArr[2],@TAB),'"')
  Next
  Return ProcList
#EndFunction
#DefineFunction GetTSSessInfo(ServerSpec,SessId)
  ErrorMode(@OFF)
  SessInfoArr = wtsQuerySessionInfo(ServerSpec,SessId)
  RC = LastError()
  ErrorMode(@CANCEL)
  If (RC != 0)
    TempMsg = StrCat('wntQuerySessionInfo("',ServerSpec,'",',SessId,')  RC = ',RC)
    TempMsg = StrCat(TempMsg,@CRLF,@CRLF,'Error!  Unable to obtain session information.')
    Message('GetTSSessInfo()',TempMsg)
    Return ''
  EndIf
  SessInfo = StrCat(               'Session Name  = ',SessInfoArr[0])
  SessInfo = StrCat(SessInfo,@CRLF,'Connection State  = ',SessInfoArr[1])
  SessInfo = StrCat(SessInfo,@CRLF,'Client Address  = ',SessInfoArr[2])
  SessInfo = StrCat(SessInfo,@CRLF,'Client Name  = ',SessInfoArr[3])
  SessInfo = StrCat(SessInfo,@CRLF,'Client Protocol  = ',SessInfoArr[4])
  SessInfo = StrCat(SessInfo,@CRLF,'Client Hardware Id  = ',SessInfoArr[5])
  SessInfo = StrCat(SessInfo,@CRLF,'Client Product Id  = ',SessInfoArr[6])
  SessInfo = StrCat(SessInfo,@CRLF,'Client Build #  = ',SessInfoArr[7])
  SessInfo = StrCat(SessInfo,@CRLF,'Client Directory  = ',SessInfoArr[8])
  SessInfo = StrCat(SessInfo,@CRLF,'Client horizontal resolution  = ',SessInfoArr[9])
  SessInfo = StrCat(SessInfo,@CRLF,'Client vertical resolution  = ',SessInfoArr[10])
  SessInfo = StrCat(SessInfo,@CRLF,'Client color depth  = ',SessInfoArr[11])
  SessInfo = StrCat(SessInfo,@CRLF,'OEM Id string  = ',SessInfoArr[12])
  SessInfo = StrCat(SessInfo,@CRLF,'Domain name  = ',SessInfoArr[13])
  SessInfo = StrCat(SessInfo,@CRLF,'Username  = ',SessInfoArr[14])
  SessInfo = StrCat(SessInfo,@CRLF,'Application name  = ',SessInfoArr[15])
  SessInfo = StrCat(SessInfo,@CRLF,'Initial program  = ',SessInfoArr[16])
  SessInfo = StrCat(SessInfo,@CRLF,'Working directory  = ',SessInfoArr[17])
  SessInfo = StrCat(SessInfo,@CRLF,'Current Time [GMT]  = ',SessInfoArr[18])
  SessInfo = StrCat(SessInfo,@CRLF,'Login Time [GMT]  = ',SessInfoArr[19])
  SessInfo = StrCat(SessInfo,@CRLF,'Last Input Time [GMT]  = ',SessInfoArr[20])
  SessInfo = StrCat(SessInfo,@CRLF,'Connect Time [GMT]  = ',SessInfoArr[21])
  SessInfo = StrCat(SessInfo,@CRLF,'Disconnect Time [GMT]  = ',SessInfoArr[22])
  Return SessInfo
#EndFunction
#DefineFunction GetTSConnSessId(ServerSpec)
  TempServer1 = StrUpper(StrReplace(ServerSpec,'\',''))
  TempServer2 = StrUpper(Environment('COMPUTERNAME'))
  If ((TempServer1 == TempServer2) || (TempServer1 == ''))
    ErrorMode(@OFF)
    SessId = wtsGetActiveConsoleSessId()
    RC = LastError()
    ErrorMode(@CANCEL)
    If (RC != 0)
      TempMsg = StrCat('wntGetActiveConsoleSessId()  RC = ',RC)
      TempMsg = StrCat(TempMsg,@CRLF,@CRLF,'Error!  Unable to obtain active console session id #.')
      Message('GetTSConnSessId()',TempMsg)
      Return ''
    EndIf
  Else
    SessId = ''
  EndIf
  Return SessId
#EndFunction
; Display the main dialog.  We will stay inside the dialog until the 'Close' button has
; been clicked, at which time this function will return and then we'll simply exit from
; the script.
ButtonPushed=Dialog("TSExpMainDlg")
:Done
Exit
:CANCEL
IntControl(72,2,0,0,0)
Return