Reputation: 1095
Given a socket port how do you find the process ID (process name) of the process on Windows 10 that uses this port? I am aware of netstat
command but I would like to do it with C code only.
Upvotes: 1
Views: 1438
Reputation: 4247
How about there, it appears there's a way: the IP Helper library.
Ref: https://learn.microsoft.com/en-us/windows/win32/iphlp/ip-helper-start-page
I haven't used it for this, but it's clearly going down the right road by providing everything you need to basically roll your own netstat.exe
.
The low-level object that contains all the info is MIB_TCPROW2
, which has the local and remote address + port, plus dwOwningPid
. So we know there's a way.
Drilling down we ultimately need the GetTcpTable2()
call, and Microsoft's web page helpfully has what appears to be fully-functional C code to do all this yourself.
https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-gettcptable2
Finding this was the best surprise of my day!
Upvotes: 1