By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,057 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,057 IT Pros & Developers. It's quick & easy.

Connect to Network With Authentication

P: 32
Hi all I want to map a network drive. If the user has access to that
site then that path will map automatically. If the user doesnt have any rights
the i need to get the username and pwd from the user by using windows authentication screen. For example the
WNetConnectionDialog shows the mapnetwork drive dialogbox.For example if you open an IE explorer and type the servername which you dont have access

In the mean while I found WNetAddConnection Function can be used to map that drive.

Here is the code I am using.But I am facing one problem

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private m_WindowsUserName As String
  3. Private m_NetworkPath As String
  4. Private ErrInfo As Long
  5. 'Initialization of Varraibles
  6. 'Private Sub Class_Initialize()
  7. '   MapDrive
  8. 'End Sub
  9.  
  10. Private Function MapDrive()
  11.     Dim NetR As NETRESOURCE
  12.     Dim MyPass As String, MyUser As String
  13.     NetR.dwScope = RESOURCE_GLOBALNET
  14.     NetR.dwType = RESOURCETYPE_DISK
  15.     NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
  16.     NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
  17.     NetR.lpLocalName = "X:" ' If undefined, Connect with no device
  18.     NetR.lpRemoteName = m_NetworkPath    ' Your valid share
  19.     ' If the MyPass and MyUser arguments are null (use vbNullString), the
  20.     ' user context for the process provides the default user name.
  21.     ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
  22.    CONNECT_INTERACTIVE Or CONNECT_PROMPT)
  23.     'CONNECT_INTERACTIVE )
  24.     If ErrInfo = NO_ERROR Then
  25.         MsgBox "Net Connection Successful!", vbExclamation, "Share Connected"
  26.     Else
  27.         MsgBox GetErrorInfo, vbExclamation, "Share Not Connected"
  28.     End If
  29. End Function
  30.  
  31. 'Function get the Error description using Error Id
  32. Private Function GetErrorInfo() As String
  33.     If ErrInfo = NO_ERROR Then
  34.     'If no Error do nothing
  35.     ElseIf ErrInfo = ERROR_CANCELLED Then
  36.         GetErrorInfo = "User Cancelled !"
  37.     ElseIf ErrInfo = ERROR_NO_NET_OR_BAD_PATH Then
  38.         GetErrorInfo = "Network Not Found !"
  39.     ElseIf ErrInfo = ERROR_NO_NETWORK Then
  40.         GetErrorInfo = "Network Not Found !"
  41.     ElseIf ErrInfo = ERROR_DEVICE_ALREADY_REMEMBERED Then
  42.         GetErrorInfo = "The Network is Already Connect. Please disconnect it and try again !"
  43.     Else
  44.         GetErrorInfo = ErrInfo & " - Net Connection Failed!"
  45.     End If
  46.     If Len(GetErrorInfo) > 0 Then
  47.     GetErrorInfo = "ERROR : " & GetErrorInfo
  48.     End If
  49. End Function
  50.  
  51. Public Property Get NetWorkPath() As String
  52.     NetWorkPath = m_NetworkPath
  53. End Property
  54.  
  55. Public Property Let NetWorkPath(ByVal vNewValue As String)
  56.     m_NetworkPath = vNewValue
  57. End Property
  58.  
  59. Public Function MapRemoteDrive()
  60.     MapDrive
  61. End Function 
But Now I am facing one problem.Even If i have permision to that server it still prompting for username and password .I dont wy.I want to use the default username and password to map the drive.If it doesnt match, it has to dispay the
authentication window.Can some one Help

Thanks in advance
Dana
Dec 6 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.