As it sometimes happens this script provided a convenient integration test for another project currently in the works. It is not production-ready by any stretch because it is lacking error handling, proper documentation, adherence to coding standards, and it could be rewritten to execute much more efficiently. It also requires Windows Vista or newer and only reports IPV4 TCP connections; not IPV6 or UDP connections. Bugs are included at no extra charge...
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SDK data structures.
;;; GetTCPTable.wbt
;typedef enum {
; TcpConnectionOffloadStateInHost,
; TcpConnectionOffloadStateOffloading,
; TcpConnectionOffloadStateOffloaded,
; TcpConnectionOffloadStateUploading,
; TcpConnectionOffloadStateMax
;} TCP_CONNECTION_OFFLOAD_STATE, *PTCP_CONNECTION_OFFLOAD_STATE;
;typedef struct _MIB_TCPROW2 {
; DWORD dwState;
; DWORD dwLocalAddr;
; DWORD dwLocalPort;
; DWORD dwRemoteAddr;
; DWORD dwRemotePort;
; DWORD dwOwningPid;
; TCP_CONNECTION_OFFLOAD_STATE dwOffloadState;
;} MIB_TCPROW2, *PMIB_TCPROW2;
;typedef struct _MIB_TCPTABLE2 {
; DWORD dwNumEntries;
; MIB_TCPROW2 table[ANY_SIZE];
;} MIB_TCPTABLE2, *PMIB_TCPTABLE2;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Need stuff.
bWin64 = WinMetrics(-3) == 5
;; Converts a network byte order ip address to a machine byte order string.
#DefineFunction Ipv4ToString(_ipv4)
hAddr = BinaryAlloc(4)
hStr = BinaryAlloc(17)
BinaryPoke4(hAddr, 0, _ipv4)
DllCall('Ntdll.dll', lpstr:'RtlIpv4AddressToStringA', lpbinary:hAddr, lpbinary:hStr)
BinaryEodSet(hStr, 17)
strRet = BinaryPeekStr(hStr, 0, 17)
BinaryFree(hAddr)
BinaryFree(hStr)
return strRet
#EndFunction
;; Converts a TCP network byte order port number to a machine byte order port number.
#DefineFunction PortOrderConvert( _port )
return DllCall('Ws2_32.dll', word:'ntohs', word:_port)
#EndFunction
; SDK constants.
NO_ERROR = 0
ERROR_INSUFFICIENT_BUFFER = 122
nRowSize = 28
; Create the struct with a table array added.
hTcpTbl = BinaryAlloc(4+nRowSize)
if bWin64 then nBufSize = 8
else nBufSize = 4
hSize = BinaryAlloc(nBufSize)
pdwSize = IntControl(42, hSize, 0, 0, 0)
nRet = DllCall('Iphlpapi.dll', long:'GetTcpTable2', lpbinary:hTcpTbl, long_ptr:pdwSize, long:1)
if bWin64 then nTableSize = BinaryPeek8(hSize, 0)
else nTableSize = BinaryPeek4(hSize, 0)
terminate(nTableSize <= 0, 'GetTCPTable2 Table Size', 'No joy in Mudville.')
; Need room for more than one connection.
if nRet == ERROR_INSUFFICIENT_BUFFER
BinaryFree(hTcpTbl)
; Create the a table and fill it with data.
hTcpTbl = BinaryAlloc(4+nTableSize)
if bWin64 then nTableSize = BinaryPoke8(hSize, 0, nTableSize)
else nTableSize = BinaryPoke4(hSize, 0, nTableSize)
; Fetch the table.
nRet = DllCall('Iphlpapi.dll', long:'GetTcpTable2', lpbinary:hTcpTbl, long_ptr:pdwSize, long:1)
terminate(nRet != NO_ERROR, 'GetTCPTable2 Table Fetch', 'Well that did work very well. Did it?')
BinaryEodSet(hTcpTbl, 4+nTableSize)
BinaryFree(hSize)
else
BinaryFree(hSize)
endif
; Dimension an array to store the table.
nRows = BinaryPeek4(hTcpTbl, 0)
aTable = ArrDimension(nRows+1, 7) ; +1 for table header.
aTable[0,0] = 'State'
aTable[0,1] = 'Local Address'
aTable[0,2] = 'Local Port'
aTable[0,3] = 'Remote Address'
aTable[0,4] = 'Remote Port'
aTable[0,5] = 'Process ID'
aTable[0,6] = 'Load State'
nOffset = 4 ; Skip entry count.
;; Copy buffer to the array with conversions as needed.
for i = 1 to nRows
for j = 0 to 6
aTable[i,j] = BinaryPeek4(hTcpTbl, nOffset)
nOffset += 4
if j==1 || j==3 then aTable[i,j] = Ipv4ToString(aTable[i,j])
else if j==2 || j==4 then aTable[i,j] = PortOrderConvert(aTable[i,j])
next
next
BinaryFree(hTcpTbl)
TcpTabFormat=`WWWDLGED,6.2`
TcpTabCaption=`TCP Ipv4 Connections`
TcpTabX=1857
TcpTabY=208
TcpTabWidth=518
TcpTabHeight=324
TcpTabNumControls=003
TcpTabProcedure=`DEFAULT`
TcpTabFont=`DEFAULT`
TcpTabTextColor=`DEFAULT`
TcpTabBackground=`DEFAULT,DEFAULT`
TcpTabConfig=0
TcpTabDPI=`192,10,20`
TcpTab001=`138,300,050,016,PUSHBUTTON,"PushButton_OK",DEFAULT,"OK",1,10,@csDefButton,DEFAULT,DEFAULT,DEFAULT`
TcpTab002=`329,300,049,016,PUSHBUTTON,"PushButton_Cancel",DEFAULT,"Cancel",0,20,DEFAULT,DEFAULT,DEFAULT,DEFAULT`
TcpTab003=`012,014,488,268,REPORTVIEW,"ReportView_1",aTable,DEFAULT,DEFAULT,30,@csFirstHeader,DEFAULT,DEFAULT,DEFAULT`
ButtonPushed=Dialog("TcpTab")
exit