Question Looking for help with WNetAddConnection and WNetCancelConnection

bowlingbrad

Member
Joined
Feb 23, 2010
Messages
6
Programming Experience
1-3
I am creating a utility to unmap and map network drives on conference room computers. Everything works just fine except for my use of WNetAddConnection2 and WNetCancelConnection2. I seem to have pinpointed the problems to virtual server shares on an EMC Clarrion. My utility works just fine if the shares are pointing to physical servers. The virtual servers create errors.
WNetAddConnection2 returns error 1219
WNetCancelConnection2 returns error 2250

I think Cancel works even though 2250 is returned.

VB.NET:
Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
(ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, _
ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

    Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
  (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer

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

    Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean

        Dim nr As NETRESOURCE
        Dim strUsername As String
        Dim strPassword As String

        nr = New NETRESOURCE
        nr.lpRemoteName = UNCPath
        nr.lpLocalName = DriveLetter & ":"
        nr.dwType = RESOURCETYPE_DISK
        strUsername = tbUsername.Text
        strPassword = tbPass.Text

        Dim result As Integer
        result = WNetAddConnection2(nr, strPassword, strUsername, RESOURCETYPE_DISK)

        If result = 0 Then
            Return True
        ElseIf result = 85 Then
            MsgBox("Error: " & result & vbLf & "Drive: " & DriveLetter & " ALREADY EXISTS!", MsgBoxStyle.Exclamation)
        Else
            MsgBox("Error: " & result & vbLf & "Drive: " & DriveLetter & " NOT CONNECTED!", MsgBoxStyle.Exclamation)
            Return False
        End If
    End Function

    Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
        Dim rc As Integer
        rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)

        If rc = 0 Or rc = 2250 Then
            Return True
        Else
            MsgBox("rc= " & rc & vbLf & "Drive letter " & DriveLetter & " NOT DISCONNECTED!")
            Return False
        End If

    End Function
 
1219 Error is "Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again."

2250 Error is "This network connection does not exist."
 
So, based on 1219 error... Should I create a local user to log into the computer and us my application to connect to the domain and map drives? If the answer is yes, can anyone assist me with how to connect using username and password to a domain using vb.net?
 
I'm back trying to get this to work.
I've changed the process slightly. I now use the following to disconnect all drives prior to connecting:
VB.NET:
Public Function UnMapAllDrives() As Boolean
        Dim processInfo As New System.Diagnostics.ProcessStartInfo()
        processInfo.FileName = "C:\WINDOWS\system32\net"
        processInfo.Arguments = "use * /del /yes"
        System.Diagnostics.Process.Start(processInfo)
    End Function

And I've even tried using this code to map the drives. Still no luck
VB.NET:
Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean
        Dim strUsername As String
        Dim strPassword As String
        strUsername = tbUsername.Text
        strPassword = tbPass.Text
        Dim processInfo As New System.Diagnostics.ProcessStartInfo()
        processInfo.FileName = "C:\WINDOWS\system32\net"
        processInfo.Arguments = "use " & DriveLetter & UNCPath & " /USER:" & strUsername & " " & strPassword
        System.Diagnostics.Process.Start(processInfo)
    End Function

STILL NO LUCK!! Isn't there anyone out there that has done something like this before?
1. Log in to a conference room computer with simple domain credentials.
2. Use personal credentials to map drives.

This works somewhat in vb scripting. I just want a cleaner look with .net

Any help would be most appreciated!
 
Back
Top