TKSoft-Online

API: Netzlaufwerke verbinden PDF Drucken E-Mail
( 0 Votes )
MS-Access Codes - Codeschnipsel Dateisystem
  
Freitag, den 11. April 2008 um 07:00 Uhr

Problemstellung:

Wie kann ich mit API ein Netzlaufwerk verbinden bzw. die Verbindung aufheben? 

1. Benötige API's und Konstanten:

 
' Netzlaufwerkfunktionen per Dialog
Public Declare Function WNetConnectionDialog Lib "mpr.dll" ( _
    ByVal hWnd As Long, _
    ByVal dwType As LongAs Long

Public Declare Function WNetDisconnectDialog Lib "mpr.dll" ( _
    ByVal hWnd As Long, _
    ByVal dwType As LongAs Long

' Netzlaufwerkfunktionen ohne Dialog
Public Declare Function WNetAddConnection Lib "mpr.dll" _
    Alias "WNetAddConnectionA" ( _
    ByVal lpszNetPath As String, _
    ByVal lpszPassword As String, _
    ByVal lpszLocalName As StringAs Long

Public Declare Function WNetCancelConnection Lib "mpr.dll" _
    Alias "WNetCancelConnectionA" ( _
    ByVal lpszName As String, _
    ByVal bForce As LongAs Long

Public Const RESOURCETYPE_DISK = &H1
Public Const WN_Success = &H0
Public Const WN_Net_Error = &H2
Public Const WN_Bad_Password = &H6
Public Const WN_Access_Denied = &H7
Public Const WN_Already_Connected = &H34
Public Const WN_Bad_NetName = &H32

2. Verbinden per Dialog (im Formular):

 

Dim lResult As Long

lResult = WNetConnectionDialog(Me.hWnd, _
    RESOURCETYPE_DISK)

Ergebnis:


3. Verbindung per Dialog aufheben (im Formular):

Dim lResult As Long

lResult = WNetDisconnectDialog(Me.hWnd, _
    RESOURCETYPE_DISK)

Ergebnis:

 

4. Verbinden per Code:

Diese Funktionen unterstützen keine Übergabe des Usernamens und des Profils


Das wird ein extra Thema.

Public Function Connect_NetDrive(sDriveLetter As String, _
    sNetworkPath As String, _
    Optional sPWD As String = vbNullString) As Boolean

    Dim lResult As Long

    lResult = WNetAddConnection(sNetworkPath, _
        sPWD, sDriveLetter)

    If lResult <> WN_Success Then
        Select Case lResult
            Case WN_Net_Error
                MsgBox "Netzwerkfehler!"
            Case WN_Bad_Password
                MsgBox "Ungültiges Passwort!"
            Case WN_Access_Denied
                MsgBox "Zugriff verweigert"
            Case WN_Already_Connected
                MsgBox "Laufwerk ist bereits verbunden"
            Case WN_Bad_NetName
                MsgBox "Der Netzwerkpfad wurde nicht gefunden"
            Case Else
                MsgBox "Unbekannter Fehler"
        End Select
    End If

    Connect_NetDrive = (lResult = WN_Success)
End Function

Aufruf z.B.:
ohne Passwort:

Connect_NetDrive "E:""\PC-Name\Freigabename"

mit Passwort:

Connect_NetDrive "E:", \PC-Name\Freigabename, "geheim"

 

5. Verbindung aufheben per Code:

Public Function Disconnect_NetDrive(sDriveLetter As StringAs Boolean

    Dim lResult As Long

    lResult = WNetCancelConnection(sDriveLetter, True)

    If lResult <> WN_Success Then
        If lResult = WN_Net_Error Then
            MsgBox "Netzwerkfehler!"
        Else
            MsgBox "Unbekannter Fehler"
        End If
    End If

    Disconnect_NetDrive = (lResult = WN_Success)
End Function

Aufruf z.B.:

  Disconnect_NetDrive "K:"

DatumKlicks
Total2389
Di. 221
Mo. 212
So. 202
Sa. 192
Fr. 185
Do. 172
Mi. 161
Aktualisiert ( Freitag, den 02. Juli 2010 um 09:21 Uhr )
 

Kommentar schreiben


Sicherheitscode
Aktualisieren

Login

Latest Comments

Latest Forum Posts

Mehr »

Download Statistik

41 Kategorien
187 Dateien
173415 Downloads