PDA

Click to See Complete Forum and Search --> : Help with mapping a drive (persistence)


FrogBoy666
Jan 30th, 2001, 01:09 PM
I'm currently using "WNetAddConnection2" to map a network drive, but the all of the connections are persistent. I was wondering if there was some way to make the mapping non-persistent (i.e., ensure that my computer doesn't try to reconnect during the next logon to WinNT).

Here is my code (in case you need it):

This is in a module:

Option Explicit

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

' error values
Public Const ERROR_SESSION_CREDENTIAL_CONFLICT = 1219&
Public Const ERROR_INVALID_PASSWORDNAME = 1216&
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_DEV_NOT_EXIST = 55&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const NO_ERROR = 0

Public Const RESOURCETYPE_DISK As Long = &H1& 'dwType

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

Public Const CONNECT_UPDATE_PROFILE As Long = &H1&

Public netRes As NETRESOURCE
Public lResult As Long



And this is in my form (frmMain):

Private Sub Command1_Click()

Dim strDriveLetter, strNetResName As String
Dim strPswd, strUser As String

strDriveLetter = "K:" + Chr$(0)
strNetResName = "\\network\path" + Chr(0)

With netRes
.dwType = RESOURCETYPE_DISK
.lpLocalName = strDriveLetter
.lpRemoteName = strNetResName
.lpProvider = ""
End With

strUser = "UserName"
strPswd = "Password"

lResult = WNetAddConnection2(netRes, strPswd, strUser, CONNECT_UPDATE_PROFILE)
'// update profile with connect information

' check for errors using const's defined above
Select Case lResult
Case NO_ERROR
' No problems occurred...

Case Else
'Handle any problems that occur
End Select

End Sub


I may be overlooking something that is already in my code (perhaps in the NETRESOURCE section)... That wouldn't be unusual. :)

kiwisheep
Jan 31st, 2001, 05:39 PM
Hope this helps - I copied it from the API Guide program.


· dwFlags
A set of bit flags that specify connection options. The following bit flag constant is currently defined:
CONNECT_UPDATE_PROFILE
The network resource connection should be remembered.

If this bit flag is set, the operating system automatically attempts to restore the connection when the user logs on.
The operating system remembers only successful connections that redirect local devices. It does not remember unsuccessful connections or deviceless connections. A deviceless connection occurs when lpLocalName is NULL or points to an empty string.
If this bit flag is clear, the operating system will not automatically restore the connection at logon.