Article ID: 177698
Article Last Modified on 7/11/2005
Option Explicit
Private Const CONNDLG_RO_PATH = &H1 'Resource path should be
'read-only.
Private Const CONNDLG_CONN_POINT = &H2 'Netware-style movable
'connection-point enabled.
Private Const CONNDLG_USE_MRU = &H4 'Use MRU combobox
Private Const CONNDLG_HIDE_BOX = &H8 'Hide persistent connect
'check box.
'/*
' * NOTE: Set, at most, one of the below flags. If neither flag is
' * set, then the persistence is set to whatever the user
' * chose during a previous connection.
'
Private Const CONNDLG_PERSIST = &H10 'Force persistent
'connection.
Private Const CONNDLG_NOT_PERSIST = &H20 'Persistent not allowed.
' */
Private Const DISC_UPDATE_PROFILE = &H1 'Remove persistent
'connection.
Private Const DISC_NO_FORCE = &H40 'Don't force the disconnect
'if files are still open.
Private Const RESOURCETYPE_DISK = &H1
Private Const NO_ERROR = 0
Private Const WN_SUCCESS = NO_ERROR
Private Declare Function MapDrive Lib "mpr.dll" Alias _
"WNetConnectionDialog1A" ( _
lpConnectDlgStruct As Any) As Long
Private Declare Function UnMapDrive Lib "mpr.dll" Alias _
"WNetDisconnectDialog1A" ( _
lpDiscDlgStruct As Any) As Long
Private Type CONNECTDLGSTRUCT
cbStructure As Long
hwndOwner As Long
lpConnRes As Long
dwFlags As Long
dwDevNum As Long
End Type
Private Type DISCDLGSTRUCT
cbStructure As Long
hwndOwner As Long
lpLocalName As String
lpRemoteName As String
dwFlags As Long
End Type
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As Long
lpRemoteName As String
lpComment As Long
lpProvider As Long
End Type
'Memory Management Routines & Constants
Private Const GMEM_FIXED = &H0
Private Const GMEM_ZEROINIT = &H40
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
Private Declare Function GlobalAlloc Lib "kernel32" ( _
ByVal wFlags As Long, _
ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" ( _
ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(lpOut As Any, _
lpIn As Any, _
ByVal cbCopy As Long)
Private Sub cmdConnectDlg_Click()
Dim cs As CONNECTDLGSTRUCT, nr As NETRESOURCE, res As Long
'Setup the NETRESOURCE structure.
nr.lpRemoteName = "\\servername\sharename"
nr.dwType = RESOURCETYPE_DISK
With cs 'Setup the connection dialog structure.
.cbStructure = LenB(cs)
.hwndOwner = Me.hWnd
.lpConnRes = GlobalAlloc(GPTR, LenB(nr))
CopyMemory ByVal .lpConnRes, nr, LenB(nr)
.dwFlags = CONNDLG_USE_MRU Or _
CONNDLG_HIDE_BOX Or _
CONNDLG_NOT_PERSIST
End With
res = MapDrive(cs) 'Call WNetConnectionDialog1.
If res = WN_SUCCESS Then
MsgBox "MapDrive Succeeded."
txtDisconnect.Text = Chr$(cs.dwDevNum + 64) & ":"
Else
MsgBox "Error: " & Err.LastDllError
End If
GlobalFree (cs.lpConnRes)
End Sub
Private Sub cmdDisconnectDlg_Click()
Dim ds As DISCDLGSTRUCT, res As Long
'Setup the disconnect dialog structure.
ds.cbStructure = LenB(ds)
ds.hwndOwner = Me.hWnd
ds.lpLocalName = txtDisconnect.Text
ds.dwFlags = DISC_NO_FORCE Or DISC_UPDATE_PROFILE
res = UnMapDrive(ds) 'Call WnetDisconnectDialog1.
If res = WN_SUCCESS Then
MsgBox "UnMapDrive Succeeded."
Else
MsgBox "Error: " & Err.LastDllError
End If
End Sub
Keywords: kbhowto kbwnet KB177698