那么可以执行CMD这个进程得到监听的端口号信息,C#代码如下:
1.//进程id
2.int pid = ProcInfo.ProcessID;
3.
4.//存放进程使用的端口号链表
5. List<int> ports = new List<int>();
6.
7. Process pro = new Process();
8. pro.StartInfo.FileName = "";
9. pro.StartInfo.UseShellExecute = false;
10. pro.StartInfo.RedirectStandardInput = true;
11. pro.StartInfo.RedirectStandardOutput = true;梦见丈夫有外遇
12. pro.StartInfo.RedirectStandardError = true;
13. pro.StartInfo.CreateNoWindow = true;
14. pro.Start();
15. pro.StandardInput.WriteLine("netstat -ano");
16. pro.StandardInput.WriteLine("exit");
17. Regex reg = new Regex("\\s+", RegexOptions.Compiled);
18.string line = null;
19. ports.Clear();
20.while ((line = pro.StandardOutput.ReadLine()) != null)
21. {
22. line = line.Trim();
23.if (line.StartsWith("TCP", StringComparison.OrdinalIgn
oreCase))
24. {
25. line = reg.Replace(line, ",");
26.string[] arr = line.Split(',');
27.if (arr[4] == pid.ToString())
28. {
29.string soc = arr[1];
30.int pos = soc.LastIndexOf(':');
31.int pot = int.Parse(soc.Substring(pos + 1));
32. ports.Add(pot);
33. }
34. }
35.else if (line.StartsWith("UDP", StringComparison.Ordin
alIgnoreCase))
36. {
37. line = reg.Replace(line, ",");
38.string[] arr = line.Split(',');
39.if (arr[3] == pid.ToString())
40. {
41.string soc = arr[1];
42.int pos = soc.LastIndexOf(':');
43.int pot = int.Parse(soc.Substring(pos + 1));
44. ports.Add(pot);
45. }
46. }
47. }
48. pro.Close();
2、如果不执行CMD进程,如何获得?可以参考这篇文章
wwwblogs/BoyXiao/archive/2012/02/20/2359273.html
文章介绍了使用Windows API获得进程和端口的映射关系:
(1)根据进程ID 获得该进程所打开的所有的TCP 和UDP 端口。
(2)根据端口号来获得打开该端口的进程。
C语言代码如下:
1.// ProcessorPort.cpp : 定义 DLL 应用程序的导出函数。
2.//
3.
4.#include "stdafx.h"
5.
6.#include <Windows.h>
7.#include <Psapi.h>
8.#include <Iprtrmib.h>
9.#include <Winsock2.h>
10.
11.#pragma comment(lib,"Psapi.lib")
12.#pragma comment(lib,"Iphlpapi.Lib")
13.#pragma comment(lib,"WS2_32.lib")
14.
16.{
17. TcpType,
18. UdpType
19.};
20.
22.{
23. DWORD dwState; //连接状态
24. DWORD dwLocalAddr; //本地地址
25. DWORD dwLocalPort; //本地端口
26. DWORD dwRemoteAddr; //远程地址
27. DWORD dwRemotePort; //远程端口
28. DWORD dwProcessId; //进程标识
29.}MIB_TCPEXROW,*PMIB_TCPEXROW;
30.
31.
33.{
34. DWORD dwLocalAddr; //本地地址
35. DWORD dwLocalPort; //本地端口
36. DWORD dwProcessId; //进程标识
37.
38.}MIB_UDPEXROW,*PMIB_UDPEXROW;
39.
40.
42.{
43. DWORD dwState; //连接状态
44. DWORD dwLocalAddr; //本地地址
45. DWORD dwLocalPort; //本地端口
46. DWORD dwRemoteAddr; //远程地址
47. DWORD dwRemotePort; //远程端口
48. DWORD dwProcessId; //进程标识
49. DWORD Unknown; //待定标识
50.}MIB_TCPEXROW_VISTA,*PMIB_TCPEXROW_VISTA;
51.
52.
54.{
55. DWORD dwNumEntries;
56. MIB_TCPEXROW table[ANY_SIZE];
57.}MIB_TCPEXTABLE,*PMIB_TCPEXTABLE;
58.
59.
61.{
62. DWORD dwNumEntries;
63. MIB_TCPEXROW_VISTA table[ANY_SIZE];
64.}MIB_TCPEXTABLE_VISTA,*PMIB_TCPEXTABLE_VISTA;
65.
66.
68.{
69. DWORD dwNumEntries;
70. MIB_UDPEXROW table[ANY_SIZE];
71.}MIB_UDPEXTABLE,*PMIB_UDPEXTABLE;
72.
73.
74.//====================================================================
=================//
75.//Name: DWORD AllocateAndGetTcpExTableFromStack()
// //
76.//Descripion: 该函数仅仅只在 Windows XP,Windows Server 2003 下有
效 //
77.//====================================================================
=================//
79. PMIB_TCPEXTABLE *pTcpTabel,
80.bool bOrder,
81. HANDLE heap,
82. DWORD zero,
83. DWORD flags
84. );
85.
86.//====================================================================
=================//
87.//Name: DWORD AllocateAndGetUdpExTableFromStack()
//
88.//Descripion: 该函数仅仅只在 XP,Windows Server 2003 下有
效 //
89.//====================================================================
=================//
91. PMIB_UDPEXTABLE *pUdpTable,
92.bool bOrder,
93. HANDLE heap,
94. DWORD zero,
95. DWORD flags
96. );
97.
98.//====================================================================
=================//
99.//Name: DWORD InternalGetTcpTable2()
//
100.//Descripion: 该函数在 Windows Vista 以及 Windows 7 下面
效 //
101.//================================================================== ===================//
103. PMIB_TCPEXTABLE_VISTA *pTcpTable_Vista,
104. HANDLE heap,
105. DWORD flags
106. );
107.
108.//================================================================== ===================//
109.//Name: DWORD InternalGetUdpTableWithOwnerPid() //
110.//Descripion: 该函数在 Windows Vista 以及 Windows 7 下面
效 //
111.//================================================================== ===================//
113. PMIB_UDPEXTABLE *pUdpTable,
114. HANDLE heap,
115. DWORD flags
116. );
117.
118.
119.//================================================================== ===================//
120.//Name: DWORD GetProcessIdByPort() //
121.//Descripion: 根据端口号得到打开该端口号的进程ID(支持 XP,Server 2003,Vista,Win7) //
122.//================================================================== ===================//
<"C" __declspec(dllexport) DWORD __stdcall GetProcessIdByPort( TcpOrUdp type, DWORD dwPort)
124.{
125. HMODULE hModule = LoadLibraryW(L"iphlpapi.dll");
126.if (hModule == NULL)
127. {
129. }
130.
131.if(type == TcpType)
132. {
133.// 表明查询的是 TCP 信息
134. PFNAllocateAndGetTcpExTableFromStack pAllocateAndGetTcpExTab leFromStack;
135. pAllocateAndGetTcpExTableFromStack =
136. (PFNAllocateAndGetTcpExTableFromStack)GetProcAddress(hMo dule, "AllocateAndGetTcpExTableFromStack");
137.if (pAllocateAndGetTcpExTableFromStack != NULL)
138. {
139.// 表明为 XP 或者 Server 2003 操作系统
140. PMIB_TCPEXTABLE pTcpExTable = NULL;
141.if (pAllocateAndGetTcpExTableFromStack(&pTcpExTable, TRU E, GetProcessHeap(), 0, AF_INET) != 0)
142. {
143.if (pTcpExTable)
144. {
145. HeapFree(GetProcessHeap(), 0, pTcpExTable); 146. }
147.
148. FreeLibrary(hModule);
149. hModule = NULL;
150.
152. }
153.
154.for (UINT i = 0; i < pTcpExTable->dwNumEntries; i++) 155. {
156.// 过滤掉数据,只查询我们需要的进程数据
157.if(dwPort == ntohs(0x0000FFFF & pTcpExTable->table[i ].dwLocalPort))
158. {
159. DWORD dwProcessId = pTcpExTable->table[i].dwProc essId;
160.if (pTcpExTable)
161. {
162. HeapFree(GetProcessHeap(), 0, pTcpExTable);
163. }
164.
165. FreeLibrary(hModule);
166. hModule = NULL;
167.
169. }
170. }
发布评论