Finding TCP Ports Used by the Application

Okay, so I am expanding my company's flexlm provider daemon so that it is a little more explicit for client applications.

I need to be able to figure out which lmgrd port is listening before clients connect. The API documentation seems rather fruitless, and I believe that they store most of their code in compiled form, so I can’t just look at their source code.

Can I take advantage of the amazing power of the Windows API to find out which ports a particular process uses? If the Process Explorer from Sysinternals can do this, I should be able to, right? What will be the sample code for this?

It must support Windows XP and above, as many of our clients have not yet been updated.

I should note that it turns out that FLEX has support for extracting a port from a license file. There is no code in front of me, but I know that this is not the best way to find out which ports your / lmgrd daemon works for.

+9
windows api process port tcp
source share
3 answers

GetTcpTable2 - see below

GetTcpTable2 Function

The GetTcpTable function retrieves the IPv4 TCP connection table.

This will populate the MIB_TCPTABLE structure.

typedef struct _MIB_TCPTABLE { DWORD dwNumEntries; MIB_TCPROW table[ANY_SIZE]; } MIB_TCPTABLE, *PMIB_TCPTABLE; 

And now MIB_TCPROW

 typedef struct _MIB_TCPROW { DWORD dwState; DWORD dwLocalAddr; DWORD dwLocalPort; DWORD dwRemoteAddr; DWORD dwRemotePort; } MIB_TCPROW, *PMIB_TCPROW; 

IMPORTANT :

You need to use GetTcpTable2 to also bind the corresponding PID.

 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; 

dwOwningPid

+8
source share

Here is the code I ended up for those who solve this problem after me

 #include "stdafx.h" #include <windows.h> #include <iphlpapi.h> // These are just for the ntohl function in the printf below #include <winsock.h> #pragma comment(lib, "Ws2_32.lib") DWORD (WINAPI *pGetExtendedTcpTable)( PVOID pTcpTable, PDWORD pdwSize, BOOL bOrder, ULONG ulAf, TCP_TABLE_CLASS TableClass, ULONG Reserved ); int _tmain(int argc, _TCHAR* argv[]) { MIB_TCPTABLE_OWNER_PID *pTCPInfo; MIB_TCPROW_OWNER_PID *owner; DWORD size; DWORD dwResult; HMODULE hLib = LoadLibrary("iphlpapi.dll"); pGetExtendedTcpTable = (DWORD (WINAPI *)(PVOID, PDWORD, BOOL, ULONG, TCP_TABLE_CLASS, ULONG)) GetProcAddress(hLib, "GetExtendedTcpTable"); if (!pGetExtendedTcpTable) { printf("Could not load iphlpapi.dll. This application is for Windows XP SP2 and up.\n"); return 1; } dwResult = pGetExtendedTcpTable(NULL, &size, false, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0); pTCPInfo = (MIB_TCPTABLE_OWNER_PID*)malloc(size); dwResult = pGetExtendedTcpTable(pTCPInfo, &size, false, AF_INET, TCP_TABLE_OWNER_PID_LISTENER, 0); if (dwResult != NO_ERROR) { printf("Couldn't get our IP table"); return 2; } printf("Iterating though table:\n"); for (DWORD dwLoop = 0; dwLoop < pTCPInfo->dwNumEntries; dwLoop++) { owner = &pTCPInfo->table[dwLoop]; printf(" PID: %5u - Port: %5u\n", owner->dwOwningPid, ntohs(owner->dwLocalPort)); } // Pause a moment printf("Done Processing\n"); return 0; } 
+4
source share

In the worst case, you can always parse the output:

 netstat -bna 
+3
source share

All Articles