资源简介
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
评论
共有 条评论