|
-
Nov 25th, 2005, 10:04 PM
#1
Thread Starter
Fanatic Member
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:
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String
strLocalDriveLetter = "Z:" 'Local drive letter to be mapped
strPassword = "" 'specify network password if required
strNetworkPathName = "\\NEWPATH\NEWPATH" 'path to network drive
If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
MsgBox ("An Error occurred mapping the drive")
Else
MsgBox ("Drive successfully mapped!")
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>
-
Nov 26th, 2005, 02:03 PM
#2
Hyperactive Member
Re: How to map a drive
Here is some code that I used to Map Network drives
VB Code:
Private Const RESOURCE_CONNECTED As Long = &H1&
Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCETYPE_DISK As Long = &H1&
Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&
Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
Alias "WNetAddConnection2A" (lpNetResource As NETCONNECT, _
ByVal lpPassword As String, ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long
Private Type NETCONNECT
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
Public Function MapDrive(LocalDrive As String, _
RemoteDrive As String, Optional Username As String, _
Optional Password As String) As Boolean
'Example:
'MapDrive "Q:", "\\RemoteMachine\RemoteDirectory", _
'"MyLoginName", "MyPassword"
Dim NetR As NETCONNECT
NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
NetR.lpLocalName = Left$(LocalDrive, 1) & ":"
NetR.lpRemoteName = RemoteDrive
MapDrive = (WNetAddConnection2(NetR, Username, Password, _
CONNECT_UPDATE_PROFILE) = 0)
If MapDrive = True Then
lstScripts.AddItem "Drive " & LocalDrive & " mapped to " & RemoteDrive
Else
lstScripts.AddItem "Unable to Map " & LocalDrive & " to " & RemoteDrive
End If
End Function
-
Nov 26th, 2005, 02:14 PM
#3
Re: How to map a drive
I got an error on this being undefined:
in this line:
VB Code:
MapDrive = (WNetAddConnection2(NetR, Username, Password, _
CONNECT_UPDATE_PROFILE) = 0)
-
Nov 26th, 2005, 02:15 PM
#4
Re: How to map a drive
I have been using this, but it doesn't appear to have a username:
VB Code:
Option Explicit
Declare Function WNetAddConnection Lib "mpr.dll" Alias _
"WNetAddConnectionA" (ByVal lpszNetPath As String, _
ByVal lpszPassword As String, ByVal lpszLocalName _
As String) As Long
Declare Function WNetCancelConnection Lib "mpr.dll" _
Alias "WNetCancelConnectionA" (ByVal lpszName _
As String, ByVal bForce As Long) As Long
Const WN_SUCCESS = 0 ' The function was successful.
Const WN_NET_ERROR = 2 ' An error occurred on the network.
Const WN_BAD_PASSWORD = 6 ' The password was invalid.
Function AddConnection(MyShareName As String, _
MyPWD As String, UseLetter As String) As Integer
On Local Error GoTo AddConnection1_Err
AddConnection = WNetAddConnection(MyShareName, _
MyPWD, UseLetter)
AddConnection_End:
Exit Function
AddConnection_Err:
AddConnection = Err
MsgBox Error$
Resume AddConnection_End
End Function
Function CancelConnection(DriveLetter As String, _
Force As Integer) As Integer
On Local Error GoTo CancelConnection_Err
CancelConnection = WNetCancelConnection(DriveLetter, _
Force)
CancelConnection_End:
Exit Function
CancelConnection_Err:
CancelConnection = Err
MsgBox Error$
Resume CancelConnection_End
End Function
Private Sub Form_Load()
'to add a connection call by:
'varible = AddConnection(<SharePath>, <Password>, <DriveLetter>)
'To cancel a connection type:
'varible = CancelConnection(<SharePath, <Force>)
'Run the project.
End Sub
-
Nov 26th, 2005, 02:19 PM
#5
Hyperactive Member
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
-
Nov 26th, 2005, 02:22 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 26th, 2005, 02:59 PM
#7
Re: How to map a drive
I like my old code because it allows you to disconnect also.
-
Nov 26th, 2005, 03:03 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Dec 3rd, 2005, 05:08 PM
#9
New Member
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:
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
Dim strLocalDriveLetter As String
Dim strPassword As String
Dim strNetworkPathName As String
strLocalDriveLetter = "Z:" 'Local drive letter to be mapped
strPassword = "" 'specify network password if required
strNetworkPathName = "\\NEWPATH\NEWPATH" 'path to network drive
If WNetAddConnection(strNetworkPathName, strPassword, strLocalDriveLetter) > 0 Then
MsgBox ("An Error occurred mapping the drive")
Else
MsgBox ("Drive successfully mapped!")
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
and then when you are defining the properties for WNetAddConnection make sure to add
VB Code:
If WNetAddConnection(strNetworkPathName, strPassword, strUser, strLocalDriveLetter)
So your final output would look like this
VB Code:
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszstrPassword As String, ByVal lpszLocalName As String) As Long
Dim strLocalDriveLetter As String
Dim strUser As String
Dim strPassword As String
Dim strNetworkPathName As String
strLocalDriveLetter = "Z:" 'Local drive letter to be mapped
strPassword = "" 'specify network password if required
strNetworkPathName = "\\NEWPATH\NEWPATH" 'path to network drive
If WNetAddConnection(strNetworkPathName, strPassword, strUser, strLocalDriveLetter) > 0 Then
MsgBox ("An Error occurred mapping the drive")
Else
MsgBox ("Drive successfully mapped!")
End If
Hope this helps!!
-
Oct 2nd, 2009, 02:04 PM
#10
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|