Jump to content

RDP tool for multiple connections gone wrong


FMS
 Share

Recommended Posts

Hello,

At this moment I'm trying to make a RDP tool to make multiple connections and switch between them.
Curently I'm having troubles in making multiple connections and got some error's.
I know why I get the error's  (trying to change a active connection) but I'm not sure how to solve this.

Does somebody know a cleaver way to make it scalable into more connections in the same embeded window?
Also I'm open for sugestions for making this code better :)

Below the code I'm working on at the moment:
 

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <File.au3>
#Include <GuiComboBoxEx.au3>

Global $configfile = @ScriptDir & "\config.rto"
Global $configarray[0][0]
Global $RDP_id = False
Global $RDP_name = False
Global $RDP_ip = False
Global $RDP_usr = False
Global $RDP_dom = False
Global $RDP_pass = False

$Form1 = GUICreate("Form1",952, 675, -1, -1, $WS_OVERLAPPEDWINDOW + $WS_CLIPSIBLINGS + $WS_CLIPCHILDREN)
$Combo1 = GUICtrlCreateCombo("Combo1", 8, 8, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$Button1 = GUICtrlCreateButton("Connect", 160, 8, 75, 25)
$Button2 = GUICtrlCreateButton("Disconnect", 240, 8, 75, 25)
$Button3 = GUICtrlCreateButton("Button3", 320, 8, 75, 25)
$oRDP = ObjCreate("MsTscAx.MsTscAx.10")
$oRDP_Ctrl = GUICtrlCreateObj($oRDP, 64, 44, 800, 600)
GUICtrlSetResizing(-1, $GUI_DOCKALL)
GUICtrlSetStyle($oRDP_Ctrl , $WS_VISIBLE)
GUISetState(@SW_SHOW)
fetch_config()

While 1
   $nMsg = GUIGetMsg()
   Switch $nMsg
      Case $GUI_EVENT_CLOSE
         Exit
      Case $Combo1
         set_config(GUICtrlRead($Combo1))
      Case $Button1
         $oRDP.DesktopWidth = 800
         $oRDP.DesktopHeight = 600
         $oRDP.Fullscreen = False
         $oRDP.ColorDepth = 16
         $oRDP.AdvancedSettings3.SmartSizing = True
         $oRDP.Server = $RDP_ip
         $oRDP.UserName = $RDP_usr
         $oRDP.Domain = $RDP_dom
         $oRDP.AdvancedSettings2.ClearTextPassword = $RDP_pass
         $oRDP.ConnectingText = "Connecting to " & $RDP_ip
         $oRDP.DisconnectedText = "Disconnected from " & $RDP_ip
         $oRDP.StartConnected = True
         $oRDP.Connect()
      Case $Button2
         $oRDP.Disconnect()
      Case $Button3
         WinSetState($oRDP_Ctrl, "", @SW_HIDE)
   EndSwitch
WEnd


Func cmdkey()
   ;cmdkey /generic:TERMSRV/ip1 /user:dom1\usr1 /pass:pass1

EndFunc

Func set_config($selected = GUICtrlRead($Combo1))
   Local $found = False
   Local $found_id = False
   $RDP_id = False
   $RDP_name = False
   $RDP_ip = False
   $RDP_usr = False
   $RDP_dom = False
   $RDP_pass = False

   For $x = 1 To UBound($configarray,1) -1
      If $selected = $configarray[$x][1] Then
;~       ConsoleWrite ($configarray[$x][0]& @CRLF & $configarray[$x][1]& @CRLF & $configarray[$x][2]& @CRLF & $configarray[$x][3]& @CRLF & $configarray[$x][4]& @CRLF & $configarray[$x][5]& @CRLF)
         $found_id = $x
         $found = True
      EndIf
   Next

   If $found Then
      $RDP_id = $configarray[$found_id][0]
      $RDP_name = $configarray[$found_id][1]
      $RDP_ip = $configarray[$found_id][2]
      $RDP_usr = $configarray[$found_id][3]
      $RDP_dom = $configarray[$found_id][4]
      $RDP_pass = $configarray[$found_id][5]
      Return True
   Else
      Return False
   EndIf

EndFunc

Func fetch_config()
   If FileExists($configfile) Then
;~    ReDim $configarray[0][0]
      If _FileReadToArray($configfile, $configarray , 0 , "|") Then
         _GUICtrlComboBox_ResetContent($Combo1)
         _GUICtrlComboBox_BeginUpdate($Combo1)
;~       _GUICtrlComboBox_AddString($Combo1, "")
         For $i = 1 To UBound($configarray,1) -1
            _GUICtrlComboBox_AddString($Combo1, $configarray[$i][1])
         Next
         _GUICtrlComboBox_EndUpdate($Combo1)
         _GUICtrlComboBox_SetCurSel($Combo1, 0)
         If set_config(GUICtrlRead($Combo1)) Then
            Return True
         Else
            Return False
         EndIf
      Else
         Return False
      EndIf
   Else
      If Not _FileCreate($configfile) Then
         Return False
      Else
         Local $LC_fileSetTime = FileSetTime($configfile, @YEAR & @MON & @MDAY , $FT_CREATED)
         If $LC_fileSetTime = 0 Then
            Return False
         Else
            Local $set_rights = Run(@ComSpec & " /c icacls " & $configfile & " /grant Users:F" , "" , @SW_HIDE)
            If Not $set_rights Then
               Return False
            Else
               Local $file = FileOpen($configfile, 1)
               If $file = -1 Then
                  Return False
               Else
                  FileWrite($file, "ID|name|IP|username|domain|pass" & @CRLF)
                  FileWrite($file, "1|name1|ip1|name1|dom1|pass1" & @CRLF)
                  FileWrite($file, "2|name2|ip2|name2|dom2|pass2" & @CRLF)
               EndIf
               FileClose($file)
               fetch_config()
               Return True
            EndIf
         EndIf
      EndIf
   EndIf
EndFunc

 

edit :
error at reuse connection :
 

(51) : ==> The requested action with this object has failed.:
$oRDP.Connect()
$oRDP^ ERROR
>Exit code: 1    Time: 13.73

 

Edited by FMS

as finishing touch god created the dutch

Link to comment
Share on other sites

  • Moderators

@FMS I'm all for using AutoIt to resolve issues, but when Microsoft has an official product to do what you're trying to accomplish, why reinvent the wheel?

https://www.microsoft.com/en-us/download/details.aspx?id=44989

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

I've a little bit more code , ( also thanks to @guinness whit resizing the GUI here )

I hope somebody could help me whit 3 issue's in this code.

- The function "connect" doesn't seems the get the current tab id, whish i thought i could get this way?
- The object $RDP_obj_ctrl seems to replace whit the GUI but is there a way it stay's whitin the tab and resizes whit the tab?
- The buttons also are replaced when GUI is resized and could overlay the tab or each other, is there a simple way to work around this?

 

#include <GUIConstants.au3>
#include <GuiTab.au3>
#include <MenuConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <Array.au3>
#include <File.au3>
#Include <GuiComboBoxEx.au3>

Global Enum $CONTROLGETPOS_XPOS, $CONTROLGETPOS_YPOS, $CONTROLGETPOS_WIDTH, $CONTROLGETPOS_HEIGHT
globals()
create_gui()

Func create_gui()
   Local $hGUI = GUICreate('', @DesktopWidth, @DesktopHeight, 0, 0, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX))
   $button1 = GUICtrlCreateButton("Connect", 10, 10, 50, 20)
   $button2 = GUICtrlCreateButton("2", 70, 10, 50, 20)

   Local $iTab = GUICtrlCreateTab(10, 40, @DesktopWidth - 50 , @DesktopHeight - 50 , $TCS_FIXEDWIDTH) ; $TCS_FIXEDWIDTH is required.
   GUICtrlSetResizing($iTab, $GUI_DOCKLEFT + $GUI_DOCKRIGHT + $GUI_DOCKTOP + $GUI_DOCKBOTTOM)

   fetch_config()

   If $config_present Then
      Local $connection_count = UBound($configarray,1) -1
      ;~    _ArrayDisplay($RDP_obj)
      ;~    ConsoleWrite("$connection_count = " & $connection_count & @CRLF )
      ReDim $RDP_obj[$connection_count]
      ReDim $RDP_obj_ctrl[$connection_count]
      ;~    _ArrayDisplay($RDP_obj)
      Local $tab_holder = GUICtrlCreateTab(10, 40, 200, 100)
      For $x = 1 To $connection_count
         GUICtrlCreateTabItem($configarray[$x][1] )
         $RDP_obj[$x-1] = ObjCreate("MsTscAx.MsTscAx.10")
         $RDP_obj_ctrl[$x-1] = GUICtrlCreateObj($RDP_obj[$x-1], 80, 80, 20, 60)
      Next
      GUICtrlCreateTabItem("")
   EndIf

   ; Assign the handle and tab control.
   _Tab_SetWidth($hGUI, $iTab)

   GUIRegisterMsg($WM_GETMINMAXINFO, _Event_Proc)
   GUIRegisterMsg($WM_SYSCOMMAND, _Event_Proc)

   GUISetState(@SW_SHOW, $hGUI)

   While 1
      Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE
            ExitLoop
         Case $button1
            connect(_GUICtrlTab_GetItemText($tab_holder, _GUICtrlTab_GetCurSel($tab_holder)))
      EndSwitch
   WEnd

   GUIDelete($hGUI)
EndFunc

Func _Event_Proc($hWnd, $iMsg, $wParam, $lParam)
    If IsHWnd($hWnd) Then
        Switch $iMsg
            Case $WM_SYSCOMMAND
                Switch _WinAPI_LoWord($wParam)
                    Case $SC_MINIMIZE, $SC_MAXIMIZE, $SC_RESTORE, $SC_SIZE
                        AdlibRegister(_Tab_ResizeWidth, 10)

                EndSwitch

            Case $WM_GETMINMAXINFO
                AdlibRegister(_Tab_ResizeWidth, 10)

        EndSwitch
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_Event_Proc

Func _Tab_ResizeWidth()
    Return __Tab_SetWidth(Default, Default)
EndFunc   ;==>_Tab_ResizeWidth

Func _Tab_SetWidth($hWnd, $iTab)
    Return __Tab_SetWidth($hWnd, $iTab)
EndFunc   ;==>_Tab_SetWidth

Func __Tab_SetWidth($hWnd = Default, $iTab = Default)
    Local Static $hWnd_Static = 0, $iTab_Static = -9999
    If IsHWnd($hWnd) And IsInt($iTab) And $iTab > 0 Then
        $hWnd_Static = $hWnd
        $iTab_Static = $iTab
    ElseIf IsKeyword($hWnd) And IsKeyword($iTab) Then
        Local $aCtrlPosTab = ControlGetPos($hWnd_Static, '', $iTab_Static)
        If Not @error Then
            Local $iWidth = Floor($aCtrlPosTab[$CONTROLGETPOS_WIDTH] / _GUICtrlTab_GetItemCount($iTab_Static)) - 2 ; Deduct 2px for the left/right arrows.
            _GUICtrlTab_SetItemSize($iTab_Static, $iWidth, 0)
        EndIf
        AdlibUnRegister(_Tab_ResizeWidth)
    EndIf
    Return True
 EndFunc   ;==>__Tab_SetWidth

Func connect($sessionid)
   ConsoleWrite("connecting to : " & $sessionid & @CRLF)
EndFunc

Func fetch_config()
   $config_present = False
   If FileExists($configfile) Then
      If _FileReadToArray($configfile, $configarray , 0 , "|") Then
         $config_present = True
         Return True
      Else
         Return False
      EndIf
   Else
      If Not _FileCreate($configfile) Then
         Return False
      Else
         Local $LC_fileSetTime = FileSetTime($configfile, @YEAR & @MON & @MDAY , $FT_CREATED)
         If $LC_fileSetTime = 0 Then
            Return False
         Else
            Local $set_rights = Run(@ComSpec & " /c icacls " & $configfile & " /grant Users:F" , "" , @SW_HIDE)
            If Not $set_rights Then
               Return False
            Else
               Local $file = FileOpen($configfile, 1)
               If $file = -1 Then
                  Return False
               Else
                  FileWrite($file, "ID|name|IP|username|domain|pass" & @CRLF)
                  FileWrite($file, "1|name1|IP1|usr1|dom1|pass1" & @CRLF)
                  FileWrite($file, "2|name2|IP2|usr2|dom2|pass2" & @CRLF)
                  FileWrite($file, "3|name3|IP3|usr3|dom3|pass3" & @CRLF)
                  FileWrite($file, "4|name4|IP4|usr4|dom4|pass4" & @CRLF)
               EndIf
               FileClose($file)
               fetch_config()
               Return True
            EndIf
         EndIf
      EndIf
   EndIf
EndFunc

Func globals()
   Global $config_present = False
   Global $configfile = @ScriptDir & "\config.rto"
   Global $configarray[0][0]
   Global $RDP_id = False
   Global $RDP_name = False
   Global $RDP_ip = False
   Global $RDP_usr = False
   Global $RDP_dom = False
   Global $RDP_pass = False
   Global $RDP_obj[0]
   Global $RDP_obj_ctrl[0]
EndFunc

 

as finishing touch god created the dutch

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...