Skip to content

Instantly share code, notes, and snippets.

@pedroinfo
Created February 4, 2025 21:08
Show Gist options
  • Save pedroinfo/b72b905694d50d951b031fdac06db3dd to your computer and use it in GitHub Desktop.
Save pedroinfo/b72b905694d50d951b031fdac06db3dd to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NetworkMonitor.WinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeDataGridView();
LoadConnections();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void InitializeDataGridView()
{
dgvConnections.ColumnCount = 6;
dgvConnections.Columns[0].Name = "PID";
dgvConnections.Columns[1].Name = "Programa";
dgvConnections.Columns[2].Name = "Local Address";
dgvConnections.Columns[3].Name = "Local Port";
dgvConnections.Columns[4].Name = "Remote Address";
dgvConnections.Columns[5].Name = "Remote Port";
}
private void LoadConnections()
{
dgvConnections.Rows.Clear();
var connections = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections();
foreach (var conn in connections)
{
string localAddress = conn.LocalEndPoint.Address.ToString();
int localPort = conn.LocalEndPoint.Port;
string remoteAddress = conn.RemoteEndPoint.Address.ToString();
int remotePort = conn.RemoteEndPoint.Port;
int pid = GetProcessId(conn);
string processName = GetProcessName(pid);
dgvConnections.Rows.Add(pid, processName, localAddress, localPort, remoteAddress, remotePort);
}
}
private int GetProcessId(TcpConnectionInformation conn)
{
try
{
var tcpTable = new MIB_TCPROW_OWNER_PID[1];
int bufferSize = 0;
GetExtendedTcpTable(IntPtr.Zero, ref bufferSize, false, 2, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0);
IntPtr tcpTablePtr = Marshal.AllocHGlobal(bufferSize);
try
{
if (GetExtendedTcpTable(tcpTablePtr, ref bufferSize, false, 2, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0) == 0)
{
int rowSize = Marshal.SizeOf(typeof(MIB_TCPROW_OWNER_PID));
int count = Marshal.ReadInt32(tcpTablePtr);
IntPtr rowPtr = new IntPtr(tcpTablePtr.ToInt64() + 4);
for (int i = 0; i < count; i++)
{
MIB_TCPROW_OWNER_PID row = Marshal.PtrToStructure<MIB_TCPROW_OWNER_PID>(rowPtr);
if (row.localAddr == BitConverter.ToUInt32(conn.LocalEndPoint.Address.GetAddressBytes(), 0) &&
row.localPort == (ushort)IPAddress.HostToNetworkOrder((short)conn.LocalEndPoint.Port) &&
row.remoteAddr == BitConverter.ToUInt32(conn.RemoteEndPoint.Address.GetAddressBytes(), 0) &&
row.remotePort == (ushort)IPAddress.HostToNetworkOrder((short)conn.RemoteEndPoint.Port))
{
return Convert.ToInt32(row.owningPid);
}
rowPtr = new IntPtr(rowPtr.ToInt64() + rowSize);
}
}
}
finally
{
Marshal.FreeHGlobal(tcpTablePtr);
}
}
catch
{
return -1;
}
return -1;
}
private string GetProcessName(int pid)
{
try
{
return Process.GetProcessById(pid).ProcessName;
}
catch
{
return "Desconhecido";
}
}
internal enum TCP_TABLE_CLASS
{
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
}
[StructLayout(LayoutKind.Sequential)]
internal struct MIB_TCPROW_OWNER_PID
{
public uint state;
public uint localAddr;
public ushort localPort;
public uint remoteAddr;
public ushort remotePort;
public uint owningPid;
}
[DllImport("iphlpapi.dll", SetLastError = true)]
private static extern int GetExtendedTcpTable(IntPtr pTcpTable, ref int dwSize, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass, uint reserved = 0);
private void RefreshButton_Click(object sender, EventArgs e)
{
LoadConnections();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment