Results 1 to 10 of 10

Thread: How to map a drive

  1. #1

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    How to map a drive

    Somebody can help me on how to map a network drive. I already posted this question but nobody answered me directly. The O.S. that I going to map is Win 2000 with the username: Admin; password: Companyname; path:\\192.168.0.70. Please help me!

    I have a code found given by other members here.
    I found this but still doesn't work.


    VB Code:
    1. Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
    2.  
    3. Dim strLocalDriveLetter As String
    4.  
    5. Dim strPassword As String
    6.  
    7. Dim strNetworkPathName As String
    8.  
    9.     strLocalDriveLetter = "Z:"                  'Local drive letter to be mapped
    10.  
    11.     strPassword = ""                            'specify network password if required
    12.  
    13.     strNetworkPathName = "\\NEWPATH\NEWPATH"   'path to network drive
    14.    
    15.     If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
    16.  
    17.         MsgBox ("An Error occurred mapping the drive")
    18.  
    19.     Else
    20.  
    21.         MsgBox ("Drive successfully mapped!")
    22.  
    23.     End If

    My question in this code is where I gonna put my username?
    I'm new in VB, please help me out this. tnx
    noister
    <advertising link removed by moderator>

  2. #2
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Re: How to map a drive

    Here is some code that I used to Map Network drives

    VB Code:
    1. Private Const RESOURCE_CONNECTED As Long = &H1&
    2. Private Const RESOURCE_GLOBALNET As Long = &H2&
    3. Private Const RESOURCETYPE_DISK As Long = &H1&
    4. Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
    5. Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&
    6.  
    7. Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
    8.   Alias "WNetAddConnection2A" (lpNetResource As NETCONNECT, _
    9.   ByVal lpPassword As String, ByVal lpUserName As String, _
    10.   ByVal dwFlags As Long) As Long
    11.  
    12. Private Type NETCONNECT
    13.    dwScope As Long
    14.    dwType As Long
    15.    dwDisplayType As Long
    16.    dwUsage As Long
    17.    lpLocalName As String
    18.    lpRemoteName As String
    19.    lpComment As String
    20.    lpProvider As String
    21. End Type
    22.  
    23.  
    24. Public Function MapDrive(LocalDrive As String, _
    25.   RemoteDrive As String, Optional Username As String, _
    26.   Optional Password As String) As Boolean
    27.  
    28. 'Example:
    29. 'MapDrive "Q:", "\\RemoteMachine\RemoteDirectory", _
    30. '"MyLoginName", "MyPassword"
    31.  
    32.    Dim NetR As NETCONNECT
    33.  
    34.    NetR.dwScope = RESOURCE_GLOBALNET
    35.    NetR.dwType = RESOURCETYPE_DISK
    36.    NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
    37.    NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
    38.    NetR.lpLocalName = Left$(LocalDrive, 1) & ":"
    39.    NetR.lpRemoteName = RemoteDrive
    40.  
    41.    MapDrive = (WNetAddConnection2(NetR, Username, Password, _
    42.        CONNECT_UPDATE_PROFILE) = 0)
    43.        
    44.  
    45. If MapDrive = True Then
    46.         lstScripts.AddItem "Drive " & LocalDrive & " mapped to " & RemoteDrive
    47. Else
    48.     lstScripts.AddItem "Unable to Map " & LocalDrive & " to " & RemoteDrive
    49. End If
    50. End Function

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to map a drive

    I got an error on this being undefined:
    VB Code:
    1. CONNECT_UPDATE_PROFILE

    in this line:
    VB Code:
    1. MapDrive = (WNetAddConnection2(NetR, Username, Password, _
    2.        CONNECT_UPDATE_PROFILE) = 0)

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to map a drive

    I have been using this, but it doesn't appear to have a username:

    VB Code:
    1. Option Explicit
    2.  
    3. Declare Function WNetAddConnection Lib "mpr.dll" Alias _
    4. "WNetAddConnectionA" (ByVal lpszNetPath As String, _
    5. ByVal lpszPassword As String, ByVal lpszLocalName _
    6. As String) As Long
    7.  
    8. Declare Function WNetCancelConnection Lib "mpr.dll" _
    9. Alias "WNetCancelConnectionA" (ByVal lpszName _
    10. As String, ByVal bForce As Long) As Long
    11.  
    12. Const WN_SUCCESS = 0 ' The function was successful.
    13. Const WN_NET_ERROR = 2 ' An error occurred on the network.
    14. Const WN_BAD_PASSWORD = 6 ' The password was invalid.
    15.  
    16. Function AddConnection(MyShareName As String, _
    17. MyPWD As String, UseLetter As String) As Integer
    18.  
    19. On Local Error GoTo AddConnection1_Err
    20.  
    21. AddConnection = WNetAddConnection(MyShareName, _
    22.   MyPWD, UseLetter)
    23. AddConnection_End:
    24. Exit Function
    25.  
    26. AddConnection_Err:
    27.   AddConnection = Err
    28.   MsgBox Error$
    29. Resume AddConnection_End
    30. End Function
    31.  
    32. Function CancelConnection(DriveLetter As String, _
    33. Force As Integer) As Integer
    34.  
    35. On Local Error GoTo CancelConnection_Err
    36. CancelConnection = WNetCancelConnection(DriveLetter, _
    37. Force)
    38. CancelConnection_End:
    39.  
    40. Exit Function
    41.  
    42. CancelConnection_Err:
    43. CancelConnection = Err
    44. MsgBox Error$
    45.  
    46. Resume CancelConnection_End
    47.  
    48. End Function
    49.  
    50.  
    51. Private Sub Form_Load()
    52.  
    53. 'to add a connection call by:
    54. 'varible = AddConnection(<SharePath>, <Password>, <DriveLetter>)
    55.  
    56. 'To cancel a connection type:
    57. 'varible = CancelConnection(<SharePath, <Force>)
    58.  
    59. 'Run the project.
    60.  
    61.  
    62. End Sub

  5. #5
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    UK, Suffolk
    Posts
    319

    Re: How to map a drive

    Sorry missed that line, I am copying the code from one of my apps.

    Private Const CONNECT_UPDATE_PROFILE = &H1

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to map a drive

    The API probably just uses the current logged in Windows Username.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to map a drive

    I like my old code because it allows you to disconnect also.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to map a drive

    Yes, the API is overkill and harder to use and has less options. The Shell NET USE code is allot better.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    New Member
    Join Date
    Dec 2005
    Posts
    1

    Arrow Re: How to map a drive

    It appears to me that you seem to have everything correct as far as server loggin.

    You have

    VB Code:
    1. Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
    2.  
    3. Dim strLocalDriveLetter As String
    4.  
    5. Dim strPassword As String
    6.  
    7. Dim strNetworkPathName As String
    8.  
    9.     strLocalDriveLetter = "Z:"                  'Local drive letter to be mapped
    10.  
    11.     strPassword = ""                            'specify network password if required
    12.  
    13.     strNetworkPathName = "\\NEWPATH\NEWPATH"   'path to network drive
    14.         If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
    15.  
    16.         MsgBox ("An Error occurred mapping the drive")
    17.  
    18.     Else
    19.  
    20.         MsgBox ("Drive successfully mapped!")
    21.  
    22.     End If

    Sense strPassword is already set As String Variable you should create a loggin with an assigned string variable also which should look like this

    VB Code:
    1. Dim strUser As String
    and then when you are defining the properties for WNetAddConnection make sure to add
    VB Code:
    1. If WNetAddConnection(strNetworkPathName, strPassword, strUser, strLocalDriveLetter)

    So your final output would look like this
    VB Code:
    1. Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
    2.  
    3. Dim strLocalDriveLetter As String
    4.  
    5. Dim strUser As String
    6.  
    7. Dim strPassword As String
    8.  
    9. Dim strNetworkPathName As String
    10.  
    11.     strLocalDriveLetter = "Z:"                  'Local drive letter to be mapped
    12.  
    13.     strPassword = ""                            'specify network password if required
    14.  
    15.     strNetworkPathName = "\\NEWPATH\NEWPATH"   'path to network drive
    16.         If WNetAddConnection(strNetworkPathName, strPassword, strUser, strLocalDriveLetter) > 0 Then
    17.  
    18.         MsgBox ("An Error occurred mapping the drive")
    19.  
    20.     Else
    21.  
    22.         MsgBox ("Drive successfully mapped!")
    23.  
    24.     End If

    Hope this helps!!

  10. #10
    Addicted Member
    Join Date
    Oct 2006
    Location
    In the midst of corn, cotton, and beans
    Posts
    185

    Re: How to map a drive

    Does the code contained in this thread permanently map the drive??

    -RT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width