Results 1 to 26 of 26

Thread: *Resolved* How do I default a DriveListBox to a UNC path?

  1. #1

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857

    Question *Resolved* How do I default a DriveListBox to a UNC path?

    How do I default a DriveListBox to a UNC path?

    I would like it to default a DriveListBox to a specific path on our network at startup and we all know drive mappings are never consistent in an office (regardless of what the LAN team tells you!). So I want to default it to a UNC path.

    Not really sure how to do this . . .

    Do I need to convert the UNC path to friendly path first? If so, what is the easiest way to convert UNC to friendly?
    Last edited by Armbruster; Oct 10th, 2002 at 11:32 AM.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    i could be wrong.. but i think the drive list box only shows mapped drives...

  3. #3
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244
    with a drivelistbox, you cant put in anything other than local drives on your machine, or mapped drives on your machine. it only queries what's on your local machine

    with a folder listbox, you can put in a unc share, below the computer, but not the computer itself.

    example

    VB Code:
    1. Me.Drive1.drive = "\\MachineName" 'Doesn't work, but doesn't err
    2.  
    3. Me.Dir1.Path = "\\MachineName" 'Errs with Path Not Found
    4.  
    5. Me.Dir1.Path = "\\MachineName\ShareName" 'Succeeds

  4. #4

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    opps, I should have been a bit more clear.

    I am writing a utility that myself and our support desk will use. I know for a fact that they all have this drive mapped on thier machines, but some have it mapped as J:, some have it mapped as R:, etc.

    I did find this:

    VB Code:
    1. Public Declare Function ScLocalPathFromUNC Lib "mapi32x" Alias "ScLocalPathFromUNC@12" _
    2.         (ByVal szUNC As String, _
    3.         ByVal szLocal As String, _
    4.         ByVal cchLocal As Integer) As Integer

    so i tried this:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim strFriendlyPath As String
    4. Dim lngReturn As Long
    5.  
    6. Private Sub Form_Load()
    7.    
    8.     lngReturn = ScLocalPathFromUNC("\\rwdata1\rws", strFriendlyPath, 100)
    9.     MsgBox lngReturn & " : " & strFriendlyPath
    10. End Sub

    I wasn't sure of the usage, but I am getting a return value of 0 so I think I'm using it wrong. Anyone familiar with this api call?
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  5. #5
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244
    So you want to know what mapped drive is mapped to a particular share?

  6. #6

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Yep!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  7. #7

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    I know some api's require that you pass null-terminated strings, but I can't hardly find any information on this api.

    Anybody know any more on this api?
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    can't u just loop the drive list box until you find the share name you are looking for??

  9. #9
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    WHY would MAPI have anything to do with mapped network drives?? I wonder if this is left over from the days of MS-Mail!

    When I use:
    VB Code:
    1. Private Declare Function ScLocalPathFromUNC Lib "mapi32" Alias "ScLocalPathFromUNC@12" _
    2.         (ByVal szUNC As String, _
    3.         ByVal szLocal As String, _
    4.         ByVal cchLocal As Integer) As Integer
    5. ' Note: No X after MAPI32
    6.  
    7. Private Sub Command1_Click()
    8. Dim strFriendlyPath As String
    9. Dim lngReturn As Long
    10.     lngReturn = ScLocalPathFromUNC("\\sl-max\Packages", strFriendlyPath, 255)
    11.     MsgBox lngReturn & " : " & strFriendlyPath
    12. End Sub
    I get a return code of 87 (and a blank return string). Not sure what 87 means, buts its in the right area for a "valid" return code from NET USE.
    NET HELPMSG 87 says "The parameter is incorrect".
    Last edited by JordanChris; Oct 10th, 2002 at 10:06 AM.

  10. #10
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244

    You can enumerate the drives then use this code

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const RESOURCETYPE_ANY = &H0
    4. Private Const RESOURCE_CONNECTED = &H1
    5.  
    6. Private Type NETRESOURCE
    7. dwScope As Long
    8. dwType As Long
    9. dwDisplayType As Long
    10. dwUsage As Long
    11. lpLocalName As Long
    12. lpRemoteName As Long
    13. lpComment As Long
    14. lpProvider As Long
    15. End Type
    16.  
    17. Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias _
    18. "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, _
    19. ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) _
    20. As Long
    21.  
    22. Private Declare Function WNetEnumResource Lib "mpr.dll" Alias _
    23. "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, _
    24. lpBuffer As Any, lpBufferSize As Long) As Long
    25.  
    26. Private Declare Function WNetCloseEnum Lib "mpr.dll" ( _
    27. ByVal hEnum As Long) As Long
    28.  
    29. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" _
    30. (ByVal lpString As Any) As Long
    31.  
    32. Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
    33. (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
    34.  
    35. Private Sub Command1_Click()
    36. MsgBox LetterToUNC("F:") ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    37. End Sub
    38.  
    39. Function LetterToUNC(DriveLetter As String) As String
    40.     Dim hEnum As Long
    41.     Dim NetInfo(1023) As NETRESOURCE
    42.     Dim entries As Long
    43.     Dim nStatus As Long
    44.     Dim LocalName As String
    45.     Dim UNCName As String
    46.     Dim i As Long
    47.     Dim r As Long
    48.    
    49.     ' Begin the enumeration
    50.     nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, _
    51.         0&, ByVal 0&, hEnum)
    52.    
    53.     LetterToUNC = "Drive Letter Not Found"
    54.    
    55.     'Check for success from open enum
    56.     If ((nStatus = 0) And (hEnum <> 0)) Then
    57.         ' Set number of entries
    58.         entries = 1024
    59.        
    60.         ' Enumerate the resource
    61.         nStatus = WNetEnumResource(hEnum, entries, NetInfo(0), _
    62.             CLng(Len(NetInfo(0))) * 1024)
    63.        
    64.         ' Check for success
    65.         If nStatus = 0 Then
    66.             For i = 0 To entries - 1
    67.                 ' Get the local name
    68.                 LocalName = ""
    69.                 If NetInfo(i).lpLocalName <> 0 Then
    70.                     LocalName = Space(lstrlen(NetInfo(i).lpLocalName) + 1)
    71.                     r = lstrcpy(LocalName, NetInfo(i).lpLocalName)
    72.                 End If
    73.                
    74.                 ' Strip null character from end
    75.                 If Len(LocalName) <> 0 Then
    76.                     LocalName = Left(LocalName, (Len(LocalName) - 1))
    77.                 End If
    78.                
    79.                 If UCase$(LocalName) = UCase$(DriveLetter) Then
    80.                     ' Get the remote name
    81.                     UNCName = ""
    82.                     If NetInfo(i).lpRemoteName <> 0 Then
    83.                         UNCName = Space(lstrlen(NetInfo(i).lpRemoteName) _
    84.                         + 1)
    85.                         r = lstrcpy(UNCName, NetInfo(i).lpRemoteName)
    86.                     End If
    87.                    
    88.                     ' Strip null character from end
    89.                     If Len(UNCName) <> 0 Then
    90.                         UNCName = Left(UNCName, (Len(UNCName) - 1))
    91.                     End If
    92.                    
    93.                     ' Return the UNC path to drive
    94.                     LetterToUNC = UNCName
    95.                    
    96.                     ' Exit the loop
    97.                     Exit For
    98.                 End If
    99.             Next i
    100.         End If
    101.     End If
    102.    
    103.     ' End enumeration
    104.     nStatus = WNetCloseEnum(hEnum)
    105. End Function

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    why can't u just do

    VB Code:
    1. Dim i As Integer
    2.     For i = 0 To dbox.ListCount
    3.         Debug.Print dbox.List(i)
    4.     Next

    parse the strings to find the path u need...

  12. #12

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by JordanChris
    WHY would MAPI have anything to do with mapped network drives?? I wonder if this is left over from the days of MS-Mail!

    When I use:
    VB Code:
    1. Private Declare Function ScLocalPathFromUNC Lib "mapi32" Alias "ScLocalPathFromUNC@12" _
    2.         (ByVal szUNC As String, _
    3.         ByVal szLocal As String, _
    4.         ByVal cchLocal As Integer) As Integer
    5. ' Note: No X after MAPI32
    6.  
    7. Private Sub Command1_Click()
    8. Dim strFriendlyPath As String
    9. Dim lngReturn As Long
    10.     lngReturn = ScLocalPathFromUNC("\\sl-max\Packages", strFriendlyPath, 255)
    11.     MsgBox lngReturn & " : " & strFriendlyPath
    12. End Sub
    I get a return code of 87 (and a blank return string). Not sure what 87 means, buts its in the right area for a "valid" return code from NET USE.
    NET HELPMSG 87 says "The parameter is incorrect".
    I search my drive for mapi32x.dll and couldn't find it so I also changed mine to mapi32.dll

    I also get a return code of 87 but not path name. I used dependency walker to verify the function is present just can't get it to work.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  13. #13
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244
    I think you're headed the wrong way using MAPI API's. You have two working solutions from Kleinma and myself.

  14. #14

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857

    Re: You can enumerate the drives then use this code

    Originally posted by WALDO
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const RESOURCETYPE_ANY = &H0
    4. Private Const RESOURCE_CONNECTED = &H1
    5.  
    6. Private Type NETRESOURCE
    7. dwScope As Long
    8. dwType As Long
    9. dwDisplayType As Long
    10. dwUsage As Long
    11. lpLocalName As Long
    12. lpRemoteName As Long
    13. lpComment As Long
    14. lpProvider As Long
    15. End Type
    16.  
    17. Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias _
    18. "WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, _
    19. ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) _
    20. As Long
    21.  
    22. Private Declare Function WNetEnumResource Lib "mpr.dll" Alias _
    23. "WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, _
    24. lpBuffer As Any, lpBufferSize As Long) As Long
    25.  
    26. Private Declare Function WNetCloseEnum Lib "mpr.dll" ( _
    27. ByVal hEnum As Long) As Long
    28.  
    29. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" _
    30. (ByVal lpString As Any) As Long
    31.  
    32. Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
    33. (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
    34.  
    35. Private Sub Command1_Click()
    36. MsgBox LetterToUNC("F:") ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    37. End Sub
    38.  
    39. Function LetterToUNC(DriveLetter As String) As String
    40.     Dim hEnum As Long
    41.     Dim NetInfo(1023) As NETRESOURCE
    42.     Dim entries As Long
    43.     Dim nStatus As Long
    44.     Dim LocalName As String
    45.     Dim UNCName As String
    46.     Dim i As Long
    47.     Dim r As Long
    48.    
    49.     ' Begin the enumeration
    50.     nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, _
    51.         0&, ByVal 0&, hEnum)
    52.    
    53.     LetterToUNC = "Drive Letter Not Found"
    54.    
    55.     'Check for success from open enum
    56.     If ((nStatus = 0) And (hEnum <> 0)) Then
    57.         ' Set number of entries
    58.         entries = 1024
    59.        
    60.         ' Enumerate the resource
    61.         nStatus = WNetEnumResource(hEnum, entries, NetInfo(0), _
    62.             CLng(Len(NetInfo(0))) * 1024)
    63.        
    64.         ' Check for success
    65.         If nStatus = 0 Then
    66.             For i = 0 To entries - 1
    67.                 ' Get the local name
    68.                 LocalName = ""
    69.                 If NetInfo(i).lpLocalName <> 0 Then
    70.                     LocalName = Space(lstrlen(NetInfo(i).lpLocalName) + 1)
    71.                     r = lstrcpy(LocalName, NetInfo(i).lpLocalName)
    72.                 End If
    73.                
    74.                 ' Strip null character from end
    75.                 If Len(LocalName) <> 0 Then
    76.                     LocalName = Left(LocalName, (Len(LocalName) - 1))
    77.                 End If
    78.                
    79.                 If UCase$(LocalName) = UCase$(DriveLetter) Then
    80.                     ' Get the remote name
    81.                     UNCName = ""
    82.                     If NetInfo(i).lpRemoteName <> 0 Then
    83.                         UNCName = Space(lstrlen(NetInfo(i).lpRemoteName) _
    84.                         + 1)
    85.                         r = lstrcpy(UNCName, NetInfo(i).lpRemoteName)
    86.                     End If
    87.                    
    88.                     ' Strip null character from end
    89.                     If Len(UNCName) <> 0 Then
    90.                         UNCName = Left(UNCName, (Len(UNCName) - 1))
    91.                     End If
    92.                    
    93.                     ' Return the UNC path to drive
    94.                     LetterToUNC = UNCName
    95.                    
    96.                     ' Exit the loop
    97.                     Exit For
    98.                 End If
    99.             Next i
    100.         End If
    101.     End If
    102.    
    103.     ' End enumeration
    104.     nStatus = WNetCloseEnum(hEnum)
    105. End Function
    That will enumerate the drives and I found other code to do the same thing, but in both cases it enumerates the drives letters and then finds the unc path.

    I know the unc path, but want to find the letter. I suppose worst case scenerio I could enumerate all drive letters, get thier unc path and compare, but that seems a bit ugly.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  15. #15

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by kleinma
    why can't u just do

    VB Code:
    1. Dim i As Integer
    2.     For i = 0 To dbox.ListCount
    3.         Debug.Print dbox.List(i)
    4.     Next

    parse the strings to find the path u need...
    That will do it but I would obviously need to parse the string. I was hoping I could simply convert.

    VB Code:
    1. Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long

    gets the unc from the drive letter, I can't believe there isn't an api to do the reverse.
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  16. #16
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    strFriendlyPath actually has a square box and : in it.
    So it is being set! but not to anything useful

  17. #17
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244
    Kleinma, correct me if i am wrong, but that what you would HAVE to do. That's what any API that would return that function would do.

    Logically that's exactly how you would find what you want. If you wanted the UNC associated with a drive then you would go directly to the drive and find the information. Since you're going the "other" way trying to match up information, this is how you would do it.

  18. #18
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    i just think it would be easiest.. and parsing the string is nothing more than a simple MID statement.. but if you want to use a bunch of fancy API calls to do it.. I guess you could go that route too... I prefer stripping a drive letter off a string any day though

  19. #19

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by kleinma
    i just think it would be easiest.. and parsing the string is nothing more than a simple MID statement.. but if you want to use a bunch of fancy API calls to do it.. I guess you could go that route too... I prefer stripping a drive letter off a string any day though
    I think you are right. Your method is probably easiest. I just like to use the api when I can do all the work in one line!

    Thanks for the feedback and suggestions guys!!!!
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  20. #20
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244
    Kleinma, i think you're way IS easiest. What Ambruster is complaining about is the fact that he has to go through every drive letter until he finds what he's looking for. Either of our ways, he would have to do that.

    Even if there was an API that would do that for him (I'm not saying there's not, but I don't know of one), that API would be doing the exact same thing we are.

    What's the difference if he does it, or a potentially non-existent API does it?

    That's the point I was trying to make.

  21. #21

    Thread Starter
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857
    Originally posted by WALDO
    Kleinma, i think you're way IS easiest. What Ambruster is complaining about is the fact that he has to go through every drive letter until he finds what he's looking for. Either of our ways, he would have to do that.

    Even if there was an API that would do that for him (I'm not saying there's not, but I don't know of one), that API would be doing the exact same thing we are.

    What's the difference if he does it, or a potentially non-existent API does it?

    That's the point I was trying to make.
    Waldo,

    You're right. Sometimes I just overthink things! Maybe I'm just an api junkie - I need to get my api "fix".

    Here is what I used (based on kleinma's suggestion) and it works great . . .

    VB Code:
    1. Private Sub SetDefaultDrive()
    2. Dim i As Integer
    3.  
    4.     For i = 1 To drvNodepath.ListCount
    5.         If InStr(1, drvNodepath.List(i), "\\rwdata1\rws", vbTextCompare) Then
    6.             drvNodepath.Drive = drvNodepath.List(i)
    7.             Exit For
    8.         End If
    9.     Next i
    10.     dirNodepath.Path = Left(drvNodepath, 1) & ":\node"
    11.  
    12. End Sub
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

  22. #22
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    glad it worked...

    and in response to you and waldo...

    APIs are sweet to do what VB can't on its own... but in certain cases.. a few lines of code just makes more sence.. because

    1) Even though most are, not all APIs are available on all the windows OSs

    2) If someone picks up your code they need to understand the API functions instead of just viewing your LOGIC

    3) You won't see any speed difference... and by coding it yourself.. it makes it more flexible if you want to tweak the code..

    but don't get me wrong.. I use tons of API calls in my app to interface with the OS where needed

  23. #23
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    It might not be a one-liner, but that code looks shorter than the API declaration!

  24. #24
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by JordanChris
    It might not be a one-liner, but that code looks shorter than the API declaration!

  25. #25
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Although I know the problem is solved, I would like to add my contribution anyway.

    I tried to get the ScLocalPathFromUNC function working. It appears that the szUNC needs to be passed in unicode to let it return at least something. The problem is that it always returns "C:\" no matter which network share I pass for szUNC. I guess MAPI should not be used for something like this.

    I wrote a function similar to WALDO's function, but with the use of WNetGetConnection. I first call GetLogicalDrives to find out which drive letters exist, then call GetDriveType to find ut if it is a remote drive, and finally call WNetGetConnection to get the UNC path.

    VB Code:
    1. Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
    2. Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    3. Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
    4.  
    5. Private Const DRIVE_REMOTE = 4
    6.  
    7. Public Function GetLocalDrive(ByVal strRemoteShare As String) As String
    8. Dim lngRet As Long
    9. Dim strBuffer As String
    10. Dim i As Integer
    11. Dim lngDrives As Long
    12. Dim lngLen As Long
    13.     lngDrives = GetLogicalDrives()
    14.     If Right$(strRemoteShare, 1) = "\" Then strRemoteShare = Left$(strRemoteShare, Len(strRemoteShare) - 1)
    15.     For i = 0 To 25
    16.         If lngDrives And (2 ^ i) Then
    17.             lngRet = GetDriveType(Chr(65 + i) & ":\")
    18.             If lngRet = DRIVE_REMOTE Then
    19.                 lngLen = 255
    20.                 strBuffer = Space(lngLen + 1)
    21.                 lngRet = WNetGetConnection(Chr(65 + i) & ":", strBuffer, lngLen)
    22.                 strBuffer = Left$(strBuffer, InStr(strBuffer, Chr(0)) - 1)
    23.                 If UCase(strBuffer) = UCase(strRemoteShare) Then
    24.                     GetLocalDrive = Chr(65 + i) & ":\"
    25.                     Exit For
    26.                 End If
    27.             End If
    28.         End If
    29.     Next
    30. End Function

    EDIT: OK, I might be an API junky as well. I agree that if you use a drivelistbox anyway, you could better loop though the listboxes entries to find the drive you need.
    Last edited by Frans C; Oct 11th, 2002 at 09:16 AM.

  26. #26
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244
    Originally posted by Frans C
    Although I know the problem is solved, I would like to add my contribution anyway.

    I tried to get the ScLocalPathFromUNC function working. It appears that the szUNC needs to be passed in unicode to let it return at least something. The problem is that it always returns "C:\" no matter which network share I pass for szUNC. I guess MAPI should not be used for something like this.

    I wrote a function similar to WALDO's function, but with the use of WNetGetConnection. I first call GetLogicalDrives to find out which drive letters exist, then call GetDriveType to find ut if it is a remote drive, and finally call WNetGetConnection to get the UNC path.
    That's what I would have done if I were coding for efficiency. I'd probably just wrap this up in a dll or something. If I were coding for simplicity, I would use Kleinma's. They're both probably equally effective.

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