Solved

Example of Drive Mapping

Posted on 1998-10-09
4
798 Views
Last Modified: 2008-02-20
I got this piece of code from an old question that Waty answered about how to map a drive.  Can anybody give me sample code or examples on how to use it to map a drive to
machine \\machinename\folder?  I am confused about what the
constants are supposed to be.

ie sService and what format sDrive is supposed to be in.
Any help would be greatly appreciated.

P.S. I am making a connection from a Windows NT workstation
to an Auspex server.


                                             Accepted Answer
    From: waty
                                                                Date: Thursday, August 20 1998 - 11:49PM PDT

    Here is a class to connect/disconnect to network drives. I use it in several project :

    Option Explicit

    Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath
    As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
    Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal
    lpszName As String, ByVal bForce As Long) 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

    '-- Error number and message
    Public ErrorNum         As Long
    Public ErrorMsg         As String

    Public rc               As Long

    Private Const ERROR_NO_CONNECTION = 8
    Private Const ERROR_NO_DISCONNECT = 9

    Public Sub Connect(sDrive As String, sService As String, Optional sPassword As String = "")
       
       On Error GoTo Err_Connect
       Me.ErrorNum = 0
       Me.ErrorMsg = ""
       rc = WNetAddConnection(sService & Chr(0), sPassword & Chr(0), sDrive & Chr(0))
       If rc <> 0 Then GoTo Err_Connect
       
       Exit Sub

    Err_Connect:
       Me.ErrorNum = rc
       Me.ErrorMsg = WnetError(rc)

    End Sub

    Public Sub DisConnect(sDrive As String)
       
       On Error GoTo Err_DisConnect
       Me.ErrorNum = 0
       Me.ErrorMsg = ""
       rc = WNetCancelConnection(sDrive + Chr(0), 0)
       If rc <> 0 Then GoTo Err_DisConnect
       
       Exit Sub
    Err_DisConnect:
       Me.ErrorNum = rc
       Me.ErrorMsg = WnetError(rc)

    End Sub

    Private Function WnetError(Errcode As Long) As String

       Select Case Errcode
          Case WN_Not_Supported:
             WnetError = "Function is not supported."
          Case WN_Out_Of_Memory:
             WnetError = "Out of Memory."
          Case WN_Net_Error:
             WnetError = "An error occurred on the network."
          Case WN_Bad_Pointer:
             WnetError = "The Pointer was Invalid."
          Case WN_Bad_NetName:
             WnetError = "Invalid Network Resource Name."
          Case WN_Bad_Password:
             WnetError = "The Password was Invalid."
          Case WN_Bad_Localname:
             WnetError = "The local device name was invalid."
          Case WN_Access_Denied:
             WnetError = "A security violation occurred."
          Case WN_Already_Connected:
             WnetError = "The local device was connected to a remote resource."
          Case Else:
             WnetError = "Unrecognized Error " + str(Errcode) + "."
       End Select

    End Function  
0
Comment
Question by:cybergirl
  • 2
4 Comments
 
LVL 3

Expert Comment

by:vmano
ID: 1439064
i think it's better WATY himself answers this question.
0
 
LVL 9

Expert Comment

by:Dalin
ID: 1439065
cybergirl,
Try this, let me know if you have questions
Regards
Dalin


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 WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" _
       (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long

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

Public Const NO_ERROR = 0
Public Const CONNECT_UPDATE_PROFILE = &H1
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCE_REMEMBERED = &H3
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2


Private Sub cmdConnect_Click()

       Dim NetR As NETRESOURCE
       Dim ErrInfo As Long
       NetR.dwScope = RESOURCE_GLOBALNET
       NetR.dwType = RESOURCETYPE_DISK
       NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
       NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
       NetR.lpLocalName = "H:" ' Connect with no device if undefined
       NetR.lpRemoteName = "\\AnyVolume\AnyShare"
       '     'NetR.lpComment = "Optional Comment"
       '     'NetR.lpProvider =' Let the OS decide
       ' If the UserName and Password arguments are NULL, the infor
       '     mation on the MSDN ' Library CD says, "The user context for
       '     the process provides the default user name." 'ErrInfo = WNet
       '     AddConnection2(NetR, 0, 0, CONNECT_UPDATE_PROFILE)
       ErrInfo = WNetAddConnection2(NetR, "MyPassword", "MyUserName", CONNECT_UPDATE_PROFILE)

              If ErrInfo = NO_ERROR Then
                     MsgBox "Net connection successful!", vbInformation, "Share Connected"
              Else
                     MsgBox "ERROR: " & Str(ErrInfo) & " - Net connection unsuccessful!", vbExclamation, _
                     "Share not Connected"
              End If

End Sub

0
 
LVL 9

Accepted Solution

by:
Dalin earned 70 total points
ID: 1439066
cybergirl,
The code I posted connected the network drive when you supply a path.  If you want the user enter their own path, use the following:

'Declaration:
Declare Function WNetConnectionDialog Lib "mpr.dll" (ByVal hwnd As Long, ByVal dwType As Long) As Long

Declare Function WNetDisconnectDialog Lib "mpr.dll" (ByVal hwnd As Long, ByVal dwType As Long) As Long
 Public Const RESOURCETYPE_DISK = &H1, RESOURCETYPE_PRINT = 0

Using:
Dim x as Long
    x = WNetConnectionDialog(Me.hwnd, RESOURCETYPE_DISK)

Regards

Dalin
0
 

Author Comment

by:cybergirl
ID: 1439067
Thanks Dalin...it works really well.
Can you show me how to disconnect?  I can increase the points by 25 if you want.
0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

705 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

20 Experts available now in Live!

Get 1:1 Help Now