//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[转]的更多相关文章

  1. 9.6 MongoDB一

    目录:ASP.NET MVC企业级实战目录 9.6.1 MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统 ...

  2. [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1

         Erlang 增加 Maps数据类型并不是很突然,因为这个提议已经进行了2~3年之久,只不过Joe Armstrong老爷子最近一篇文章Big changes to Erlang掀起不小了风 ...

  3. [Erlang 0112] Elixir Protocols

    Why Elixir   为什么要学习Elixir?答案很简单,为了更好的学习Erlang.这么无厘头的理由? Erlang语法设计几乎没有考虑过取悦开发者,所以学习之初的门槛略高.对于已经克服了最初 ...

  4. 终于将rsync-3.1.2配置成功,之外还挖掘了一些新的用法

    1.为什么要用rsync: 有两台主机,开始准备做HA,考虑到工作量的问题,最终决定将重要文件进行同步即可. 找了一些同步的工具,rsync得到一致好评,速度快,消耗小等等. 2.接着找资料,最后选用 ...

  5. es-redis

    列出一些redis命令: 免得我不是dba,每次用都得翻看文档,很蛋疼.于是写了个连接脚本 [root@elk-redis-test105 ts]# ls conn-redis.sh [root@el ...

  6. CentOS安装Redis详细教程

    构建 Redis redis 目前没有官方 RPM 安装包,我们需要从源代码编译,而为了要编译就需要安装 Make 和 GCC. 如果没有安装过 GCC 和 Make,那么就使用 yum 安装. yu ...

  7. Redis启动多端口、运行多实例

    默认Redis程序安装在/usr/local/redis目录下: 配置文件:/usr/local/redis/redis.conf,该配置文件中配置的端口为默认端口:6379: Redis的启动命令路 ...

  8. Erlang error handling

    Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...

  9. Redis 哨兵模式实现主从故障互切换

    200 ? "200px" : this.width)!important;} --> 介绍 Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 S ...

随机推荐

  1. NiFi之Processor配置

    Processor(处理器)之配置 选择一个Processor,比如ExecuteSQL,从它的名字可以看出该处理器的功能就是去执行一个sql(当然是执行的该sql必须要有返回值的),下面就以Exec ...

  2. python基础(二)抽象

    1 函数与模块 编程大师Martin Fowler先生曾经说过:"代码有很多种坏味道,重复是最坏的一种!" 函数 为了减少代码中重复出现的冗余代码,通常我们选择创建函数来供代码重复 ...

  3. Vuex基本介绍

    1.什么是Vuex Vuex是一个专为vue.js应用程序开发的状态管理模式. 状态管理:data里面的变量都是vue的状态. 2.为什么要用Vuex 当我们构建一个中大型的单页面应用程序时,Vuex ...

  4. C++ 编程学习(六) 函数

    零.小知识点 1.函数返回类型可以是除数组外的自定义类型 一.传值调用 向函数传递参数的传值调用方法,把参数的实际值复制给函数的形式参数.在这种情况下,修改函数内的形式参数不会影响实际参数. 默认情况 ...

  5. Python爬虫实战(一) 使用urllib库爬取拉勾网数据

    本笔记写于2020年2月4日.Python版本为3.7.4,编辑器是VS code 主要参考资料有: B站视频av44518113 Python官方文档 PS:如果笔记中有任何错误,欢迎在评论中指出, ...

  6. HDU——Monkey and Banana 动态规划

                                                                       Monkey and Banana Time Limit:2000 ...

  7. 吴裕雄--天生自然C++语言学习笔记:C++ 存储类

    存储类定义 C++ 程序中变量/函数的范围(可见性)和生命周期.这些说明符放置在它们所修饰的类型之前.下面列出 C++ 程序中可用的存储类: auto register static extern m ...

  8. Unity 协程运行时的监控和优化

    我是快乐的搬运工: http://gulu-dev.com/post/perf_assist/2016-12-20-unity-coroutine-optimizing#toc_0 --------- ...

  9. JS的BOM对象

    BOM对象 (一)简介:BOM对象,即浏览器对象模型: 通过javascript的对象,操作和浏览器相关的操作 B:  Browser,浏览器 O: Object,对象 M: Model,模型 (1) ...

  10. python函数-装饰器

    python函数-装饰器 1.装饰器的原则--开放封闭原则 开放:对于添加新功能是开放的 封闭:对于修改原功能是封闭的 2.装饰器的作用 在不更改原函数调用方式的前提下对原函数添加新功能 3.装饰器的 ...