Mapper un disque réseau

Résolu
Nicolas H. Messages postés 36 Date d'inscription vendredi 19 octobre 2001 Statut Membre Dernière intervention 26 mai 2016 - Modifié par Nicolas H. le 25/04/2015 à 20:43
Nicolas H. Messages postés 36 Date d'inscription vendredi 19 octobre 2001 Statut Membre Dernière intervention 26 mai 2016 - 28 avril 2015 à 22:23
Bonjour,

J'ai un problème avec le mappage d'un disque réseau par mon application.

Je passe les infos de la lettre et du chemin en CLI. Les fonctions me retourne un True mais rien de visible dans l'explorer.

Quand je supprime le CLI aucun problème.

Voici le code :

    <DllImport("mpr.dll", EntryPoint:="WNetAddConnection2A")> _
    Public Shared Function WNetAddConnection2(ByRef lpNetResource As NetResource, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
    End Function

    <DllImport("mpr.dll", EntryPoint:="WNetCancelConnection2A")> _
    Public Shared Function WNetCancelConnection2(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
    End Function

    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure

    Public Const ForceDisconnect As Integer = 1
    Public Const RESOURCETYPE_DISK As Integer = &H1

    Friend bMapDrive As Boolean = False

    Friend sDriveLetter As String
    Friend sProjectFolder As String

    Dim aValidDriveLetter() As String = {"A", "B", "C", "D", "E", _
                                       "F", "G", "H", "I", "J", _
                                       "K", "L", "M", "N", "O", _
                                       "P", "Q", "R", "S", "T", _
                                       "U", "V", "W", "X", "Y", _
                                       "Z"}

    Friend Function MapDrive(ByVal sDriveLetter As String, ByVal sUNCPath As String) As Boolean

        Dim nr As New NetResource

        nr.lpRemoteName = sUNCPath
        nr.lpLocalName = sDriveLetter & ":"
        nr.dwType = RESOURCETYPE_DISK

        Dim sUsername As String = Nothing
        Dim sPassword As String = Nothing

        Dim iResult As Integer = WNetAddConnection2(nr, sPassword, sUsername, 0)

        Return If(iResult = 0, True, False)

    End Function

    Friend Function UnMapDrive(ByVal sDriveLetter As String) As Boolean

        Dim iResult As Integer = WNetCancelConnection2(sDriveLetter & ":", 0, ForceDisconnect)

        Return If(iResult = 0, True, False)

    End Function

    Private Sub frmLoad_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs

        If (CommandLineArgs.Count > 0) Then
            For i As Integer = 0 To CommandLineArgs.Count - 1

                Select Case CommandLineArgs(i).Split(CChar(":"))(0).ToUpper
                    Case "-DL", "/DL"

                        sDriveLetter = CommandLineArgs(i).Split(CChar(":"))(1).ToUpper

                    Case "-PF", "/PF"

                        If (Mid(CommandLineArgs(i).Split(CChar(":"))(1).ToUpper, 1, 2) = "\\") Then _
                            sProjectFolder = CommandLineArgs(i).Split(CChar(":"))(1) : bMapDrive = True

                End Select

            Next

            If (bMapDrive And Array.IndexOf(IO.DriveInfo.GetDrives(), sDriveLetter) = -1 And Array.IndexOf(aValidDriveLetter, sDriveLetter) >= 0) Then _
                MapDrive(sDriveLetter, sProjectFolder)

        End If

    End Sub



Si l'un de vous a une idée du pourquoi ou du comment je peux corriger ceci.


Merci d'avance,
Nicolas H.


--

1 réponse

Nicolas H. Messages postés 36 Date d'inscription vendredi 19 octobre 2001 Statut Membre Dernière intervention 26 mai 2016 1
28 avril 2015 à 22:23
Bonsoir,

J'ai encore fait quelque test et rencontré le même problème en C++.

J'ai donc fait une dll en C++ pour totalement dégroupé l'appel à la dll mpr du CLI. Tout fonctionne bien sauf si on lance l'invite de commande en tant qu'admin.


Bien à vous,
Nicolas H.

--
0
Rejoignez-nous