|
-
Oct 10th, 2002, 08:57 AM
#1
Thread Starter
Fanatic Member
*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!
-
Oct 10th, 2002, 09:01 AM
#2
i could be wrong.. but i think the drive list box only shows mapped drives...
-
Oct 10th, 2002, 09:06 AM
#3
Addicted Member
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:
Me.Drive1.drive = "\\MachineName" 'Doesn't work, but doesn't err
Me.Dir1.Path = "\\MachineName" 'Errs with Path Not Found
Me.Dir1.Path = "\\MachineName\ShareName" 'Succeeds
-
Oct 10th, 2002, 09:13 AM
#4
Thread Starter
Fanatic Member
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:
Public Declare Function ScLocalPathFromUNC Lib "mapi32x" Alias "ScLocalPathFromUNC@12" _
(ByVal szUNC As String, _
ByVal szLocal As String, _
ByVal cchLocal As Integer) As Integer
so i tried this:
VB Code:
Option Explicit
Dim strFriendlyPath As String
Dim lngReturn As Long
Private Sub Form_Load()
lngReturn = ScLocalPathFromUNC("\\rwdata1\rws", strFriendlyPath, 100)
MsgBox lngReturn & " : " & strFriendlyPath
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!
-
Oct 10th, 2002, 09:21 AM
#5
Addicted Member
So you want to know what mapped drive is mapped to a particular share?
-
Oct 10th, 2002, 09:33 AM
#6
Thread Starter
Fanatic Member
"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!
-
Oct 10th, 2002, 09:36 AM
#7
Thread Starter
Fanatic Member
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!
-
Oct 10th, 2002, 09:47 AM
#8
can't u just loop the drive list box until you find the share name you are looking for??
-
Oct 10th, 2002, 10:00 AM
#9
Frenzied Member
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:
Private Declare Function ScLocalPathFromUNC Lib "mapi32" Alias "ScLocalPathFromUNC@12" _
(ByVal szUNC As String, _
ByVal szLocal As String, _
ByVal cchLocal As Integer) As Integer
' Note: No X after MAPI32
Private Sub Command1_Click()
Dim strFriendlyPath As String
Dim lngReturn As Long
lngReturn = ScLocalPathFromUNC("\\sl-max\Packages", strFriendlyPath, 255)
MsgBox lngReturn & " : " & strFriendlyPath
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.
-
Oct 10th, 2002, 10:04 AM
#10
Addicted Member
You can enumerate the drives then use this code
VB Code:
Option Explicit
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCE_CONNECTED = &H1
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As Long
lpRemoteName As Long
lpComment As Long
lpProvider As Long
End Type
Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias _
"WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, _
ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) _
As Long
Private Declare Function WNetEnumResource Lib "mpr.dll" Alias _
"WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, _
lpBuffer As Any, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" ( _
ByVal hEnum As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" _
(ByVal lpString As Any) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
(ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Private Sub Command1_Click()
MsgBox LetterToUNC("F:") ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
End Sub
Function LetterToUNC(DriveLetter As String) As String
Dim hEnum As Long
Dim NetInfo(1023) As NETRESOURCE
Dim entries As Long
Dim nStatus As Long
Dim LocalName As String
Dim UNCName As String
Dim i As Long
Dim r As Long
' Begin the enumeration
nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, _
0&, ByVal 0&, hEnum)
LetterToUNC = "Drive Letter Not Found"
'Check for success from open enum
If ((nStatus = 0) And (hEnum <> 0)) Then
' Set number of entries
entries = 1024
' Enumerate the resource
nStatus = WNetEnumResource(hEnum, entries, NetInfo(0), _
CLng(Len(NetInfo(0))) * 1024)
' Check for success
If nStatus = 0 Then
For i = 0 To entries - 1
' Get the local name
LocalName = ""
If NetInfo(i).lpLocalName <> 0 Then
LocalName = Space(lstrlen(NetInfo(i).lpLocalName) + 1)
r = lstrcpy(LocalName, NetInfo(i).lpLocalName)
End If
' Strip null character from end
If Len(LocalName) <> 0 Then
LocalName = Left(LocalName, (Len(LocalName) - 1))
End If
If UCase$(LocalName) = UCase$(DriveLetter) Then
' Get the remote name
UNCName = ""
If NetInfo(i).lpRemoteName <> 0 Then
UNCName = Space(lstrlen(NetInfo(i).lpRemoteName) _
+ 1)
r = lstrcpy(UNCName, NetInfo(i).lpRemoteName)
End If
' Strip null character from end
If Len(UNCName) <> 0 Then
UNCName = Left(UNCName, (Len(UNCName) - 1))
End If
' Return the UNC path to drive
LetterToUNC = UNCName
' Exit the loop
Exit For
End If
Next i
End If
End If
' End enumeration
nStatus = WNetCloseEnum(hEnum)
End Function
-
Oct 10th, 2002, 10:08 AM
#11
why can't u just do
VB Code:
Dim i As Integer
For i = 0 To dbox.ListCount
Debug.Print dbox.List(i)
Next
parse the strings to find the path u need...
-
Oct 10th, 2002, 10:48 AM
#12
Thread Starter
Fanatic Member
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:
Private Declare Function ScLocalPathFromUNC Lib "mapi32" Alias "ScLocalPathFromUNC@12" _
(ByVal szUNC As String, _
ByVal szLocal As String, _
ByVal cchLocal As Integer) As Integer
' Note: No X after MAPI32
Private Sub Command1_Click()
Dim strFriendlyPath As String
Dim lngReturn As Long
lngReturn = ScLocalPathFromUNC("\\sl-max\Packages", strFriendlyPath, 255)
MsgBox lngReturn & " : " & strFriendlyPath
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!
-
Oct 10th, 2002, 11:00 AM
#13
Addicted Member
I think you're headed the wrong way using MAPI API's. You have two working solutions from Kleinma and myself.
-
Oct 10th, 2002, 11:00 AM
#14
Thread Starter
Fanatic Member
Re: You can enumerate the drives then use this code
Originally posted by WALDO
VB Code:
Option Explicit
Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCE_CONNECTED = &H1
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As Long
lpRemoteName As Long
lpComment As Long
lpProvider As Long
End Type
Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias _
"WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, _
ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) _
As Long
Private Declare Function WNetEnumResource Lib "mpr.dll" Alias _
"WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, _
lpBuffer As Any, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" ( _
ByVal hEnum As Long) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" _
(ByVal lpString As Any) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" _
(ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
Private Sub Command1_Click()
MsgBox LetterToUNC("F:") ' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
End Sub
Function LetterToUNC(DriveLetter As String) As String
Dim hEnum As Long
Dim NetInfo(1023) As NETRESOURCE
Dim entries As Long
Dim nStatus As Long
Dim LocalName As String
Dim UNCName As String
Dim i As Long
Dim r As Long
' Begin the enumeration
nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, _
0&, ByVal 0&, hEnum)
LetterToUNC = "Drive Letter Not Found"
'Check for success from open enum
If ((nStatus = 0) And (hEnum <> 0)) Then
' Set number of entries
entries = 1024
' Enumerate the resource
nStatus = WNetEnumResource(hEnum, entries, NetInfo(0), _
CLng(Len(NetInfo(0))) * 1024)
' Check for success
If nStatus = 0 Then
For i = 0 To entries - 1
' Get the local name
LocalName = ""
If NetInfo(i).lpLocalName <> 0 Then
LocalName = Space(lstrlen(NetInfo(i).lpLocalName) + 1)
r = lstrcpy(LocalName, NetInfo(i).lpLocalName)
End If
' Strip null character from end
If Len(LocalName) <> 0 Then
LocalName = Left(LocalName, (Len(LocalName) - 1))
End If
If UCase$(LocalName) = UCase$(DriveLetter) Then
' Get the remote name
UNCName = ""
If NetInfo(i).lpRemoteName <> 0 Then
UNCName = Space(lstrlen(NetInfo(i).lpRemoteName) _
+ 1)
r = lstrcpy(UNCName, NetInfo(i).lpRemoteName)
End If
' Strip null character from end
If Len(UNCName) <> 0 Then
UNCName = Left(UNCName, (Len(UNCName) - 1))
End If
' Return the UNC path to drive
LetterToUNC = UNCName
' Exit the loop
Exit For
End If
Next i
End If
End If
' End enumeration
nStatus = WNetCloseEnum(hEnum)
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!
-
Oct 10th, 2002, 11:03 AM
#15
Thread Starter
Fanatic Member
Originally posted by kleinma
why can't u just do
VB Code:
Dim i As Integer
For i = 0 To dbox.ListCount
Debug.Print dbox.List(i)
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:
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!
-
Oct 10th, 2002, 11:09 AM
#16
Frenzied Member
strFriendlyPath actually has a square box and : in it.
So it is being set! but not to anything useful
-
Oct 10th, 2002, 11:13 AM
#17
Addicted Member
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.
-
Oct 10th, 2002, 11:24 AM
#18
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
-
Oct 10th, 2002, 11:32 AM
#19
Thread Starter
Fanatic Member
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!
-
Oct 10th, 2002, 11:34 AM
#20
Addicted Member
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.
-
Oct 10th, 2002, 02:22 PM
#21
Thread Starter
Fanatic Member
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:
Private Sub SetDefaultDrive()
Dim i As Integer
For i = 1 To drvNodepath.ListCount
If InStr(1, drvNodepath.List(i), "\\rwdata1\rws", vbTextCompare) Then
drvNodepath.Drive = drvNodepath.List(i)
Exit For
End If
Next i
dirNodepath.Path = Left(drvNodepath, 1) & ":\node"
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!
-
Oct 10th, 2002, 02:28 PM
#22
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
-
Oct 11th, 2002, 03:47 AM
#23
Frenzied Member
It might not be a one-liner, but that code looks shorter than the API declaration!
-
Oct 11th, 2002, 07:24 AM
#24
Originally posted by JordanChris
It might not be a one-liner, but that code looks shorter than the API declaration!
-
Oct 11th, 2002, 09:11 AM
#25
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:
Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long
Private Const DRIVE_REMOTE = 4
Public Function GetLocalDrive(ByVal strRemoteShare As String) As String
Dim lngRet As Long
Dim strBuffer As String
Dim i As Integer
Dim lngDrives As Long
Dim lngLen As Long
lngDrives = GetLogicalDrives()
If Right$(strRemoteShare, 1) = "\" Then strRemoteShare = Left$(strRemoteShare, Len(strRemoteShare) - 1)
For i = 0 To 25
If lngDrives And (2 ^ i) Then
lngRet = GetDriveType(Chr(65 + i) & ":\")
If lngRet = DRIVE_REMOTE Then
lngLen = 255
strBuffer = Space(lngLen + 1)
lngRet = WNetGetConnection(Chr(65 + i) & ":", strBuffer, lngLen)
strBuffer = Left$(strBuffer, InStr(strBuffer, Chr(0)) - 1)
If UCase(strBuffer) = UCase(strRemoteShare) Then
GetLocalDrive = Chr(65 + i) & ":\"
Exit For
End If
End If
End If
Next
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.
-
Oct 11th, 2002, 09:15 AM
#26
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|