tags:

views:

135

answers:

2

I am trying to use GetExtendedTcpTable via a Python program. Basically I am trying to convert "ActiveState Code Recipe 392572: Using the Win32 IPHelper API" to "Getting the active TCP/UDP connections using the GetExtendedTcpTable function".

My problem is that I cannot seem to get the Python script to recognize TCP_TABLE_CLASS.TCP_TABL\E_OWNER_PID_ALL.

I have tried

ctypes.windll.iphlpapi.GetExtendedTcpTable(NULL, ctypes.byref(dwSize), bOrder, AF_INET, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0)

but this always bails with "AttributeError: function 'TCP_TABLE_CLASS' not found"

I have also tried

ctypes.windll.iphlpapi.GetExtendedTcpTable(NULL, ctypes.byref(dwSize), bOrder, AF_INET, ctypes.windll.iphlpapi.TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0)

which receives the same results.

Any recommendations are appreciated.

Cutaway

+1  A: 

The TCP_TABLE_CLASS is an enum

typedef enum  {
  TCP_TABLE_BASIC_LISTENER,
  TCP_TABLE_BASIC_CONNECTIONS,
  TCP_TABLE_BASIC_ALL,
  TCP_TABLE_OWNER_PID_LISTENER,
  TCP_TABLE_OWNER_PID_CONNECTIONS,
  TCP_TABLE_OWNER_PID_ALL,
  TCP_TABLE_OWNER_MODULE_LISTENER,
  TCP_TABLE_OWNER_MODULE_CONNECTIONS,
  TCP_TABLE_OWNER_MODULE_ALL 
} TCP_TABLE_CLASS, *PTCP_TABLE_CLASS;

you must define it in your python script with some constants. This is not exported by the dll.

  TCP_TABLE_BASIC_LISTENER = 0
  TCP_TABLE_BASIC_CONNECTIONS = 1
  TCP_TABLE_BASIC_ALL = 2
  TCP_TABLE_OWNER_PID_LISTENER = 3
  TCP_TABLE_OWNER_PID_CONNECTIONS = 4
  TCP_TABLE_OWNER_PID_ALL = 5
  TCP_TABLE_OWNER_MODULE_LISTENER = 6
  TCP_TABLE_OWNER_MODULE_CONNECTIONS = 7
  TCP_TABLE_OWNER_MODULE_ALL = 8

luc
A: 

In this case, since:

typedef enum { TCP_TABLE_BASIC_LISTENER, TCP_TABLE_BASIC_CONNECTIONS, TCP_TABLE_BASIC_ALL, TCP_TABLE_OWNER_PID_LISTENER, TCP_TABLE_OWNER_PID_CONNECTIONS, TCP_TABLE_OWNER_PID_ALL, TCP_TABLE_OWNER_MODULE_LISTENER, TCP_TABLE_OWNER_MODULE_CONNECTIONS, TCP_TABLE_OWNER_MODULE_ALL } TCP_TABLE_CLASS, *PTCP_TABLE_CLASS;

I used '5' and it worked.

Thank you, Cutaway

Cutaway