
WNetAddConnection2 PLEASE HELP!
Barbara,
Below is a code snip from a working tool I wrote, hope this helps.
Richard
In Module:
'NETRESOURCE Struct necessary for WNetAddConnection2 function
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
'Adds the NT network connection
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
In Form:
Private Sub cmdOK_Click()
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
Dim Password As String
Dim UserName As String
Dim RetVal As Variant
'Fire Up the Hourglass While Waiting for a Connection
Screen.MousePointer = vbHourglass
Password = txtPassword & Chr$(0)
UserName = txtUserName & Chr$(0)
'Defines NETRESOURCE functions
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = LocalDrv
NetR.lpRemoteName = txtServerName
'Establishes the drive mapping
ErrInfo = WNetAddConnection2(NetR, txtPassword, txtUserName, 0)
If ErrInfo = NO_ERROR Then
'Clear the Hourglass
Screen.MousePointer = vbDefault
'Clears the password textbox
txtPassword.Text = ""
'Hides the logon dialog box
frmLogin.Hide
'Opens Explorer to the connected network drive
RetVal = Shell("EXPLORER.EXE " & LocalDrv, 1)
'Loads the Systray icon control
Load frmIcon
Else
'Clear the Hourglass
Screen.MousePointer = vbDefault
'Handle the Error
GoTo ErrorHandler
End If
Exit Sub
ErrorHandler:
Select Case ErrInfo
Blah, Blah, Blah...
Quote:
>>Don't know whether this is the answer, but I noticed a couple of minor
>>things in your code:
>>1) Dim MyPass, MyUser As String
>>What you're actually doing here is Dimming MyPass as a variant. You
>>need:
>> Dim MyPass As String, MyUser As String
>>2) You should terminate all strings in your call with a null character.
>>Try:
>> NetR.lpLocalName = "U:" & vbNullChar
>> NetR.lpRemoteName = Trim(Text1.Text) & vbNullChar
>> MyPass = "kgeaug98" & vbNullChar
>> MyUser = "kcmsgo" & vbNullChar
>>HTH.
>>> Here's my code...
>>> BAS file...
>>> 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
>>> Declare Function WNetCancelConnection2 Lib "mpr.dll" _
>>> Alias "WNetCancelConnection2A" _
>>> (ByVal lpName As String, _
>>> ByVal dwFlags As Long, _
>>> ByVal fForce As Long) As Long
>>> 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
>>> -----------------------------------------------------------------
>>> Form...
>>> Private Sub Command1_Click()
>>> Dim NetR As NETRESOURCE
>>> Dim ErrInfo As Long
>>> Dim MyPass, MyUser As String
>>> NetR.dwScope = 2
>>> NetR.dwType = 1
>>> NetR.dwDisplayType = 3
>>> NetR.dwUsage = 1
>>> NetR.lpLocalName = "U:"
>>> NetR.lpRemoteName = Trim(Text1.Text) ' text box
>>> MyPass = "kgeaug98"
>>> MyUser = "kcmsgo"
>>> ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, 0)
>>> If ErrInfo = 0 Then
>>> MsgBox "Net Connection Successful"
>>> Else
>>> MsgBox "Error: " & ErrInfo & " Connection Failed"
>>> End If
>>> End Sub
>>> Private Sub Command2_Click()
>>> Dim ErrInfo As Long
>>> ErrInfo = WNetCancelConnection2(Trim(Text1.Text), 0, True)
>>> If ErrInfo = 0 Then
>>> MsgBox "Net disconnection Successful"
>>> Else
>>> MsgBox "ERROR: " & ErrInfo & " Net Disconnection Failed"
>>> End If
>>> End Sub
>>> -------------------------------------------------------------------
-
>-
>>> What I've found...
>>> I was using ErrInfo = WNetCancelConnection2("U:",0,True).
>>> This was removing the "mapping" of U: in Windows Explorer, but the
>>> connection was still active. I've changed it to use the
>NetR.lpRemoteName,
>>> and this cancelled the connection. So, this problem was resolved.
>>> New problem...I can double click on "Network Neighborhood" and get to a
>>> particular lan by keying in the id "kcmsgo" and the password "kgeaug98"
>(as
>>> stated above) and this works fine. However, this API call
>>> WNetAddConnection2 (above) works on some lans and not on others. The
>double
>>> clicking, in Network Neighborhood, works on all of the lans. I'm
getting
>>> Error Code of 5 (Access denied). I don't understand what is happening
>>> different between Windows Explorer (Network Neighborhood) and the
>>> WNetAddConnection2 API call.
>>> Any help would be appreciated.
>>> Thanks!
>>--
>>Beer, Wine and Database Programming. What could be better?
>>Visit "Doug Steele's Beer and Programming Emporium"
>>http://webhome.idirect.com/~djsteele/
>I did have MyPass and MyUser as string. However, I hadn't tried putting
the
>vbNullChar at the end of each of these variables. I tried this today, and
>it didn't work either. Thanks for your suggestion. Any other thoughts
>would be greatly appreciated.