Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

How to get the drive mapping from a drive letter?

I need to get the drive mapping of a specific drive letter.  

i.e.  I provide "F" and the function returns "//blah/blahblah"

The catch is, I can't use Microsoft Scripting Runtime as a resource.  I would preferably like to do this using on of the following Reference Libraries:

Visual Basic for Applications
Visual Basic runtime objects and procedures
Visual Basic objects and procedures

OR using an API function

Why not use Microsoft Scripting Runtime you ask?  Because I'm trying to compile an executable on an NT4 Server and I don't want to install it.  I just want to compile an executable and move it to a directory.  If I reference MSR I get a Object Error when I try to run it on NT4.  I tried installing the application by creating a package but it ended up causing problems.  We couldn't get the server to respond after rebooting.

Any help would be greatly appreciated.
1 Solution
Originally by GivenRandy:

Const lBUFFER_SIZE As Long = 255

Private Declare Function WNetGetConnection32 Lib "MPR.DLL" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, lSize As Long) As Long

Private Sub Command1_Click()
   MsgBox UNC("F:")  ' only the first letter of the pathname is required
End Sub

Private Function UNC(Pathname As String) As String
   UNC = Space(lBUFFER_SIZE)
   WNetGetConnection32 Left$(Pathname, 1) & ":", UNC, lBUFFER_SIZE
End Function
jbauer22Author Commented:

Here's my function

Public Function GetMapping(ByVal strDrive As String) As String
    Dim strMapping As String * 255
    Dim lngLength As Long
    '--- Set Drive letter to proper format
    strDrive = Left(strDrive, 1) & ":"
    '--- Get Lenth of Buffer
    lngLength = Len(strMapping)
    '--- Get Mapping
    WNetGetConnection strDrive, strMapping, lngLength
    '--- Remove Nulls
    GetMapping = NV(strMapping)
End Function

'--- I use this globally
Public Function NV(varValue As Variant) As String

    If (InStr(varValue, Chr(0)) > 0) Then
        varValue = Left(varValue, InStr(varValue, Chr(0)) - 1)
    ElseIf IsNull(varValue) Then
        varValue = ""
    End If

    NV = varValue
End Function

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now