Jump to content

Network Drive connection doesnt work - (Moved)


Go to solution Solved by Jos,

Recommended Posts

Hello guys, 

I would like to create a script that connects a network drive after selecting the path and the drive letters that are still available. However, the script creates a drive although this drive already exists. It should give an error message. 

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>
#include <WinAPIFiles.au3>

Opt("MustDeclareVars", 0)

#Region ### START Koda GUI section ### Form=C:\DACHSER\Juliano\Form1.kxf
$GUI_Main = GUICreate("Form1", 620, 479, -1200, 400)
$btn_OK = GUICtrlCreateButton("OK", 217, 300, 185, 50, ($BS_DEFPUSHBUTTON))
$btn_Cancel = GUICtrlCreateButton("Cancel", 500, 420, 100, 40)
$Combo1 = GUICtrlCreateCombo("DriveLetter", 80, 55, 85, 20, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL, $WS_TABSTOP))
$input_NetPath = GUICtrlCreateInput("Path", 210, 55, 360, 20)
$cb_Persistent = GUICtrlCreateCheckbox("Verbindung bei Anmeldung wiederherstellen", 80, 180, 249, 17)
$cb_ExtraCredentials = GUICtrlCreateCheckbox("Verbindung mit anderen Anmeldeinformationen", 80, 210, 256, 17)
$Label1 = GUICtrlCreateLabel("Netzlaufwerk verbinden!", 20, 5, 250, 20)
GUICtrlSetFont($Label1, 12, 400, 0, "MS Sans Serif")
# Secondary GUI for Extracredentials
$GUI_Secondary = GUICreate("Form2", 417, 319, -1800, 490)
$btn_OK_Secondary = GUICtrlCreateButton("OK", 177, 270, 145, 30, ($BS_DEFPUSHBUTTON))
$btn_Cancel_Secondary = GUICtrlCreateButton("Cancel", 147, 230, 150, 30)
$Label2 = GUICtrlCreateLabel("Anmeldeinformationen eingeben!", 20, 5, 250, 20)
$Label3 = GUICtrlCreateLabel("Anmeldenamen eingeben!", 17, 105, 250, 20)
$input_Username = GUICtrlCreateInput("", 17, 120, 360, 20)
$input_Password = GUICtrlCreateInput("", 17, 200, 360, 20, $ES_PASSWORD)
$Label4 = GUICtrlCreateLabel("Passwort eingeben!", 17, 180, 250, 20)
GUICtrlSetFont($Label2, 12, 400, 0, "MS Sans Serif")
#EndRegion ### END Koda GUI section ###

$aFreeDriveList = _GetFreeDriveLetters()
_ArrayDelete($aFreeDriveList, 0)

GUICtrlSetData($Combo1, _ArrayToString($aFreeDriveList), "H:")

GUISetState(@SW_SHOW, $GUI_Main)

$iReturnExtraCredentials = GUICtrlRead($cb_ExtraCredentials)

While 1
    $MsgBox = GUIGetMsg()
    Switch $MsgBox
        Case $GUI_EVENT_CLOSE
            Exit

        Case $btn_Cancel_Secondary
            Exit

        Case $btn_Cancel
            Exit

        Case $btn_OK
            $iReturnExtraCredentials = GUICtrlRead($cb_ExtraCredentials)

            If $iReturnExtraCredentials == 1 Then
                GUISetState(@SW_SHOW, $GUI_Secondary)

            Else
                $input_Username = ""
                $input_Password = ""
                _Networkdrive()
            EndIf

        Case $btn_OK_Secondary
            _Networkdrive()

    EndSwitch
WEnd

$User = GUICtrlRead($input_Username)
$Password = GUICtrlRead($input_Password)

Func _Networkdrive()

    $iPersistent = 0
    $iReturnPersistent = GUICtrlRead($cb_Persistent)
    If $iReturnPersistent == 1 Then
        $iPersistent = 1
    EndIf

    $sNetpath = GUICtrlRead($input_NetPath)
    If $iReturnExtraCredentials And $btn_OK_Secondary == 1 Then
        $sDrive = DriveMapAdd(GUICtrlRead($Combo1), $sNetpath, $iPersistent, $User, $Password)
    Else
        $sDrive = DriveMapAdd(GUICtrlRead($Combo1), $sNetpath, $iPersistent)
    EndIf


    If $sDrive == 0 Then
        If @error == 1 Then
            $Errordefinition = "Unknown Error!"
        ElseIf @error == 2 Then
            $Errordefinition = "Access to the remote share was denied!"
        ElseIf @error == 3 Then
            $Errordefinition = "The device is already assigned!"
        ElseIf @error == 4 Then
            $Errordefinition = "Invalid device name!"
        ElseIf @error == 5 Then
            $Errordefinition = "Invalid remote share!"
        Else
            $Errordefinition = "Invalid password!"
        EndIf

        MsgBox($MB_TOPMOST + $MB_ICONERROR + $MB_OK, "Connect Network Drive", "The network drive could not connect successfully!" & @CRLF & "The Error is: " & $Errordefinition)
        GUISetState(@SW_ENABLE, $GUI_Main)

;~  ElseIf FileExists($sNetpath) Then
;~      $Errordefinition = "The device is already assigned!"
;~      MsgBox($MB_TOPMOST + $MB_ICONERROR + $MB_OK, "1Connect Network Drive", "The network drive could not connect successfully!" & @CRLF & "The Error is: " & $Errordefinition)
;~      GUISetState(@SW_ENABLE, $GUI_Main)
;~      DriveMapDel( GUICtrlRead($Combo1))

    ElseIf $sDrive == 1 Then
        MsgBox($MB_TOPMOST + $MB_ICONINFORMATION + $MB_OK, "Connect Network Drive", "The Network drive has sucessfully connected!")
        GUISetState(@SW_ENABLE, $GUI_Main)
        Exit
    Else
        MsgBox($MB_TOPMOST + $MB_ICONERROR + $MB_OK, "Connect Network Drive", "An Unexpected error occured!" & @CRLF & "Please contact the administrator!")
        GUISetState(@SW_ENABLE, $GUI_Main)
        Exit

    EndIf
EndFunc   ;==>_Networkdrive

Func _GetFreeDriveLetters()
    Dim $aArray[1]
    For $x = 67 To 90
        If DriveStatus(Chr($x) & ':\') = 'INVALID' Then
            ReDim $aArray[UBound($aArray) + 1]
            $aArray[UBound($aArray) - 1] = Chr($x) & ':'
        EndIf
    Next
    $aArray[0] = UBound($aArray) - 1
    Return ($aArray)
EndFunc   ;==>_GetFreeDriveLetters

Netzlaufwerk verbinden FINAL.au3

Edited by Jos
added with code
Link to comment
Share on other sites

  • Developers

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

For starters:  You are reading the $User and $Password at the wrong place!
What exactly is it mapping and how did you determine the drive already exists?

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Any reason why you start a new thread in German about the same topic as your previous one? 

This is an English forum, so when you like to discuss your AutoIt3 issues in German you can go to https://autoit.de/

Also removed the quoted "BUMP reply"... please don't.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers
33 minutes ago, DivineFaith said:

That´s right! In my script i use a combox where i select a free drive letter. But i want, that the script doesn't map der Networkpath a second time no matter which drive letter is selected. 

Again ... how are you determining that is is mapping it again as I do not believe that is the case?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Show me as it can't have the same drive letter.... or do you want to check whether the same network share was already mapped to another drive?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers
  • Solution

Here's a change to your script that will check whether the share is already mapped to another drive and give you the option to continue or stop

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <Array.au3>
#include <WinAPIFiles.au3>

Opt("MustDeclareVars", 0)

#Region ### START Koda GUI section ### Form=C:\DACHSER\Juliano\Form1.kxf
$GUI_Main = GUICreate("Form1", 620, 479, 1200, 400)
$btn_OK = GUICtrlCreateButton("OK", 217, 300, 185, 50, ($BS_DEFPUSHBUTTON))
$btn_Cancel = GUICtrlCreateButton("Cancel", 500, 420, 100, 40)
$Combo1 = GUICtrlCreateCombo("DriveLetter", 80, 55, 85, 20, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL, $WS_TABSTOP))
$input_NetPath = GUICtrlCreateInput("Path", 210, 55, 360, 20)
$cb_Persistent = GUICtrlCreateCheckbox("Verbindung bei Anmeldung wiederherstellen", 80, 180, 249, 17)
$cb_ExtraCredentials = GUICtrlCreateCheckbox("Verbindung mit anderen Anmeldeinformationen", 80, 210, 256, 17)
$Label1 = GUICtrlCreateLabel("Netzlaufwerk verbinden!", 20, 5, 250, 20)
GUICtrlSetFont($Label1, 12, 400, 0, "MS Sans Serif")
# Secondary GUI for Extracredentials
$GUI_Secondary = GUICreate("Form2", 417, 319, -1800, 490)
$btn_OK_Secondary = GUICtrlCreateButton("OK", 177, 270, 145, 30, ($BS_DEFPUSHBUTTON))
$btn_Cancel_Secondary = GUICtrlCreateButton("Cancel", 147, 230, 150, 30)
$Label2 = GUICtrlCreateLabel("Anmeldeinformationen eingeben!", 20, 5, 250, 20)
$Label3 = GUICtrlCreateLabel("Anmeldenamen eingeben!", 17, 105, 250, 20)
$input_Username = GUICtrlCreateInput("", 17, 120, 360, 20)
$input_Password = GUICtrlCreateInput("", 17, 200, 360, 20, $ES_PASSWORD)
$Label4 = GUICtrlCreateLabel("Passwort eingeben!", 17, 180, 250, 20)
GUICtrlSetFont($Label2, 12, 400, 0, "MS Sans Serif")
#EndRegion ### END Koda GUI section ###

$aFreeDriveList = _GetFreeDriveLetters()

GUICtrlSetData($Combo1, $aFreeDriveList[0][0], "H:")
;_ArrayDisplay($aFreeDriveList)
GUISetState(@SW_SHOW, $GUI_Main)

$iReturnExtraCredentials = GUICtrlRead($cb_ExtraCredentials)

While 1
    $MsgBox = GUIGetMsg()
    Switch $MsgBox
        Case $GUI_EVENT_CLOSE
            Exit

        Case $btn_Cancel_Secondary
            Exit

        Case $btn_Cancel
            Exit

        Case $btn_OK
            $iReturnExtraCredentials = GUICtrlRead($cb_ExtraCredentials)

            If $iReturnExtraCredentials == 1 Then
                GUISetState(@SW_SHOW, $GUI_Secondary)
            Else
                $User = ""
                $Password = ""
                _Networkdrive()
            EndIf

        Case $btn_OK_Secondary
            $User = GUICtrlRead($input_Username)
            $Password = GUICtrlRead($input_Password)
            _Networkdrive()

    EndSwitch
WEnd


Func _Networkdrive()

    $iPersistent = 0
    $iReturnPersistent = GUICtrlRead($cb_Persistent)
    If $iReturnPersistent == 1 Then
        $iPersistent = 1
    EndIf

    $sNetpath = GUICtrlRead($input_NetPath)

    For $x = 1 To 24
        If $sNetpath = $aFreeDriveList[$x][1] Then
            If MsgBox($MB_YESNO + $MB_TOPMOST + $MB_ICONWARNING, "Warning", "Share already mapped to " & _
                    $aFreeDriveList[$x][0] & @LF & "Do you want to continue?") <> $IDYES Then Return
        EndIf
    Next


    If $iReturnExtraCredentials And $btn_OK_Secondary == 1 Then
        $sDrive = DriveMapAdd(GUICtrlRead($Combo1), $sNetpath, $iPersistent, $User, $Password)
    Else
        $sDrive = DriveMapAdd(GUICtrlRead($Combo1), $sNetpath, $iPersistent)
    EndIf


    If $sDrive == 0 Then
        If @error == 1 Then
            $Errordefinition = "Unknown Error!"
        ElseIf @error == 2 Then
            $Errordefinition = "Access to the remote share was denied!"
        ElseIf @error == 3 Then
            $Errordefinition = "The device is already assigned!"
        ElseIf @error == 4 Then
            $Errordefinition = "Invalid device name!"
        ElseIf @error == 5 Then
            $Errordefinition = "Invalid remote share!"
        Else
            $Errordefinition = "Invalid password!"
        EndIf

        MsgBox($MB_TOPMOST + $MB_ICONERROR + $MB_OK, "Connect Network Drive", "The network drive could not connect successfully!" & @CRLF & "The Error is: " & $Errordefinition)
        GUISetState(@SW_ENABLE, $GUI_Main)

;~  ElseIf FileExists($sNetpath) Then
;~      $Errordefinition = "The device is already assigned!"
;~      MsgBox($MB_TOPMOST + $MB_ICONERROR + $MB_OK, "1Connect Network Drive", "The network drive could not connect successfully!" & @CRLF & "The Error is: " & $Errordefinition)
;~      GUISetState(@SW_ENABLE, $GUI_Main)
;~      DriveMapDel( GUICtrlRead($Combo1))

    ElseIf $sDrive == 1 Then
        MsgBox($MB_TOPMOST + $MB_ICONINFORMATION + $MB_OK, "Connect Network Drive", "The Network drive has sucessfully connected!")
        GUISetState(@SW_ENABLE, $GUI_Main)
        Exit
    Else
        MsgBox($MB_TOPMOST + $MB_ICONERROR + $MB_OK, "Connect Network Drive", "An Unexpected error occured!" & @CRLF & "Please contact the administrator!")
        GUISetState(@SW_ENABLE, $GUI_Main)
        Exit

    EndIf
EndFunc   ;==>_Networkdrive

Func _GetFreeDriveLetters()
    Dim $aArray[25][2]
    $dropdown = ""
    For $x = 67 To 90
        Local $drive = Chr($x) & ':'
        $aArray[$x - 66][0] = $drive
        $aArray[$x - 66][1] = DriveMapGet($drive)
        If $aArray[$x - 66][1] = "" Then
            $dropdown &= "|" & $drive
        EndIf
    Next
    $aArray[0][0] = $dropdown
    Return ($aArray)
EndFunc   ;==>_GetFreeDriveLetters

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...