PID<->Port[转]
//Netstat -anb #include <Windows.h>
#include "Psapi.h"
#include <Iprtrmib.h> #pragma comment(lib,"Psapi.lib")
#pragma comment(lib,"Iphlpapi.Lib")
#pragma comment(lib,"WS2_32.lib") #include <iostream>
#include <vector> using namespace std;
using std::vector; enum TcpOrUdp
{
TcpType,
UdpType
}; typedef struct
{
DWORD dwState; //连接状态
DWORD dwLocalAddr; //本地地址
DWORD dwLocalPort; //本地端口
DWORD dwRemoteAddr; //远程地址
DWORD dwRemotePort; //远程端口
DWORD dwProcessId; //进程标识 }MIB_TCPEXROW,*PMIB_TCPEXROW; typedef struct
{
DWORD dwLocalAddr; //本地地址
DWORD dwLocalPort; //本地端口
DWORD dwProcessId; //进程标识 }MIB_UDPEXROW,*PMIB_UDPEXROW; typedef struct
{
DWORD dwState; //连接状态
DWORD dwLocalAddr; //本地地址
DWORD dwLocalPort; //本地端口
DWORD dwRemoteAddr; //远程地址
DWORD dwRemotePort; //远程端口
DWORD dwProcessId; //进程标识
DWORD Unknown; //待定标识 }MIB_TCPEXROW_VISTA,*PMIB_TCPEXROW_VISTA; typedef struct
{
DWORD dwNumEntries;
MIB_TCPEXROW table[ANY_SIZE]; }MIB_TCPEXTABLE,*PMIB_TCPEXTABLE; typedef struct
{
DWORD dwNumEntries;
MIB_TCPEXROW_VISTA table[ANY_SIZE]; }MIB_TCPEXTABLE_VISTA,*PMIB_TCPEXTABLE_VISTA; typedef struct
{
DWORD dwNumEntries;
MIB_UDPEXROW table[ANY_SIZE]; }MIB_UDPEXTABLE,*PMIB_UDPEXTABLE; //=====================================================================================//
//Name: DWORD AllocateAndGetTcpExTableFromStack() //
// //
//Descripion: 该函数仅仅只在 Windows XP,Windows Server 2003 下有效 //
// //
//=====================================================================================//
typedef DWORD (WINAPI *PFNAllocateAndGetTcpExTableFromStack)(
PMIB_TCPEXTABLE *pTcpTabel,
bool bOrder,
HANDLE heap,
DWORD zero,
DWORD flags
); //=====================================================================================//
//Name: DWORD AllocateAndGetUdpExTableFromStack() //
// //
//Descripion: 该函数仅仅只在 XP,Windows Server 2003 下有效 //
// //
//=====================================================================================//
typedef DWORD (WINAPI *PFNAllocateAndGetUdpExTableFromStack)(
PMIB_UDPEXTABLE *pUdpTable,
bool bOrder,
HANDLE heap,
DWORD zero,
DWORD flags
); //=====================================================================================//
//Name: DWORD InternalGetTcpTable2() //
// //
//Descripion: 该函数在 Windows Vista 以及 Windows 7 下面效 //
// //
//=====================================================================================//
typedef DWORD (WINAPI *PFNInternalGetTcpTable2)(
PMIB_TCPEXTABLE_VISTA *pTcpTable_Vista,
HANDLE heap,
DWORD flags
); //=====================================================================================//
//Name: DWORD InternalGetUdpTableWithOwnerPid() //
// //
//Descripion: 该函数在 Windows Vista 以及 Windows 7 下面效 //
// //
//=====================================================================================//
typedef DWORD (WINAPI *PFNInternalGetUdpTableWithOwnerPid)(
PMIB_UDPEXTABLE *pUdpTable,
HANDLE heap,
DWORD flags
); //=====================================================================================//
//Name: DWORD GetProcessIdByPort() //
// //
//Descripion: 根据端口号求出打开该端口号的进程 ID(支持 XP,Server 2003,Vista,Win7) //
// //
//=====================================================================================//
DWORD GetProcessIdByPort(TcpOrUdp type, DWORD dwPort)
{
HMODULE hModule = LoadLibraryW(L"iphlpapi.dll");
if (hModule == NULL)
{
return ;
} if(type == TcpType)
{
// 表明查询的是 TCP 信息
PFNAllocateAndGetTcpExTableFromStack pAllocateAndGetTcpExTableFromStack;
pAllocateAndGetTcpExTableFromStack =
(PFNAllocateAndGetTcpExTableFromStack)GetProcAddress(hModule, "AllocateAndGetTcpExTableFromStack");
if (pAllocateAndGetTcpExTableFromStack != NULL)
{
// 表明为 XP 或者 Server 2003 操作系统
PMIB_TCPEXTABLE pTcpExTable = NULL;
if (pAllocateAndGetTcpExTableFromStack(&pTcpExTable, TRUE, GetProcessHeap(), , AF_INET) != )
{
if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
} for (UINT i = ; i < pTcpExTable->dwNumEntries; i++)
{
// 过滤掉数据,只查询我们需要的进程数据
if(dwPort == ntohs(0x0000FFFF & pTcpExTable->table[i].dwLocalPort))
{
DWORD dwProcessId = pTcpExTable->table[i].dwProcessId;
if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwProcessId;
}
} if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
}
else
{
// 表明为 Vista 或者 7 操作系统
PMIB_TCPEXTABLE_VISTA pTcpExTable = NULL;
PFNInternalGetTcpTable2 pInternalGetTcpTable2 =
(PFNInternalGetTcpTable2)GetProcAddress(hModule, "InternalGetTcpTable2");
if (pInternalGetTcpTable2 == NULL)
{
if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
} if (pInternalGetTcpTable2(&pTcpExTable, GetProcessHeap(), ))
{
if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
} for (UINT i = ;i < pTcpExTable->dwNumEntries; i++)
{
// 过滤掉数据,只查询我们需要的进程数据
if(dwPort == ntohs(0x0000FFFF & pTcpExTable->table[i].dwLocalPort))
{
DWORD dwProcessId = pTcpExTable->table[i].dwProcessId;
if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwProcessId;
}
} if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
}
}
else if(type == UdpType)
{
// 表明查询的是 UDP 信息
PMIB_UDPEXTABLE pUdpExTable = NULL;
PFNAllocateAndGetUdpExTableFromStack pAllocateAndGetUdpExTableFromStack;
pAllocateAndGetUdpExTableFromStack =
(PFNAllocateAndGetUdpExTableFromStack)GetProcAddress(hModule,"AllocateAndGetUdpExTableFromStack");
if (pAllocateAndGetUdpExTableFromStack != NULL)
{
// 表明为 XP 或者 Server 2003 操作系统
if (pAllocateAndGetUdpExTableFromStack(&pUdpExTable, TRUE, GetProcessHeap(), , AF_INET) != )
{
if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
} for (UINT i = ; i < pUdpExTable->dwNumEntries; i++)
{
// 过滤掉数据,只查询我们需要的进程数据
if (dwPort == ntohs(0x0000FFFF & pUdpExTable->table[i].dwLocalPort))
{
DWORD dwProcessId = pUdpExTable->table[i].dwProcessId;
if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwProcessId;
}
} if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
}
else
{
// 表明为 Vista 或者 7 操作系统
PFNInternalGetUdpTableWithOwnerPid pInternalGetUdpTableWithOwnerPid;
pInternalGetUdpTableWithOwnerPid =
(PFNInternalGetUdpTableWithOwnerPid)GetProcAddress(hModule, "InternalGetUdpTableWithOwnerPid");
if (pInternalGetUdpTableWithOwnerPid != NULL)
{
if (pInternalGetUdpTableWithOwnerPid(&pUdpExTable, GetProcessHeap(), ))
{
if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
} for (UINT i = ; i < pUdpExTable->dwNumEntries; i++)
{
// 过滤掉数据,只查询我们需要的进程数据
if (dwPort == ntohs(0x0000FFFF & pUdpExTable->table[i].dwLocalPort))
{
DWORD dwProcessId = pUdpExTable->table[i].dwProcessId;
if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwProcessId;
}
}
} if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return ;
}
}
else
{
FreeLibrary(hModule);
hModule = NULL; return -;
}
} //=====================================================================================//
//Name: DWORD GetAllPortByProcessId() //
// //
//Descripion: 根据进程 ID 来求出该进程所打开的所有的端口号,并且在 dwAllPort 数组中返回所有端口号 //
// 其中 dwMaxLen 为数组的长度,函数的返回值为进程所打开的端口的数目 //
// (支持 XP,Server 2003,Vista,Win7) //
// //
//=====================================================================================//
DWORD GetAllPortByProcessId(TcpOrUdp type, DWORD dwProcessId, DWORD * dwAllPort, DWORD dwMaxLen)
{
DWORD dwPortCount = ;
HMODULE hModule = LoadLibraryW(L"iphlpapi.dll");
if (hModule == NULL)
{
return dwPortCount;
} if(type == TcpType)
{
// 表明查询的是 UDP 信息
PFNAllocateAndGetTcpExTableFromStack pAllocateAndGetTcpExTableFromStack;
pAllocateAndGetTcpExTableFromStack = (PFNAllocateAndGetTcpExTableFromStack)GetProcAddress(hModule, "AllocateAndGetTcpExTableFromStack");
if (pAllocateAndGetTcpExTableFromStack != NULL)
{
// 表明为 XP 或者 Server 2003 操作系统
PMIB_TCPEXTABLE pTcpExTable = NULL;
if (pAllocateAndGetTcpExTableFromStack(&pTcpExTable, TRUE, GetProcessHeap(), , AF_INET) != )
{
if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
} for (UINT i = ; i < pTcpExTable->dwNumEntries; i++)
{
// 过滤掉数据,只获取我们要查询的进程的 Port 信息
if(dwProcessId == pTcpExTable->table[i].dwProcessId)
{
if(dwPortCount < dwMaxLen)
{
dwAllPort[dwPortCount] = ntohs(0x0000FFFF & pTcpExTable->table[i].dwLocalPort);
dwPortCount++;
}
}
} if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
}
else
{
// 表明为 Vista 或者 7 操作系统
PMIB_TCPEXTABLE_VISTA pTcpExTable = NULL;
PFNInternalGetTcpTable2 pInternalGetTcpTable2 = (PFNInternalGetTcpTable2)GetProcAddress(hModule, "InternalGetTcpTable2");
if (pInternalGetTcpTable2 == NULL)
{
if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
} if (pInternalGetTcpTable2(&pTcpExTable, GetProcessHeap(), ))
{
if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
} for (UINT i = ;i < pTcpExTable->dwNumEntries; i++)
{
// 过滤掉数据,只获取我们要查询的进程的 TCP Port 信息
if(dwProcessId == pTcpExTable->table[i].dwProcessId)
{
if(dwPortCount < dwMaxLen)
{
dwAllPort[dwPortCount] = ntohs(0x0000FFFF & pTcpExTable->table[i].dwLocalPort);
dwPortCount++;
}
}
} if (pTcpExTable)
{
HeapFree(GetProcessHeap(), , pTcpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
}
}
else if(type == UdpType)
{
// 表明查询的是 UDP 信息
PMIB_UDPEXTABLE pUdpExTable = NULL;
PFNAllocateAndGetUdpExTableFromStack pAllocateAndGetUdpExTableFromStack;
pAllocateAndGetUdpExTableFromStack = (PFNAllocateAndGetUdpExTableFromStack)GetProcAddress(hModule,"AllocateAndGetUdpExTableFromStack");
if (pAllocateAndGetUdpExTableFromStack != NULL)
{
// 表明为 XP 或者 Server 2003 操作系统
if (pAllocateAndGetUdpExTableFromStack(&pUdpExTable, TRUE, GetProcessHeap(), , AF_INET) != )
{
if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
} for (UINT i = ; i < pUdpExTable->dwNumEntries; i++)
{
// 过滤掉数据,只获取我们要查询的进程的 UDP Port信息
if(dwProcessId == pUdpExTable->table[i].dwProcessId)
{
if(dwPortCount < dwMaxLen)
{
dwAllPort[dwPortCount] = ntohs(0x0000FFFF & pUdpExTable->table[i].dwLocalPort);
dwPortCount++;
}
}
} if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
}
else
{
// 表明为 Vista 或者 7 操作系统
PFNInternalGetUdpTableWithOwnerPid pInternalGetUdpTableWithOwnerPid;
pInternalGetUdpTableWithOwnerPid = (PFNInternalGetUdpTableWithOwnerPid)GetProcAddress(hModule, "InternalGetUdpTableWithOwnerPid");
if (pInternalGetUdpTableWithOwnerPid != NULL)
{
if (pInternalGetUdpTableWithOwnerPid(&pUdpExTable, GetProcessHeap(), ))
{
if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
} for (UINT i = ; i < pUdpExTable->dwNumEntries; i++)
{
// 过滤掉数据,只获取我们要查询的进程的 UDP Port信息
if(dwProcessId == pUdpExTable->table[i].dwProcessId)
{
if(dwPortCount < dwMaxLen)
{
dwAllPort[dwPortCount] = ntohs(0x0000FFFF & pUdpExTable->table[i].dwLocalPort);
dwPortCount++;
}
}
}
} if (pUdpExTable)
{
HeapFree(GetProcessHeap(), , pUdpExTable);
} FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
}
}
else
{
FreeLibrary(hModule);
hModule = NULL; return dwPortCount;
}
} int main()
{
DWORD dwAllPort[];
DWORD dwMaxLen = ; DWORD dwPort;
DWORD dwProcessId; DWORD dwResult = ; DWORD dwInput = ;
while(dwInput != )
{
cout<<"Input 1: Query TCP Port By Process ID;"<<endl;
cout<<"Input 2: Query UDP Port By Process ID;"<<endl;
cout<<"Input 3: Query Process ID By TCP Port;"<<endl;
cout<<"Input 4: Query Process ID By UDP Port;"<<endl;
cout<<"Input 5: Exit Application;"<<endl<<endl;
cout<<"Please Input Your Select Option: ";
cin>>dwInput;
system("cls"); memset(dwAllPort, , sizeof(DWORD) * dwMaxLen); switch(dwInput)
{
case :
{
cout<<"Please Input The Process ID: ";
cin>>dwProcessId;
dwResult = GetAllPortByProcessId(TcpType, dwProcessId, dwAllPort, dwMaxLen);
cout<<endl<<"=========Query TCP Port By Process ID Result : ========="<<endl;
for(int i=;i<dwResult;i++)
{
cout<<"第 "<<i + <<" 条: "<<dwAllPort[i]<<endl;
}
cout<<"========================================================"<<endl<<endl<<endl;
break;
}
case :
{
cout<<"Please Input The Process ID: ";
cin>>dwProcessId;
dwResult = GetAllPortByProcessId(UdpType, dwProcessId, dwAllPort, dwMaxLen);
cout<<endl<<"=========Query UDP Port By Process ID Result : ========="<<endl;
for(int i=;i<dwResult;i++)
{
cout<<"第 "<<i + <<" 条: "<<dwAllPort[i]<<endl;
}
cout<<"========================================================"<<endl<<endl<<endl;
break;
}
case :
{
cout<<"Please Input The Port: ";
cin>>dwPort;
dwResult = GetProcessIdByPort(TcpType, dwPort);
cout<<endl<<"=========Query Process ID By TCP Port Result : ========="<<endl;
cout<<"结果为: "<<dwResult<<endl;
cout<<"========================================================"<<endl<<endl<<endl;
break;
}
case :
{
cout<<"Please Input The Port: ";
cin>>dwPort;
dwResult = GetProcessIdByPort(UdpType, dwPort);
cout<<endl<<"=========Query Process ID By UDP Port Result : ========="<<endl;
cout<<"结果为: "<<dwResult<<endl;
cout<<"========================================================"<<endl<<endl<<endl;
break;
}
case :
{
return ;
}
default:
{
break;
}
}
} return ;
}
win7 x64下测试通过。
原文地址:http://www.cnblogs.com/BoyXiao/archive/2012/02/20/2359273.html
代码来自:http://blog.csdn.net/change518/article/details/7473475
PID<->Port[转]的更多相关文章
- 9.6 MongoDB一
目录:ASP.NET MVC企业级实战目录 9.6.1 MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统 ...
- [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1
Erlang 增加 Maps数据类型并不是很突然,因为这个提议已经进行了2~3年之久,只不过Joe Armstrong老爷子最近一篇文章Big changes to Erlang掀起不小了风 ...
- [Erlang 0112] Elixir Protocols
Why Elixir 为什么要学习Elixir?答案很简单,为了更好的学习Erlang.这么无厘头的理由? Erlang语法设计几乎没有考虑过取悦开发者,所以学习之初的门槛略高.对于已经克服了最初 ...
- 终于将rsync-3.1.2配置成功,之外还挖掘了一些新的用法
1.为什么要用rsync: 有两台主机,开始准备做HA,考虑到工作量的问题,最终决定将重要文件进行同步即可. 找了一些同步的工具,rsync得到一致好评,速度快,消耗小等等. 2.接着找资料,最后选用 ...
- es-redis
列出一些redis命令: 免得我不是dba,每次用都得翻看文档,很蛋疼.于是写了个连接脚本 [root@elk-redis-test105 ts]# ls conn-redis.sh [root@el ...
- CentOS安装Redis详细教程
构建 Redis redis 目前没有官方 RPM 安装包,我们需要从源代码编译,而为了要编译就需要安装 Make 和 GCC. 如果没有安装过 GCC 和 Make,那么就使用 yum 安装. yu ...
- Redis启动多端口、运行多实例
默认Redis程序安装在/usr/local/redis目录下: 配置文件:/usr/local/redis/redis.conf,该配置文件中配置的端口为默认端口:6379: Redis的启动命令路 ...
- Erlang error handling
Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...
- Redis 哨兵模式实现主从故障互切换
200 ? "200px" : this.width)!important;} --> 介绍 Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 S ...
随机推荐
- Hibernate--(二)增删改查
1.增删改查: public class Test { public static void main(String[] args) { SessionFactory sf = new Configu ...
- Python win32api.keybd_event模拟键盘输入
win32api.keybd_event 该函数原型:keybd_event(bVk, bScan, dwFlags, dwExtraInfo) 第一个参数:虚拟键码(键盘键码对照表见附录): 第二个 ...
- VUE.js入门学习(1)-起步
1.hello world <div id="app">{{content}}</div>var app = new Vue({ el:'#app', da ...
- redis(七)---- SpringBoot和redis整合
SpringBoot和Redis整合非常简单 添加pom依赖 <dependency> <groupId>org.springframework.boot</groupI ...
- h5-动画基本介绍
1.介绍 *{ ; ; } div{ width: 200px; height: 200px; background-color: #5aff61; /*添加动画效果*/ /*1.animation- ...
- findbugs报OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE的修改实例
先看出问题的一段代码 public void encode(String xxxPath, String thumbTmpPath, String imageType) { LOGGER.info(& ...
- 操作实践:maven工程查找工程中多余的jar包
声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 版本迭代过程中对jar的依赖可能会产生变化,一些本不必再依赖的jar包可以因为没有清除而依然留在版本的发布 ...
- 图解kubernetes容器状态同步机制核心实现
在K8s中将Pod调度到某一台Node节点之后,后续的状态维护信息则是由对应机器上的kubelet进行维护,如何实时反馈本地运行状态,并通知apiserver则是设计的难点, 本节主要是通过感知Pod ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 删除数据表
MySQL中删除数据表是非常容易操作的, 但是在进行删除表操作时要非常小心,因为执行删除命令后所有数据都会消失. 语法 以下为删除MySQL数据表的通用语法: DROP TABLE table_nam ...
- Java并发分析—volatile
在https://www.cnblogs.com/xyzyj/p/11148497.html中已经说明了在多线程并发的情况下,会出现数据的不一致问题,但归根结底就是一个原因,在宏观上就是线程的执行顺序 ...