|
-
September 12th, 2001, 09:36 AM
#1
How can I use this function?
There is a function in MSDN called WNetOpenEnum().
Can I use it from VB and how if yes?
Thank you.
-
September 12th, 2001, 09:57 AM
#2
Re: How can I use this function?
Add a listbox to a form and try this 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
public Sub Form_Load()
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)
'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
'added the [] to seperate on printout only
List1.AddItem UNCName & " is drive " & LocalName
' Exit the loop
'Exit for
'End If
next i
End If
End If
' End enumeration
nStatus = WNetCloseEnum(hEnum)
End Sub
-
September 12th, 2001, 09:59 AM
#3
Re: How can I use this function?
Here's an example for the function from API-Guide. Visit www.allapi.net for the API Guide.
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
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 = DriveLetter
'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
'added the [] to seperate on printout only
LetterToUNC = UNCName
' Exit the loop
Exit for
End If
next i
End If
End If
' End enumeration
nStatus = WNetCloseEnum(hEnum)
End Function
private Sub Form_Load()
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: [email protected]
'-> This sample was created by Donald Grover
MsgBox "C: UNC path: " + LetterToUNC("C:")
End Sub
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
|