Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need some help with WNet

Status
Not open for further replies.

SaMaLaKo

Technical User
Feb 16, 2005
178
BE
Hi all,

I'm creating a function to enumerate all networkdrives.
but it won't work.
I copied the most from the M$ Website (Here)

First of all the Pointertostring function didn't work
(p should be type long, but lplocalname is a string --> conflict)but solved this tempararily.

Now the script wont go further then the highlighted text.
It returns me errorcode 87.

Private Const ERROR_INVALID_PARAMETER = 87& 'Either the dwScope or the dwType parameter is invalid, or there is an invalid combination of parameters.

But I can't find what is wrong. Can you guys see it?

Code:
Private Declare Function GlobalAlloc Lib "KERNEL32" ( _
    ByVal wFlags As Long, _
    ByVal dwBytes As Long) As Long
    
Private Declare Function GlobalFree Lib "KERNEL32" ( _
    ByVal hMem As Long) As Long
    
Private Declare Sub CopyMemory Lib "KERNEL32" _
    Alias "RtlMoveMemory" ( _
    hpvDest As Any, _
    hpvSource As Any, _
    ByVal cbCopy As Long)
    
Private Declare Function CopyPointer2String Lib "KERNEL32" _
    Alias "lstrcpyA" ( _
    ByVal NewString As String, _
    ByVal OldString As Long) As Long
    
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, _
    ByVal lpBuffer As Long, _
    lpBufferSize As Long) As Long
    
Private Declare Function WNetCloseEnum Lib "mpr.dll" ( _
    ByVal hEnum As Long) As Long

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
Private Const GMEM_FIXED = &H0
Private Const GMEM_ZEROINIT = &H40
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)
Private Const NO_ERROR = 0
'RESOURCE ENUMERATION.
Private Const RESOURCE_CONNECTED = &H1
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCEUSAGE_CONTAINER = &H2

Public Function ListNetworkDrives() As Variant
    Dim hEnum As Long, lpBuff As Long, nr As NETRESOURCE
    Dim cbBuff As Long, cCount As Long
    Dim p As Long, res As Long, i As Long
    Dim arReturnData As Variant
    
    'Setup the NETRESOURCE input structure.
    nr.dwUsage = RESOURCEUSAGE_CONTAINER
    nr.lpRemoteName = 0
    cbBuff = 1000
    cCount = &HFFFFFFFF
    
    ReDim arReturnData(0)
    'Open a Net enumeration operation handle: hEnum.
    [Highlight]res = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_DISK, 0, nr, hEnum)[/Highlight]
    If res = 0 Then
        'Create a buffer large enough for the results.
        '1000 bytes should be sufficient.
        lpBuff = GlobalAlloc(GPTR, cbBuff)
        'Call the enumeration function.
        res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
        If res = 0 Then
            p = lpBuff
            'WNetEnumResource fills the buffer with an array of
            'NETRESOURCE structures. Walk through the list and print
            'each local and remote name.
            For i = 1 To cCount
                CopyMemory nr, ByVal p, LenB(nr)
                p = p + LenB(nr)
                If Not arReturnData(0) = vbNullString Then ReDim Preserve arReturnData(UBound(arReturnData) + 1)
                arReturnData(UBound(arReturnData)) = PointerToString(nr.lpLocalName) & ";" & PointerToString(nr.lpRemoteName)
            Next i
        Else
            arReturnData(0) = "ERROR: " & TranslateErrInfo(res)
        End If
        If lpBuff <> 0 Then GlobalFree (lpBuff)
            WNetCloseEnum (hEnum) 'Close the enumeration
        Else
            arReturnData(0) = "ERROR: " & TranslateErrInfo(res)
    End If
    ListNetworkDrives = arReturnData
End Function

Private Function PointerToString(p As String) As String
    'The values returned in the NETRESOURCE structures are pointers to
    'ANSI strings so they need to be converted to Visual Basic Strings.
    Dim s As String
    s = String(255, Chr$(0))
    CopyPointer2String s, p
    PointerToString = Left(s, InStr(s, Chr$(0)) - 1)
End Function

Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
Ignore it guys(and girls?) found the problem.
was something in the declaration of the type....

Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top