I couldn’t seem to find any other examples so I decided to generate one from a C# template I found on google.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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 |