Jump to content

_WinNet_OpenEnum & _WinNet_EnumResource are evil


Zinthose
 Share

Recommended Posts

I'm trying to get a list of all network systems including their comments for inventory purposes. But I keep hitting the same brick wall :)

_WinNet_OpenEnum & _WinNet_EnumResource are evil!

I know these are the functions I need to do the task but for the life of me I cant get them to work.. :) !!

I found a VB6 example at "How To List Local Network Connections with WNetEnumResources" and have tried to translate it to work in AutoIt.

Anyone out there willing to show me an AutoIt example this one is just plain hard.

--- TTFN

Link to comment
Share on other sites

I'm still banging my head on this one... :)

Here is what I have so far...

#include <WinNet.au3>
#include <Memory.au3>

Local $pResource, $Resource
Local $RC

$Resource = DllStructCreate($tagNETRESOURCE)

DllStructSetData($Resource, "Usage", $RESOURCEUSAGE_CONTAINER)
;DllStructSetData($Resource, "RemoteName", 0x0)
$pResource = DllStructGetPtr($Resource)

;## Open Enum
    Local $iScope = 1
    Local $iType = 0
    Local $iUsage = 0
    Local $hEnum = 0

;## Enum Resource
    Local $iCount = 0xFFFFFFFF
    Local $iBufSize = 16384

    $RC = _WinNet_OpenEnum($iScope, $iType, $iUsage, 0, $hEnum)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $RC = ' & $RC & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hEnum = ' & $hEnum & @CRLF) ;### Debug Console

    If $RC <> True Then
        Exit 1
    EndIf

    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : DllStructGetSize($Resource) = ' & DllStructGetSize($Resource) & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iBufSize / DllStructGetSize($Resource) = ' & $iBufSize / DllStructGetSize($Resource)& @CRLF) ;### Debug Console

;## Enum Resource
    $Buffer = DllStructCreate("Buffer ptr[" & $iBufSize / DllStructGetSize($Resource)  & "]")
    $pBuffer = DllStructGetPtr($Buffer)

;## Pull hair out here!!!!
    $RC = _WinNet_EnumResource($hEnum, $iCount, $pBuffer, $iBufSize)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iBufSize = ' & $iBufSize & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $RC = ' & $RC & @CRLF) ;### Debug Console
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iCount = ' & $iCount & @CRLF) ;### Debug Console

If $RC <> 0 Then
    Exit 2
EndIf

CODE
@@ Debug(24) : $RC = True

>Error code: 0

@@ Debug(25) : $hEnum = 9108840

@@ Debug(31) : DllStructGetSize($Resource) = 32

@@ Debug(32) : $iBufSize / DllStructGetSize($Resource) = 512

@@ Debug(39) : $iBufSize = 16384

>Error code: 487

@@ Debug(40) : $RC = False

@@ Debug(41) : $iCount = -1

+>09:36:18 AutoIT3.exe ended.rc:0

I'm pretty sure it's an issue with $Buffer but I haven't been able to figure it out.

--- TTFN

Link to comment
Share on other sites

  • 9 months later...

I'm still banging my head on this one... Posted Image

Here is what I have so far...

#include <WinNet.au3>
#include <Memory.au3>

Local $pResource, $Resource
Local $RC

$Resource = DllStructCreate($tagNETRESOURCE)

DllStructSetData($Resource, "Usage", $RESOURCEUSAGE_CONTAINER)
;DllStructSetData($Resource, "RemoteName", 0x0)
$pResource = DllStructGetPtr($Resource)

;## Open Enum
Local $iScope = 1
Local $iType = 0
Local $iUsage = 0
Local $hEnum = 0

;## Enum Resource
Local $iCount = 0xFFFFFFFF
Local $iBufSize = 16384

$RC = _WinNet_OpenEnum($iScope, $iType, $iUsage, 0, $hEnum)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $RC = ' & $RC & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hEnum = ' & $hEnum & @CRLF) ;### Debug Console

If $RC <> True Then
Exit 1
EndIf

ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : DllStructGetSize($Resource) = ' & DllStructGetSize($Resource) & @CRLF) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iBufSize / DllStructGetSize($Resource) = ' & $iBufSize / DllStructGetSize($Resource)& @CRLF) ;### Debug Console

;## Enum Resource
$Buffer = DllStructCreate("Buffer ptr[" & $iBufSize / DllStructGetSize($Resource)  & "]")
$pBuffer = DllStructGetPtr($Buffer)

;## Pull hair out here!!!!
$RC = _WinNet_EnumResource($hEnum, $iCount, $pBuffer, $iBufSize)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iBufSize = ' & $iBufSize & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $RC = ' & $RC & @CRLF) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iCount = ' & $iCount & @CRLF) ;### Debug Console

If $RC <> 0 Then
Exit 2
EndIf

CODE@@ Debug(24) : $RC = True

>Error code: 0

@@ Debug(25) : $hEnum = 9108840

@@ Debug(31) : DllStructGetSize($Resource) = 32

@@ Debug(32) : $iBufSize / DllStructGetSize($Resource) = 512

@@ Debug(39) : $iBufSize = 16384

>Error code: 487

@@ Debug(40) : $RC = False

@@ Debug(41) : $iCount = -1

+>09:36:18 AutoIT3.exe ended.rc:0

I'm pretty sure it's an issue with $Buffer but I haven't been able to figure it out.

Were you able to resolve the issue? If yes can you share with me how did you get _WinNet_EnumResource to work and extract data from the buffer?

I used _MemGlobalAlloc to create the buffer, but I can't figure out how to retrieve data from this buffer.

Link to comment
Share on other sites

Try that:

#include <WinNet.au3>
#include <Memory.au3>

Local $pResource, $Resource
Local $RC

$Resource = DllStructCreate($tagNETRESOURCE)

DllStructSetData($Resource, "Usage", $RESOURCEUSAGE_CONTAINER)
DllStructSetData($Resource, "RemoteName", 0x0)
$pResource = DllStructGetPtr($Resource)

;## Open Enum
Local $iUsage = 0
Local $iScope = 1
Local $iType = 0
Local $hEnum = 0

;## Enum Resource
Local $iCount = 0xFFFFFFFF
Local $iBufSize = 1024

$RC = _WinNet_OpenEnum($iScope, $iType, $iUsage, 0, $hEnum)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $RC = ' & $RC & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hEnum = ' & $hEnum & @CRLF) ;### Debug Console

If $RC <> True Then
    Exit 1
EndIf

ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : DllStructGetSize($Resource) = ' & DllStructGetSize($Resource) & @CRLF) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iBufSize / DllStructGetSize($Resource) = ' & $iBufSize / DllStructGetSize($Resource) & @CRLF) ;### Debug Console

;## Enum Resource
;~ $Buffer = DllStructCreate("Buffer ptr[" & $iBufSize / DllStructGetSize($Resource) & "]")
$Buffer = DllStructCreate("ubyte[" & $iBufSize & "]")
$pBuffer = DllStructGetPtr($Buffer)

;## Pull hair out here!!!!
$RC = _WinNet_EnumResource($hEnum, $iCount, $pBuffer, $iBufSize)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iBufSize = ' & $iBufSize & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $RC = ' & $RC & @CRLF) ;### Debug Console
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iCount = ' & $iCount & @CRLF) ;### Debug Console

If $RC <> 0 Then
    Exit 2
EndIf

$p = $pBuffer
For $i = 1 To $iCount
    ConsoleWrite("Resource #" & $i & ": ")
    _tagNETRESOURCE_Display($p)
    $p += DllStructGetSize($Resource)
Next

Func _tagNETRESOURCE_Display($ptr)
    $data = DllStructCreate($tagNETRESOURCE, $ptr)
    ConsoleWrite('Scope = ' & DllStructGetData($data, 'Scope') & " / ")
    ConsoleWrite('Type = ' & DllStructGetData($data, 'Type') & " / ")
    ConsoleWrite('DisplayType = ' & DllStructGetData($data, 'DisplayType') & " / ")
    ConsoleWrite('Usage = ' & DllStructGetData($data, 'Usage') & " / ")
    ConsoleWrite('LocalName = ' & _PointertoString(DllStructGetData($data, 'LocalName')) & " / ")
    ConsoleWrite('RemoteName = ' & _PointertoString(DllStructGetData($data, 'RemoteName')) & " / ")
    ConsoleWrite('Comment = ' & _PointertoString(DllStructGetData($data, 'Comment')) & " / ")
    ConsoleWrite('Provider = ' & _PointertoString(DllStructGetData($data, 'Provider')) & @CRLF)
EndFunc   ;==>_tagNETRESOURCE_Display

Func _PointertoString($ptr)
    If Not IsPtr($ptr) Or $ptr = 0 Then Return ""
    Local $str = ""
    While 1
        $data = DllStructCreate("ubyte", $ptr)
        $value = DllStructGetData($data, 1)
        If $value = 0 Then ExitLoop
        If $value > 31 Then
            $str &= Chr($value)
        Else
            $str &= '.'
        EndIf
        $ptr += 1
    WEnd
    Return $str
EndFunc   ;==>_PointertoString

Changes to original code:

- set $iBufSize to 1024 as it seems to be enough

- set $Buffer struct to contain $iBufSize bytes

- added loop to display all results

- added function to display 1 result

- added function to return the chr(0)-terminated string at a specific location

There's probably a better way to write it but this works and should get you back on your path...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...