knk53
asked on
Attempt to connect to network share with wnetaddconnection2 fails error 1326
I am trying to connect to a network resource using WNETaddconnection2. The function keeps returning error 1326 which is LOGON FAILURE, when I try to connect to \\COMPUTER\SHARE. I am using VB .NET 2003.
I can run NET USE \\COMPUTER\SHARE /USER:myuname from the command line and it succeeds. The default user name and password combination (myuname, mypass) is a valid user on the network resource.
Following is all of the declarations and code that I used. In the procedure, CONNECT_SH(), the variable DBPATH a string and is set to "\\COMPUTER\SHARE" . The user name is pulled from windows using the GETUSERNAME API and I can see that is set to myuname when I am debugging.
<StructLayout(LayoutKind.S equential) > _
Public Structure NetResource
Public Scope As Integer
Public dType As Integer
Public dDispType As Integer
Public dUsage As Integer
<MarshalAs(UnmanagedType.L PTStr)> _
Public localname As String
<MarshalAs(UnmanagedType.L PTStr)> _
Public remotename As String
<MarshalAs(UnmanagedType.B yValTStr, SizeConst:=50)> _
Public comment As String
<MarshalAs(UnmanagedType.B yValTStr, SizeConst:=10)> _
Public provider As String
End Structure
Public Const NO_ERROR As Integer = 0
Public Const CONNECT_LOCALDRIVE As Integer = 256
Public Const CONNECT_REDIRECT As Integer = 128
'DWSCOPE
Public Const RESOURCE_CONNECTED As Integer = &H1
Public Const RESOURCE_GLOBALNET As Integer = &H2
Public Const RESOURCE_REMEMBERED As Integer = &H3
'DWTYPE
Public Const RESOURCETYPE_ANY As Integer = &H0
Public Const RESOURCETYPE_DISK As Integer = &H1
Public Const RESOURCETYPE_PRINT As Integer = &H2
'DWDISPLAYTYPE
Public Const RESOURCEDISPLAYTYPE_GENERI C As Integer = &H0
Public Const RESOURCEDISPLAYTYPE_DOMAIN As Integer = &H1
Public Const RESOURCEDISPLAYTYPE_SERVER As Integer = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE As Integer = &H3
Public Const RESOURCEDISPLAYTYPE_FILE As Integer = &H4
Public Const RESOURCEDISPLAYTYPE_GROUP As Integer = &H5
Public Const RESOURCEUSAGE_CONNECTABLE As Integer = &H1
Public Const RESOURCEUSAGE_CONTAINER As Integer = &H2
Public Const CONNECT_UPDATE_PROFILE As Integer = 1
Public Declare Auto Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2" (ByRef lpNetResource As NetResource, _
ByVal lpPassword As String, _
ByVal lpUsername As String, _
ByVal dwFlags As Integer) As Integer
Public Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpbuffer As String, _
ByRef nsize As Integer) As Integer
Public Function ConnectSh() As Boolean
Dim x, lans As Integer
Dim lclu, lclp, dsource As String
Dim ldate As Date
Dim netrs As Win32API.NetResource
Dim nullstr, lpw, lun, utemp As String
nullstr = Chr(0)
netrs.localname = nullstr
netrs.provider = nullstr
netrs.remotename = dbpath + Chr(0)
netrs.comment = nullstr
netrs.dType = Win32API.RESOURCETYPE_DISK
' netrs.Scope = Win32API.RESOURCE_GLOBALNE T
netrs.Scope = 0
netrs.dDispType = Win32API.RESOURCEDISPLAYTY PE_SHARE
netrs.dUsage = Win32API.RESOURCEUSAGE_CON NECTABLE
utemp = New String(CChar(" "), 50)
lans = Win32API.GetUserName(utemp , 50)
lun = utemp
lpw = ""
lans = Win32API.WNetAddConnection 2(netrs, lpw, lun, 0)
If lans = 0 Then
Return True
Else
Return False
End If
End Function
I have tried numerous variations of the code
1. Setting lun to "myuname"
2. Setting lun to "myuname" + nullstr
3. Setting lpw to nullstr (With each of the combinations above)
Again I can go to the command line and type
NET USE \\COMPUTER\SHARE /USER:myuname and it connects to the resource.
Any ideas???????????
I can run NET USE \\COMPUTER\SHARE /USER:myuname from the command line and it succeeds. The default user name and password combination (myuname, mypass) is a valid user on the network resource.
Following is all of the declarations and code that I used. In the procedure, CONNECT_SH(), the variable DBPATH a string and is set to "\\COMPUTER\SHARE" . The user name is pulled from windows using the GETUSERNAME API and I can see that is set to myuname when I am debugging.
<StructLayout(LayoutKind.S
Public Structure NetResource
Public Scope As Integer
Public dType As Integer
Public dDispType As Integer
Public dUsage As Integer
<MarshalAs(UnmanagedType.L
Public localname As String
<MarshalAs(UnmanagedType.L
Public remotename As String
<MarshalAs(UnmanagedType.B
Public comment As String
<MarshalAs(UnmanagedType.B
Public provider As String
End Structure
Public Const NO_ERROR As Integer = 0
Public Const CONNECT_LOCALDRIVE As Integer = 256
Public Const CONNECT_REDIRECT As Integer = 128
'DWSCOPE
Public Const RESOURCE_CONNECTED As Integer = &H1
Public Const RESOURCE_GLOBALNET As Integer = &H2
Public Const RESOURCE_REMEMBERED As Integer = &H3
'DWTYPE
Public Const RESOURCETYPE_ANY As Integer = &H0
Public Const RESOURCETYPE_DISK As Integer = &H1
Public Const RESOURCETYPE_PRINT As Integer = &H2
'DWDISPLAYTYPE
Public Const RESOURCEDISPLAYTYPE_GENERI
Public Const RESOURCEDISPLAYTYPE_DOMAIN
Public Const RESOURCEDISPLAYTYPE_SERVER
Public Const RESOURCEDISPLAYTYPE_SHARE As Integer = &H3
Public Const RESOURCEDISPLAYTYPE_FILE As Integer = &H4
Public Const RESOURCEDISPLAYTYPE_GROUP As Integer = &H5
Public Const RESOURCEUSAGE_CONNECTABLE As Integer = &H1
Public Const RESOURCEUSAGE_CONTAINER As Integer = &H2
Public Const CONNECT_UPDATE_PROFILE As Integer = 1
Public Declare Auto Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2" (ByRef lpNetResource As NetResource, _
ByVal lpPassword As String, _
ByVal lpUsername As String, _
ByVal dwFlags As Integer) As Integer
Public Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpbuffer As String, _
ByRef nsize As Integer) As Integer
Public Function ConnectSh() As Boolean
Dim x, lans As Integer
Dim lclu, lclp, dsource As String
Dim ldate As Date
Dim netrs As Win32API.NetResource
Dim nullstr, lpw, lun, utemp As String
nullstr = Chr(0)
netrs.localname = nullstr
netrs.provider = nullstr
netrs.remotename = dbpath + Chr(0)
netrs.comment = nullstr
netrs.dType = Win32API.RESOURCETYPE_DISK
' netrs.Scope = Win32API.RESOURCE_GLOBALNE
netrs.Scope = 0
netrs.dDispType = Win32API.RESOURCEDISPLAYTY
netrs.dUsage = Win32API.RESOURCEUSAGE_CON
utemp = New String(CChar(" "), 50)
lans = Win32API.GetUserName(utemp
lun = utemp
lpw = ""
lans = Win32API.WNetAddConnection
If lans = 0 Then
Return True
Else
Return False
End If
End Function
I have tried numerous variations of the code
1. Setting lun to "myuname"
2. Setting lun to "myuname" + nullstr
3. Setting lpw to nullstr (With each of the combinations above)
Again I can go to the command line and type
NET USE \\COMPUTER\SHARE /USER:myuname and it connects to the resource.
Any ideas???????????
My bad.. There shouldn't be a trailing backslash in the example. (See code section)
WNet.AddConnection("username", "password", "\\192.168.1.104\share", "R:")
ASKER
eql1044,
That responded with the same error. 1326. How should I call the function if I want to use the default user name and password? Also, I dont want a drive letter just the resource.
That responded with the same error. 1326. How should I call the function if I want to use the default user name and password? Also, I dont want a drive letter just the resource.
If you don't want a drive letter you dont have to specify the local name (see code section)
There is a couple of things you will want to make sure that your credentials are correct. It wouldn't hurt either to go to the computer that is hosting the share and remove it then re-enable. Then try again with the correct credentials.
There is a couple of things you will want to make sure that your credentials are correct. It wouldn't hurt either to go to the computer that is hosting the share and remove it then re-enable. Then try again with the correct credentials.
' To add resource without drive.
WNet.AddConnection("user", "password", "\\192.168.1.104\shared", Nothing)
' To close that connection
WNet.CancelConnection(WNet.RemoteName)
ASKER
I sent
WNET.AddConnection("myuser name",""," \\mycomput er\share", "")
this responds with error 1326.
************************** ********** ********** ********
I can go to the command line and type
NET USE \\mycomputer\share /user:myusername <enter> then type
NET USE <enter> and see
STATUS LOCAL REMOTE
OK \\mycomputer\share
I know the credentials are working, but why does wnetaddconnection2 return an error???
WNET.AddConnection("myuser
this responds with error 1326.
**************************
I can go to the command line and type
NET USE \\mycomputer\share /user:myusername <enter> then type
NET USE <enter> and see
STATUS LOCAL REMOTE
OK \\mycomputer\share
I know the credentials are working, but why does wnetaddconnection2 return an error???
You might have to specify a password if you didnt add the everyone group or anonymous logon group permissions to the share. How is your share setup with groups/permissions?
Everyone group does not include anonymous security identifier.
http://support.microsoft.com/kb/278259
Everyone group does not include anonymous security identifier.
http://support.microsoft.com/kb/278259
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I tried your calling method and it did work with the word NOTHING. Then I replaced the word Nothing with the "" and it still passed. I think the NOTHING is the correct answer. It is possible that, once you have logged onto a share using the API the next time you logon it may accept the "".
That appears to be the case because you are already connected so it might not even verify the password in that case. If you cancel the connection then it wouldn't already exists and would most likely fail using "" when working with the API you need make sure you pass NULL string which can be accomplished using Nothing OR vbNullString.
Open in new window