1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
'Variables utilisées par l'API à mettre dans un module
Global Const ERROR_BADDB = 1
Global Const ERROR_BADKEY = 2
Global Const ERROR_CANTOPEN = 3
Global Const ERROR_CANTREAD = 4
Global Const ERROR_CANTWRITE = 5
Global Const ERROR_OUTOFMEMORY = 6
Global Const ERROR_INVALID_PARAMETER = 7
Global Const ERROR_ACCESS_DENIED = 8
Global Const ERROR_BAD_NETPATH = 53
Global Const ERROR_BAD_NET_NAME = 67
Global Const ERROR_ALREADY_ASSIGNED = 85
Global Const ERROR_INVALID_PARAMETERS = 87
Global Const ERROR_NO_MORE_ITEMS = 259
Global Const ERROR_NO_SUCH_LOGON_SESSION = 1312
Global Const ERROR_USER_EXISTS = 1316
'API de création d'un lecteur réseau à mettre dans un module
Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
'API de suppression du lecteur réseau
Public Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
Global Const RESOURCETYPE_DISK = &H1
Public Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
Global theNetResource As NETRESOURCE
'Procédure de connexion à mettre où tu veux
Public Sub AddConnexion(MonChemin As String, MonMotDePasse As String,Utilisateur As String, Lecteur As String)
Dim iEtatConnexion As Integer
On Error GoTo MsgErr
theNetResource.dwType = RESOURCETYPE_DISK
theNetResource.lpRemoteName = MonChemin
theNetResource.lpLocalName = Lecteur
iEtatConnexion = WNetAddConnection2(theNetResource, MonMotDePasse, Utilisateur, 0)
If iEtatConnexion <> 0 Then
If iEtatConnexion = ERROR_ACCESS_DENIED Then
MsgBox "Accès au répertoire " & UCase(MonChemin) & " refusé." , vbOKOnly + vbExclamation, "ANOMALIE"
ElseIf iEtatConnexion = ERROR_BAD_NETPATH Then
MsgBox "Chemin inexistant ou incorrect." & vbCrLf & "Connection au répertoire " & UCase(MonChemin) & " impossible." , vbOKOnly + vbExclamation, "ANOMALIE"
ElseIf iEtatConnexion = ERROR_BAD_NET_NAME Then
MsgBox "Répertoire inexistant ou non-partagé." & vbCrLf & "Connection au répertoire " & UCase(MonChemin) & " impossible." , vbOKOnly + vbExclamation, "ANOMALIE"
ElseIf iEtatConnexion = ERROR_ALREADY_ASSIGNED Then
'Déjà conncecté
Exit Sub
ElseIf iEtatConnexion = ERROR_NO_SUCH_LOGON_SESSION Then
MsgBox "Aucune session en cours." & vbCrLf & "Connection au répertoire " & UCase(MonChemin) & " impossible." & vbCrLf & "Vérifier dans le menu Panneau de configurations\Services : Service TFJ," & vbCrLf & "bouton Démarrage ... si la zone Ce Compte contient une valeur autre " & vbCrLf & "que nulle ou LocalSystem." , vbOKOnly + vbExclamation, "ANOMALIE"
ElseIf iEtatConnexion = ERROR_USER_EXISTS Then
MsgBox "Mot de passe incorrect." & vbCrLf & "Connection au répertoire " & UCase(MonChemin) & " impossible." , vbOKOnly + vbExclamation, "ANOMALIE"
Else
MsgBox "Erreur de connexion " & iEtatConnexion & "." & vbCrLf & "Connection au répertoire " & UCase(MonChemin) & " impossible." , vbOKOnly + vbExclamation, "ANOMALIE"
End If
End If
Exit Sub
MsgErr:
MsgBox Err.Number & vbCrLf & Err.Description, vbOKOnly + vbCritical, "ANOMALIE"
End Sub
'Procédure de fermeture de connexion à mettre où tu veux
Public Sub CancelConnection()
Dim iEtatConnexion As Integer
On Error GoTo MsgErr
iEtatConnexion = WNetCancelConnection2(theNetResource.lpLocalName, 0, 0)
If iEtatConnexion <> 0 Then
MsgBox "Déconnexion du lecteur " & UCase(theNetResource.lpLocalName) & " impossible.", vbOKOnly + vbExclamation, "INFORMATION"
End If
Exit Sub
MsgErr:
MsgBox Err.Number & vbCrLf & Err.Description, vbOKOnly + vbCritical, "ANOMALIE"
End Sub |
Partager