查询端口是否被占用,并找出占用端口的进程
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
windows下的一个查询端口是否被占用的函数,若端口被占用,则输出占用该端口的进程。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
bool CheckPortState( IN unsigned num) { PMIB_TCPTABLE_OWNER_PID pTcpTable; pTcpTable = new MIB_TCPTABLE_OWNER_PID; //获取所需要的内存大小 DWORD tmpSize = sizeof(MIB_TCPTABLE_OWNER_PID); GetExtendedTcpTable( pTcpTable, &tmpSize,false , AF_INET, TCP_TABLE_OWNER_PID_ALL, 0); //分配足够大小的内存并获取端口信息 DWORD dwSize = tmpSize/sizeof(MIB_TCPTABLE_OWNER_PID); delete pTcpTable; pTcpTable = NULL; pTcpTable = new MIB_TCPTABLE_OWNER_PID[dwSize]; GetExtendedTcpTable( pTcpTable, &tmpSize, true, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0); //判断端口是否被占用,并找出占用端口的进程,对于某些system权限的进程需要提权 for (int i = 0; i < (int) pTcpTable->dwNumEntries; i++) { if ( num == ntohs( (u_short) pTcpTable->table[i].dwLocalPort ) ) { HANDLE provileges = NULL; LUID Luid; //提权操作 if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES| TOKEN_QUERY, &provileges) ) { long res = GetLastError(); cout<<"error code "<<res<<endl; if (pTcpTable != NULL) { delete []pTcpTable; pTcpTable = NULL; } return false; } if (!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&Luid)) { cout<<"LookupPrivilegeValue err!"<<endl; if (pTcpTable != NULL) { delete []pTcpTable; pTcpTable = NULL; } return false; } TOKEN_PRIVILEGES tp; tp.PrivilegeCount=1; tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED; tp.Privileges[0].Luid=Luid; if (!AdjustTokenPrivileges(provileges,0,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL)) { cout<<"AdjustTokenPrivileges err!"<<endl; if (pTcpTable != NULL) { delete []pTcpTable; pTcpTable = NULL; } return false; } HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, false, pTcpTable->table[i].dwOwningPid); if ( hProcess == NULL ) { long res = GetLastError(); cout<<"error code "<<res<<endl; if (pTcpTable != NULL) { delete []pTcpTable; pTcpTable = NULL; } return false; } wchar_t wsProcessName[MAX_PATH + 1] = {0}; DWORD len = MAX_PATH; if ( QueryFullProcessImageName(hProcess, 0, wsProcessName, &len) ) { wcout<<L"Port["<<num<<L"] is occupied "<<L"by process["<<wsProcessName<<L"] PID[" <<pTcpTable->table[i].dwOwningPid<<L"]"<<endl; CloseHandle(hProcess); } else { CloseHandle(hProcess); hProcess = NULL; if (pTcpTable != NULL) { delete []pTcpTable; pTcpTable = NULL; } return false; } } } if (pTcpTable != NULL) { delete []pTcpTable; pTcpTable = NULL; } return true; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
bool CheckUdpPortState(IN unsigned num ) { PMIB_UDPTABLE_OWNER_PID pUdpTable; pUdpTable = new MIB_UDPTABLE_OWNER_PID; //获取所需要的内存大小 DWORD tmpSize = sizeof(MIB_UDPTABLE_OWNER_PID); GetExtendedUdpTable( pUdpTable, &tmpSize,false , AF_INET, UDP_TABLE_OWNER_PID, 0); //分配足够大小的内存并获取端口信息 DWORD dwSize = tmpSize/sizeof(MIB_UDPTABLE_OWNER_PID); delete pUdpTable; pUdpTable = NULL; pUdpTable = new MIB_UDPTABLE_OWNER_PID[dwSize]; GetExtendedUdpTable( pUdpTable, &tmpSize, true, AF_INET, UDP_TABLE_OWNER_PID, 0); HANDLE provileges = NULL; LUID Luid; //提权操作 if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES| TOKEN_QUERY, &provileges) ) { long res = GetLastError(); cout<<"error code "<<res<<endl; if (pUdpTable != NULL) { delete []pUdpTable; pUdpTable = NULL; } return false; } if (!LookupPrivilegeValue(NULL,SE_DEBUG_NAME,&Luid)) { cout<<"LookupPrivilegeValue err!"<<endl; if (pUdpTable != NULL) { delete []pUdpTable; pUdpTable = NULL; } return false; } TOKEN_PRIVILEGES tp; tp.PrivilegeCount=1; tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED; tp.Privileges[0].Luid=Luid; if (!AdjustTokenPrivileges(provileges,0,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL)) { cout<<"AdjustTokenPrivileges err!"<<endl; if (pUdpTable != NULL) { delete []pUdpTable; pUdpTable = NULL; } return false; } //判断端口是否被占用,并找出占用端口的进程,对于某些system权限的进程需要提权 for (int i = 0; i < (int) pUdpTable->dwNumEntries; i++) { if ( num == ntohs( (u_short) pUdpTable->table[i].dwLocalPort ) ) { HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, false, pUdpTable->table[i].dwOwningPid); if ( hProcess == NULL ) { long res = GetLastError(); cout<<"error code "<<res<<endl; if (pUdpTable != NULL) { delete []pUdpTable; pUdpTable = NULL; } return false; } wchar_t wsProcessName[MAX_PATH + 1] = {0}; DWORD len = MAX_PATH; if ( QueryFullProcessImageName(hProcess, 0, wsProcessName, &len) ) { wcout<<L"Port["<<num<<L"] is occupied "<<L"by process["<<wsProcessName<<L"] PID[" <<pUdpTable->table[i].dwOwningPid<<L"]"<<endl; CloseHandle(hProcess); } else { CloseHandle(hProcess); hProcess = NULL; if (pUdpTable != NULL) { delete []pUdpTable; pUdpTable = NULL; } return false; } } } if (pUdpTable != NULL) { delete []pUdpTable; pUdpTable = NULL; } return true; } |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1141