I couldn’t seem to find any other examples so I decided to generate one from a C# template I found on google.

Imports System.Runtime.InteropServices

Module Module1
    <DllImport("iphlpapi.dll", SetLastError:=True)>
    Private Function GetTcpTable2(ByVal pTcpTable As Byte(), <Out> ByRef pdwSize As Integer, ByVal bOrder As Boolean) As Integer

    End Function
    Sub Main()
        Dim pdwSize As Integer = 20000
        Dim buffer As Byte() = New Byte(pdwSize - 1) {}
        Dim res As Integer = GetTcpTable2(buffer, pdwSize, True)

        If res <> 0 Then
            buffer = New Byte(pdwSize - 1) {}
            res = GetTcpTable2(buffer, pdwSize, True)
            If res <> 0 Then Throw New Exception(res)
        End If

        Dim nOffset As Integer = 0
        Dim dwNumEntries = Convert.ToInt32(buffer(nOffset))
        nOffset += 4
        Console.WriteLine("total connection: {0}", dwNumEntries)

        For i As Integer = 0 To dwNumEntries - 1
            Console.WriteLine("*****************")
            Dim st As Integer = Convert.ToInt32(buffer(nOffset))
            Console.WriteLine("state: {0}", st)
            nOffset += 4
            Dim LocalAdrr As String = buffer(nOffset).ToString() & "." & buffer(nOffset + 1).ToString() & "." & buffer(nOffset + 2).ToString() & "." & buffer(nOffset + 3).ToString()
            Console.WriteLine("local ip: {0}", LocalAdrr)
            nOffset += 4

            Dim LocalPort As String = (CInt(buffer(nOffset)) << 8)
            LocalPort += (CInt(buffer(nOffset + 1)))
            LocalPort += (CInt(buffer(nOffset + 2)) << 24)
            LocalPort += (CInt(buffer(nOffset + 3)) << 16)
            Console.WriteLine("Local port: {0}", LocalPort)
            nOffset += 4

            Dim RemoteAdrr As String = buffer(nOffset).ToString() & "." + buffer(nOffset + 1).ToString() & "." + buffer(nOffset + 2).ToString() & "." + buffer(nOffset + 3).ToString()
            Console.WriteLine("remote ip: {0}", RemoteAdrr)
            nOffset += 4

            Dim RemotePort As String = (CInt(buffer(nOffset)) << 8)
            RemotePort += (CInt(buffer(nOffset + 1)))
            RemotePort += (CInt(buffer(nOffset + 2)) << 24)
            RemotePort += (CInt(buffer(nOffset + 3)) << 16)
            Console.WriteLine("remote port: {0}", RemotePort)
            nOffset += 4

            Dim pid As Integer = BitConverter.ToInt32(buffer.Skip(nOffset).Take(4).ToArray(), 0)
            Console.WriteLine("pid: {0}", pid)
            nOffset += 4
            Dim OffloadState As Integer = Convert.ToInt32(buffer(nOffset))
            Console.WriteLine("OffloadState: {0}", OffloadState)
            nOffset += 4
        Next

        Console.ReadKey()
    End Sub

End Module

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax