Solved

WNetAddConnection2 ?????

Posted on 1997-12-20
24
1,933 Views
Last Modified: 2012-05-04
I'm trying to connect to a remote network. Right now I just use:
\\[IP] [UserName] [Password] in Run
I want to do the same thing from VB, I think WNetAddConnection2 is the answer. I just can't figure out how to get it working right. Anyone here have an example that could help me out?
Thanks
0
Comment
Question by:Kmiller
  • 12
  • 12
24 Comments
 
LVL 8

Expert Comment

by:mrmick
ID: 1446260
What happens when you try?  I need to know if you're using the function correctly and not connecting or if you're having trouble with calling the function.  Do you get a GPF or an error code as the return value?
0
 
LVL 8

Accepted Solution

by:
mrmick earned 200 total points
ID: 1446261
Check out in the Microsoft Knowledge Base
Article ID: Q141757
Title: "How to Connect to a Network Drive by Using WNetAddConnection"

http://premium.microsoft.com/support/kb/articles/q141/7/57.asp


0
 

Author Comment

by:Kmiller
ID: 1446262
I tried that one but it gives me a runtime error 53 file not found:user. If I replace user with user32 I get another runtime error 453:Can't find dll entry point. If I replace the entire declare statment with:
Private Declare Function WnetAddConnection Lib "mpr.dll" ( _
    ByVal lpszNetPath As String, _
    ByVal lpszPassword As String, _
    ByVal lpszLocalName As String _
) As Integer
I get error 67. Also WnetAddConnection doesn't have a UserName argument like WnetAddConnection2, but what do I know I can't get any of this to work.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446263
The correct declares for the two functions are as follows:

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 WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long

0
 

Author Comment

by:Kmiller
ID: 1446264
This is what I have in there now, it gives me unrecognized error #67:
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
Const WN_Success = &H0
Const WN_Not_Supported = &H1
Const WN_Net_Error = &H2
Const WN_Bad_Pointer = &H4
Const WN_Bad_NetName = &H32
Const WN_Bad_Password = &H6
Const WN_Bad_Localname = &H33
Const WN_Access_Denied = &H7
Const WN_Out_Of_Memory = &HB
Const WN_Already_Connected = &H34
Private Function IsSuccess%(ReturnCode%, Msg$)
If ReturnCode% = WN_Success Then
    IsSuccess% = True
Else
    IsSuccess% = False
    Select Case ReturnCode%
    Case WN_Success:
        Drive1.Refresh
    Case WN_Not_Supported:
        Msg$ = "Function is not supported."
    Case WN_Out_Of_Memory:
        Msg$ = "Out of Memory."
    Case WN_Net_Error:
        Msg$ = "An error occurred on the network."
    Case WN_Bad_Pointer:
        Msg$ = "The Pointer was Invalid."
    Case WN_Bad_NetName:
        Msg$ = "Invalid Network Resource Name."
    Case WN_Bad_Password:
        Msg$ = "The Password was Invalid."
    Case WN_Bad_Localname:
        Msg$ = "The local device name was invalid."
    Case WN_Access_Denied:
        Msg$ = "A security violation occurred."
    Case WN_Already_Connected:
        Msg$ = "The local device was connected to a remote resource."
    Case Else:
        Msg$ = "Unrecognized Error " + Str$(ReturnCode%) + "."
    End Select
End If
End Function

Private Sub Connect_Click()
ServerText$ = UCase$(NetPath.Text) + Chr$(0) ' Network resource name
PasswordText$ = Password.Text + Chr$(0) ' Password for the resource
driveletter$ = "N:" + Chr$(0) ' Substitute your own drive letter
' Enter the following two lines as one, single line:
Succeed% = WNetAddConnection(ServerText$, PasswordText$, driveletter$)
If IsSuccess(Succeed%, Msg$) = True Then ' Call Function to parse
    ' potential error messages.
    Drive1.Refresh
    NetPath.Text = "" ' Reset the contents following connection
Else
    MsgBox Msg$
End If
End Sub



0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446265
67 = ERROR_BAD_NET_NAME

I'm trying your code now.
0
 

Author Comment

by:Kmiller
ID: 1446266
i'm using a IP address for the netname
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446267
Your code works perfectly for me with only one exception (which I don't think is your problem, but could cause other problems later) - I changed the Succeed% to Succeed& because the return value is a long integer (plus appropriate changes to references that ware affected by the type change).

You're probably using the wrong network resource name.

Use Windows Explorer, go down to network neighborhood and  open the tree...

If the Server Computer Name is for instance "TheServer" - Open it up and find the resource you wish to map.  If you see for instance "C-NTFS" - to map to this the server resource, the resource name would be:

\\TheServer\C-NTFS

This should get you on track.


0
 

Author Comment

by:Kmiller
ID: 1446268
I think i'm getting closer =)
I now get error 1326 but I think it's actually trying to connect.

I used \\[IP]\test for the net resource
\\[IP]\test [UserName] [Password] works in RUN no prob.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446269
1326 = ERROR_LOGON_FAILURE

I can't test using the IP method because I don't permit TCP/IP mapping for security reasons on my network.  I don’t because I've got a direct connect to the internet through a router directly connected to my hub.

Why not use the server computer name?  This I know works.

0
 

Author Comment

by:Kmiller
ID: 1446270
It's not a local network, I connect to the remote computer via tcp\ip where I share a drive for group projects. We both are running NT4. Could my prob be that no username is being sent?
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446271
I would assume so because the Win32 SDK states that the WNetAddConnection function is designed for connecting to a local network resource; however, the WNetAddConnection2 function states "makes a connection to a network resource."  I assume this means a WAN.  Try converting this code to make the connection using the WNetAddConnection2 function.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Kmiller
ID: 1446272
=( which brings us back to my orginal post, I'm having trouble getting the WNetAddConnection2 function working. It requires that I define a type
Private Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    lpLocalName As Long
    lpRemoteName As Long
    lpComment As Long
    lpProvider As Long
End Type
Between this and Enumerating the network resources I get totally lost. Sorry about being such a pest, maybe I should give up =)
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446273
No, don't give up.  Give me a few minutes.
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446274
What's your email address?
0
 

Author Comment

by:Kmiller
ID: 1446275
I found a KB article that might help, I'll get back to you.
0
 

Author Comment

by:Kmiller
ID: 1446276
climbon@pacbell.net
0
 

Author Comment

by:Kmiller
ID: 1446277
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446278
The Q article works for me the same, does it work substituting "//xxx.xxx.xxx.xxx" instead of the ServerName\ShareName combination?


0
 

Author Comment

by:Kmiller
ID: 1446279
Well believe it or not it works now =)
Thanks for putting up with me!

0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446280
So, you were able to use:

NetR.lpRemoteName = "\\xxx.xxx.xxx.xxx"

???
0
 

Author Comment

by:Kmiller
ID: 1446281
Actually I just got the KB article on WNetADDConnection2 working:

Private 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
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal fForce As Long) As Long
Private 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
Private Const NO_ERROR = 0
Private Const CONNECT_UPDATE_PROFILE = &H1
' The following includes all the constants defined for NETRESOURCE, ' not just the ones used in this example.
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCETYPE_PRINT = &H2
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCE_CONNECTED = &H1
Private Const RESOURCE_REMEMBERED = &H3
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Private Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Private Const RESOURCEDISPLAYTYPE_SERVER = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
Private Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const RESOURCEUSAGE_CONTAINER = &H2
' Error Constants:
Private Const ERROR_ACCESS_DENIED = 5&
Private Const ERROR_ALREADY_ASSIGNED = 85&
Private Const ERROR_BAD_DEV_TYPE = 66&
Private Const ERROR_BAD_DEVICE = 1200&
Private Const ERROR_BAD_NET_NAME = 67&
Private Const ERROR_BAD_PROFILE = 1206&
Private Const ERROR_BAD_PROVIDER = 1204&
Private Const ERROR_BUSY = 170&
Private Const ERROR_CANCELLED = 1223&
Private Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Private Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_INVALID_PASSWORD = 86&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&

Private Sub Command1_Click()
Dim NetR As NETRESOURCE
Dim ErrInfo As Long
Dim MyPass, MyUser As String
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = "X:" ' If undefined, Connect with no device
NetR.lpRemoteName = "\\[IP]\test" ' Your valid share
'NetR.lpComment = "Optional Comment"
'NetR.lpProvider = ' Leave this undefined
'If the UserName and Password arguments are NULL, the user context
'for the process provides the default user name.
MyPass = "[mypass]"
MyUser = "[myusername]"
ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, CONNECT_UPDATE_PROFILE)
If ErrInfo = NO_ERROR Then
    MsgBox "Net Connection Successful!", vbInformation, "Share Connected"
Else
    MsgBox "ERROR: " & ErrInfo & " - Net Connection Failed!", vbExclamation, "Share not Connected "
End If
End Sub
Private Sub Command2_Click()
Dim ErrInfo As Long
Dim strLocalName As String
'You may specify either the lpRemoteName or lpLocalName
'strLocalName = "\\ServerName\ShareName" strLocalName = "X:"
ErrInfo = WNetCancelConnection2(strLocalName, CONNECT_UPDATE_PROFILE, False)
If ErrInfo = NO_ERROR Then
    MsgBox "Net Disconnection Successful!", vbInformation, "Share Disconnected"
Else
    MsgBox "ERROR: " & ErrInfo & " - Net Disconnection Failed!", vbExclamation, "Share not Disconnected"
End If
End Sub

0
 

Author Comment

by:Kmiller
ID: 1446282
umm, on second look the short answer was yes the code uses
NetR.lpRemoteName = "\\xxx.xxx.xxx.xxx"
=)
0
 
LVL 8

Expert Comment

by:mrmick
ID: 1446283
Thanks.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now