gettingstarted
[Top] [All Lists]

Re: mapping a network drive

To: Getting Started <gettingstarted at lists dot realsoftware dot com>
Subject: Re: mapping a network drive
From: Aaron Ballman <aaron at realsoftware dot com>
Date: Mon, 27 Dec 2004 16:38:49 -0600
Delivered-to: gettingstarted at lists dot realsoftware dot com
References: <83DF127D-5855-11D9-B9CD-000A95AE3462 at ihs dot gov>
Preferably default to X: - any ideas? If I can't figure this out I have to do it in VB, then I have to support two versions (I already have an application that maps a windows source in Mac OS X, would like to use the same application for Windows).

If you can do it in VB, then you can do it in RB as well. Let me see if I get the gist of what you're after. You have a network drive, like \\foo\bar\baz\, and you want to map that directory to a drive letter programatically (say, x:)?

I found a snippet of VB code (but have no network to test it on since I'm on vacation currently) that should work.

Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
      "WNetAddConnection2A" (lpNetResource As Ptr, _
      lpPassword As CString, lpUserName As CString, _
      dwFlags As Integer) As Integer

Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
      "WNetCancelConnection2A" (lpName As CString, _
      dwFlags As Integer, fForce As Integer) As Integer

// This is the structure of the memory block you
// pass as the lpNetResource parameter to the WNetAddConnection2 method.
'      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


      Const NO_ERROR = 0
      Const CONNECT_UPDATE_PROFILE = &H1
      ' The following includes all the constants defined for NETRESOURCE,
      ' not just the ones used in this example.
      Const RESOURCETYPE_DISK = &H1
      Const RESOURCETYPE_PRINT = &H2
      Const RESOURCETYPE_ANY = &H0
      Const RESOURCE_CONNECTED = &H1
      Const RESOURCE_REMEMBERED = &H3
      Const RESOURCE_GLOBALNET = &H2
      Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
      Const RESOURCEDISPLAYTYPE_GENERIC = &H0
      Const RESOURCEDISPLAYTYPE_SERVER = &H2
      Const RESOURCEDISPLAYTYPE_SHARE = &H3
      Const RESOURCEUSAGE_CONNECTABLE = &H1
      Const RESOURCEUSAGE_CONTAINER = &H2
      ' Error Constants:
      Const ERROR_ACCESS_DENIED = 5&
      Const ERROR_ALREADY_ASSIGNED = 85&
      Const ERROR_BAD_DEV_TYPE = 66&
      Const ERROR_BAD_DEVICE = 1200&
      Const ERROR_BAD_NET_NAME = 67&
      Const ERROR_BAD_PROFILE = 1206&
      Const ERROR_BAD_PROVIDER = 1204&
      Const ERROR_BUSY = 170&
      Const ERROR_CANCELLED = 1223&
      Const ERROR_CANNOT_OPEN_PROFILE = 1205&
      Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
      Const ERROR_EXTENDED_ERROR = 1208&
      Const ERROR_INVALID_PASSWORD = 86&
      Const ERROR_NO_NET_OR_BAD_PATH = 1203&

Add two PushButtons to Window1. These will be PushButton1 and PushButton2 by default. Add the following code to Window1, substituting a valid share name for "\\ServerName\ShareName":

PushButton1.Action()
'      Dim NetR As NETRESOURCE
      Dim NetR as new MemoryBlock( 32 )
      Dim ErrInfo As Integer
      Dim MyPass As String, MyUser As String

        NetR.Long( 0 ) = RESOURCE_GLOBALNET
'      NetR.dwScope = RESOURCE_GLOBALNET
        NetR.Long( 4 ) = RESOURCETYPE_DISK
'      NetR.dwType = RESOURCETYPE_DISK
        NetR.Long( 8 ) = RESOURCEDISPLAYTYPE_SHARE
'      NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
        NetR.Long( 12 ) = RESOURCEUSEAGE_CONNECTABLE
'      NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
'      NetR.lpLocalName = "X:" ' If undefined, Connect with no device
        dim driveLetter as new MemoryBlock( 3 )
        driveLetter.CString( 0 ) = "X:"
        NetR.Ptr( 16 ) = driveLetter
'      NetR.lpRemoteName = "\\ServerName\ShareName"   ' Your valid share
        dim shareName as new MemoryBlock( Len( validSharePath ) + 1 )
        shareName.CString( 0 ) = validSharePath
        NetR.Ptr( 20 ) = shareName
      'NetR.lpComment = "Optional Comment"
      'NetR.lpProvider =    ' Leave this undefined

      ' If the MyPass and MyUser arguments are "",
      ' user context for the process provides the default user name.
      ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
      CONNECT_UPDATE_PROFILE)
      If ErrInfo = NO_ERROR Then
        MsgBox "Net Connection Successful!"
      Else
        MsgBox "ERROR: " + Str( ErrInfo ) + " - Net Connection Failed!"
      End If
End Sub

PushButton2.Action()
      Dim ErrInfo As Integer
      Dim strLocalName As String

      ' You may specify either the lpRemoteName or lpLocalName
      'strLocalName = "\\ServerName\ShareName"
      strLocalName = "X:"
      ErrInfo = WNetCancelConnection2(strLocalName, _
      CONNECT_UPDATE_PROFILE, False)
      If ErrInfo = NO_ERROR Then
        MsgBox "Net Disconnection Successful!"
      Else
        MsgBox "ERROR: " + Str( ErrInfo )+ " - Net Disconnection Failed!"
      End If
End Sub

Keep in mind that I'm translating the VB code in my head. This code is not tested, but it should be functional. I found the code here <http://www.tek-tips.com/viewthread.cfm?qid=315357&page=1>

HTH!

~Aaron
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://www.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>