資源簡介
C++實現獲取win7系統運行中的進程端口號,IP,狀態和PID參數,測試可以獲取相關參數
代碼片段和文件信息
//?portAndProcess.cpp?:?Defines?the?entry?point?for?the?console?application.
//http://blog.sina.com.cn/s/blog_9fc415370101cb98.html?
//參考文獻如上,但是需要根據自己的編譯器進行適當變量修改;
//該測試在window7?64?旗艦P1可以運行?平臺VS2015
#pragma?once
#include?“stdafx.h“
//#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
#pragma?comment(lib?“WS2_32.lib“)
const?DWORD?ANY_TYSIZE =256;
const?DWORD?TYMIB_TCP_STATE_CLOSED?????=?1;
const?DWORD?TYMIB_TCP_STATE_LISTEN?????=?2;
const?DWORD?TYMIB_TCP_STATE_SYN_SENT???=?3;
const?DWORD?TYMIB_TCP_STATE_SYN_RCVD???=?4;
const?DWORD?TYMIB_TCP_STATE_ESTAB??????=?5;
const?DWORD?TYMIB_TCP_STATE_FIN_WAIT1?=?6;
const?DWORD?TYMIB_TCP_STATE_FIN_WAIT2?=?7;
const?DWORD?TYMIB_TCP_STATE_CLOSE_WAIT?=?8;
const?DWORD?TYMIB_TCP_STATE_CLOSING????=?9;
const?DWORD?TYMIB_TCP_STATE_LAST_ACK???=?10;
const?DWORD?TYMIB_TCP_STATE_TIME_WAIT?=?11;
const?DWORD?TYMIB_TCP_STATE_DELETE_TCB?=?12;
//////////////////////////////////////////////////////////////////////////2016
typedef?DWORD(WINAPI?*PGetExtendedTcpTable)(
PVOID??pTcpTable2
PDWORD???pdwSize
BOOL???bOrder
ULONG???ulAf
TCP_TABLE_CLASS???TableClass
ULONG???Reserved
);
typedef?struct?_MIB_TCPROW_OWNER_PIDTY
{
DWORD???????dwState;//連接狀態
DWORD???????dwLocalAddr;//本地?IP地址
DWORD???????dwLocalPort;//本地端口
DWORD???????dwRemoteAddr;//遠程?IP?地址
DWORD???????dwRemotePort;//遠程端口
DWORD???????dwOwningPid;//關聯的進程ID
}?MIB_TCPROW_OWNER_PIDTY?*PMIB_TCPROW_OWNER_PIDTY;
typedef?struct?_MIB_TCPTABLE_OWNER_PIDTY
{
DWORD????????????????dwNumEntries;
MIB_TCPROW_OWNER_PIDTY?tablety[ANY_TYSIZE];
}?MIBTCPTABLEOWNERPIDTY?*PMIBTCPTABLEOWNERPID;
//////////////////////////////////////////////////////////////////////////2016
int?GetPortFromProcessName(PCHAR?ProcessName?vector?&localAddressVector)
{
?
?//?獲取擴展函數的入口地址?
?HMODULE?hModule?=?::LoadLibrary(“iphlpapi.dll“);
?
?PGetExtendedTcpTable?pFuncGetTCPTable;
?pFuncGetTCPTable?=?(PGetExtendedTcpTable)::GetProcAddress(hModule?“GetExtendedTcpTable“);
?if?(pF
評論
共有 條評論