Jump to content

Need help in writing corect Dll Call to 'iphlpapi.dll' (GetIpNetTable)


Houl777
 Share

Recommended Posts

Good day

I want to get ARP Table from iphlpapi.dll (function GetIpNetTable).

This is my first Dll call for all time so i need help.

According to MSDN

DWORD GetIpNetTable(
_Out_   PMIB_IPNETTABLE pIpNetTable,
_Inout_ PULONG pdwSize,
_In_     BOOL bOrder
);

I wrote this construction:

DllCall("iphlpapi.dll", "int", "GetIpNetTable", _
type1, param1,_
type2, param2,_
"bool", "True")

Which type,parameters or structures i need to add?

Maybe someone already have working dll call?

Edited by Houl777
Link to comment
Share on other sites

Perhaps the types are INT_PTR, int and bool (you know that).

#include <WinAPI.au3>

$MIB_IPNETROW = DllStructCreate('dword;dword;byte;dword;dword')

$pIpNetTable = DllStructCreate('dword;ptr')
DllStructSetData($pIpNetTable, 2, DllStructGetPtr($MIB_IPNETROW))

$RESULT = DllCall("iphlpapi.dll", "dword", "GetIpNetTable", "INT_PTR", DllStructGetPtr($pIpNetTable), "int", DllStructGetSize($pIpNetTable), 'bool', True)

msgbox(64, "", $RESULT[0] & '. ' & _WinAPI_GetLastErrorMessage())

Leaving the rest to you... :guitar:

Edited by MKISH

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

I've correct code now it returns error 122 ('All Work'), and 0 entires.

$MIB_IPNETROW = DllStructCreate('dword;dword;byte;dword;dword')
$pIpNetTable = DllStructCreate('dword;ptr')
DllStructSetData($pIpNetTable, 2, DllStructGetPtr($MIB_IPNETROW))

$dwSize = DllStructCreate("dword")

$RESULT = DllCall("iphlpapi.dll", "int", "GetIpNetTable", "ptr", DllStructGetPtr($pIpNetTable), "ptr", DllStructGetPtr($dwSize), "int", 1)
$numARPentries = DllStructGetData($pIpNetTable, 1) ; number of entries

msgbox(64, "", $RESULT[0] & '. ' & _WinAPI_GetLastErrorMessage())

Why it returns 0 entires?

And how am i can get ARP Table from $pIpNetTable?

Edited by Houl777
Link to comment
Share on other sites

I think the buffer needs to point to $mib_ipnetrow. $dwSize is filled with a new value indicating the buffersize is not right.

#include <winapi.au3>

$MIB_IPNETROW = DllStructCreate('dword;dword;byte[32];dword;dword')
$pIpNetTable = DllStructCreate('dword;ptr')

DllStructSetData($pIpNetTable, 2, DllStructGetPtr($MIB_IPNETROW))

$dwSize = DllStructCreate("dword")

$RESULT = DllCall("iphlpapi.dll", "int", "GetIpNetTable", "ptr", DllStructGetPtr($pIpNetTable), "ptr", DllStructGetPtr($dwSize), "int", 1)
$numARPentries = DllStructGetData($pIpNetTable, 1) ; number of entries

ConsoleWrite('Size needed = ' & DllStructGetData($dwSize, 1) & @LF)

msgbox(64, $numARPentries, $RESULT[0] & '. ' & _WinAPI_GetLastErrorMessage())

Edit: Scratch what I said about pointing to $mib_ipnetrow. That wouldnt be right.

Edited by Beege
Link to comment
Share on other sites

After modifying a few examples, I've gotten this code:

$aCall = DllCall("iphlpapi.dll", "dword", "GetIpNetTable", "ptr*", 0, "dword*", 0, "int", 1)
$iSize = $aCall[2]

$MIB_IPNETROW = 'dword;dword;byte[' & $iSize & '];dword;dword'
$pIpNetTable = DllStructCreate("dword;" & $MIB_IPNETROW)

$aCall = DllCall("iphlpapi.dll", "dword", "GetIpNetTable", "ptr", DllStructGetPtr($pIpNetTable), "dword*", $iSize, "int", 1)
ConsoleWrite ((DllStructGetData($pIpNetTable,1,1)-2)&@CRLF)

Now it shows the correct number of items in the ARP table

How am i can get it from this structure ?

Edited by Houl777
Link to comment
Share on other sites

You need to form an array of structures. The problem is variable length of the array so you have to create raw binary structure tha wraps complete MIB_IPNETTABLE structure. Once you do that it's only a matter of collecting by interpretting chunks of raw structure as array of MIB_IPNETROW's and printing interesting data.

I understand that there might be difficulties for you to understand what I'm talking about here so I wrote a function to show you how that few sentences look written in AutoIt as opposed to English:

PrintIpNetTable()


Func PrintIpNetTable()

    ; Start with initial call to calculate the size of the sructure
    Local $aCall = DllCall("iphlpapi.dll", "dword", "GetIpNetTable", _
            "ptr*", 0, _
            "dword*", 0, _
            "bool", 1)
    ; Check for errors
    If @error Then Return SetError(1, 0, 0)
    If $aCall[0] <> 122 Then Return SetError(2, 0, 0) ; ERROR_INSUFFICIENT_BUFFER

    ; Read the size
    Local $iSize = $aCall[2]
    ; Allocate that much
    Local $tByteStructure = DllStructCreate("byte[" & $iSize & "]")
    ; Get pointer
    Local $pPointer = DllStructGetPtr($tByteStructure)

    ; Now fill the struct
    $aCall = DllCall("iphlpapi.dll", "dword", "GetIpNetTable", _
            "ptr", $pPointer, _
            "dword*", $iSize, _
            "bool", 1)

    ; Interpret as modified MIB_IPNETTABLE structure to read the number of entries
    Local $tMIB_IPNETTABLE_NumEnries = DllStructCreate("dword dwNumEntries;", $pPointer)
    ; Read that number
    Local $iNumEntries = DllStructGetData($tMIB_IPNETTABLE_NumEnries, "dwNumEntries")

    $pPointer += 4 ; skip the size of that dword

    Local Const $MAXLEN_PHYSADDR = 8
    Local $tMIB_IPNETROW

    ; Loop through the array of structures printing read data
    For $i = 0 To $iNumEntries - 1
        $tMIB_IPNETROW = DllStructCreate("dword dwIndex;" & _
                "dword dwPhysAddrLen;" & _
                "byte bPhysAddr[" & $MAXLEN_PHYSADDR & "];" & _
                "dword dwAddr;" & _
                "dword dwType;", _
                $pPointer)

        ; Print read
        ConsoleWrite("+Index " & DllStructGetData($tMIB_IPNETROW, "dwIndex") & ":" & @CRLF & _
                @TAB & "Physical address = " & BinToMAC(DllStructGetData($tMIB_IPNETROW, "bPhysAddr"), DllStructGetData($tMIB_IPNETROW, "dwPhysAddrLen")) & @CRLF & _
                @TAB & "IPv4 address = " & NumToIP(DllStructGetData($tMIB_IPNETROW, "dwAddr")) & @CRLF & _
                @TAB & "Type = " & ARPType(DllStructGetData($tMIB_IPNETROW, "dwType")) & @CRLF & @CRLF)

        ; Skip this struct and continue looping
        $pPointer += DllStructGetSize($tMIB_IPNETROW)
    Next

    ; Bye, bye...
EndFunc   ;==>PrintIpNetTable


; Few helper functions
Func NumToIP($iIP)
    Return Int(BinaryMid($iIP, 1, 1)) & "." & Int(BinaryMid($iIP, 2, 1)) & "." & Int(BinaryMid($iIP, 3, 1)) & "." & Int(BinaryMid($iIP, 4, 1))
EndFunc

Func BinToMAC($bIn, $iSeg)
    If $iSeg = 0 Then Return "00-00-00-00-00-00"
    Local $sOut
    For $i = 1 To $iSeg
        $sOut &= Hex(BinaryMid($bIn, $i, 1)) & "-"
    Next
    Return StringTrimRight($sOut, 1)
EndFunc

Func ARPType($iType)
    Local Static $aType[5] = ["", "Unknown", "Invalid", "Dynamic", "Static"]
    If $iType < 1 Or $iType > 4 Then $iType = 1
    Return $aType[$iType]
EndFunc

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

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...