NetProcessTool.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. namespace Y.Utils.WindowsUtils.ProcessUtils
  9. {
  10. public class NetProcessTool
  11. {
  12. #region 枚举 读取连接表选项
  13. enum TCP_TABLE_CLASS
  14. {
  15. TCP_TABLE_BASIC_LISTENER,
  16. TCP_TABLE_BASIC_CONNECTIONS,
  17. TCP_TABLE_BASIC_ALL,
  18. TCP_TABLE_OWNER_PID_LISTENER,
  19. TCP_TABLE_OWNER_PID_CONNECTIONS,
  20. TCP_TABLE_OWNER_PID_ALL,
  21. TCP_TABLE_OWNER_MODULE_LISTENER,
  22. TCP_TABLE_OWNER_MODULE_CONNECTIONS,
  23. TCP_TABLE_OWNER_MODULE_ALL
  24. }
  25. enum UDP_TABLE_CLASS
  26. {
  27. UDP_TABLE_BASIC,
  28. UDP_TABLE_OWNER_PID,
  29. UDP_TABLE_OWNER_MODULE
  30. }
  31. #endregion
  32. #region UDP 和 TCP 列表 结构
  33. struct UdpTable
  34. {
  35. public uint dwNumEntries;
  36. private UdpRow table;
  37. }
  38. struct TcpTable
  39. {
  40. public uint dwNumEntries;
  41. private TcpRow table;
  42. }
  43. #endregion
  44. #region UDP 和 TCP 行记录 结构
  45. public struct UdpRow
  46. {
  47. private uint localAddr;
  48. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  49. private byte[] localPort;
  50. public IPAddress LocalIP
  51. {
  52. get
  53. {
  54. return new IPAddress((long)((ulong)this.localAddr));
  55. }
  56. }
  57. public ushort LocalPort
  58. {
  59. get
  60. {
  61. return BitConverter.ToUInt16(new byte[]
  62. {
  63. this.localPort[1],
  64. this.localPort[0]
  65. }, 0);
  66. }
  67. }
  68. public int ProcessId { get; }
  69. }
  70. public struct TcpRow
  71. {
  72. ConnectionState state;
  73. private uint localAddr;
  74. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  75. private byte[] localPort;
  76. private uint remoteAddr;
  77. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  78. private byte[] remotePort;
  79. public IPAddress LocalIP
  80. {
  81. get
  82. {
  83. return new IPAddress((long)((ulong)this.localAddr));
  84. }
  85. }
  86. public ushort LocalPort
  87. {
  88. get
  89. {
  90. return BitConverter.ToUInt16(new byte[]
  91. {
  92. this.localPort[1],
  93. this.localPort[0]
  94. }, 0);
  95. }
  96. }
  97. public IPAddress RemoteIP
  98. {
  99. get
  100. {
  101. return new IPAddress((long)((ulong)this.remoteAddr));
  102. }
  103. }
  104. public ushort RemotePort
  105. {
  106. get
  107. {
  108. return BitConverter.ToUInt16(new byte[]
  109. {
  110. this.remotePort[1],
  111. this.remotePort[0]
  112. }, 0);
  113. }
  114. }
  115. public int ProcessId { get; }
  116. }
  117. #endregion
  118. [DllImport("iphlpapi.dll", SetLastError = true)]
  119. static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass, uint reserved = 0u);
  120. [DllImport("iphlpapi.dll", SetLastError = true)]
  121. static extern uint GetExtendedUdpTable(IntPtr pUdpTable, ref int dwOutBufLen, bool sort, int ipVersion, UDP_TABLE_CLASS tblClass, uint reserved = 0u);
  122. public static TcpRow[] GetTcpConn()
  123. {
  124. TcpRow[] array = null;
  125. int ipVersion = 2;
  126. int cb = 0;
  127. uint extendedTcpTable = GetExtendedTcpTable(IntPtr.Zero, ref cb, true, ipVersion, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0u);
  128. IntPtr intPtr = Marshal.AllocHGlobal(cb);
  129. try
  130. {
  131. extendedTcpTable = GetExtendedTcpTable(intPtr, ref cb, true, ipVersion, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0u);
  132. bool flag = extendedTcpTable > 0u;
  133. if (flag) return null;
  134. TcpTable tcpTable = (TcpTable)Marshal.PtrToStructure(intPtr, typeof(TcpTable));
  135. IntPtr intPtr2 = (IntPtr)((long)intPtr + Marshal.SizeOf(tcpTable.dwNumEntries));
  136. array = new TcpRow[tcpTable.dwNumEntries];
  137. int num = 0;
  138. while (num < (long)((ulong)tcpTable.dwNumEntries))
  139. {
  140. TcpRow tcpRow = (TcpRow)Marshal.PtrToStructure(intPtr2, typeof(TcpRow));
  141. array[num] = tcpRow;
  142. intPtr2 = (IntPtr)((long)intPtr2 + Marshal.SizeOf(tcpRow));
  143. num++;
  144. }
  145. }
  146. catch (Exception)
  147. { }
  148. finally
  149. {
  150. Marshal.FreeHGlobal(intPtr);
  151. }
  152. return array;
  153. }
  154. public static UdpRow[] GetUdpConn()
  155. {
  156. UdpRow[] array = null;
  157. int ipVersion = 2;
  158. int cb = 0;
  159. uint extendedUdpTable = GetExtendedUdpTable(IntPtr.Zero, ref cb, true, ipVersion, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0u);
  160. IntPtr intPtr = Marshal.AllocHGlobal(cb);
  161. try
  162. {
  163. extendedUdpTable = GetExtendedUdpTable(intPtr, ref cb, true, ipVersion, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0u);
  164. bool flag = extendedUdpTable > 0u;
  165. if (flag) return null;
  166. UdpTable udpTable = (UdpTable)Marshal.PtrToStructure(intPtr, typeof(UdpTable));
  167. IntPtr intPtr2 = (IntPtr)((long)intPtr + Marshal.SizeOf(udpTable.dwNumEntries));
  168. array = new UdpRow[udpTable.dwNumEntries];
  169. int num = 0;
  170. while (num < (long)((ulong)udpTable.dwNumEntries))
  171. {
  172. UdpRow udpRow = (UdpRow)Marshal.PtrToStructure(intPtr2, typeof(UdpRow));
  173. array[num] = udpRow;
  174. intPtr2 = (IntPtr)((long)intPtr2 + Marshal.SizeOf(udpRow));
  175. num++;
  176. }
  177. }
  178. catch (Exception)
  179. { }
  180. finally
  181. {
  182. Marshal.FreeHGlobal(intPtr);
  183. }
  184. return array;
  185. }
  186. }
  187. }