Netstate command implementation (XP version)

Source: Internet
Author: User

You can use GetTcpTable to obtain the IP address, port, remote IP address, and port of the current process. However, you cannot associate the socket with the process name! You can use AllocateAndGetTcpExTableFromStack to implement this function, but this function is invalid in win7. It may be because it is still troublesome. In win7, we can use GetExtendTcpTable to implement the above functions, this implementation will be provided next time.

 #include <stdio.h> #include <WinSock2.h> #include <stdio.h> #include <iphlpapi.h> #include <tlhelp32.h> #include <> #include <vector> #include <Psapi.h>  comment(lib,"ws2_32.lib")  comment(lib,"Iphlpapi.lib")  comment (lib,"Psapi.lib")      typedef      DWORD dwState;             DWORD dwLocalPort;         DWORD dwRemotePort;         DWORD dwProcessId;          szLocalAddr;          szRemoteAddr;          szProcessName;     }TCPINFO,*   typedef        } MIB_TCPEXROW, *   typedef      } MIB_TCPEXTABLE, *    typedef       } MIB_UDPEXROW, *   typedef      } MIB_UDPEXTABLE, *   typedef DWORD (WINAPI *PFNAllocateAndGetTcpExTableFromStack)(PMIB_TCPEXTABLE * typedef DWORD (WINAPI *PFNAllocateAndGetUdpExTableFromStack)(PMIB_UDPEXTABLE *  vector<TCPINFO>         vector<TCPINFO>     vec =     (vector<TCPINFO>::iterator iter = vec.begin(); iter != vec.end(); iter++           State = ConvertStateToString(iter->         printf(,(iter->szLocalAddr).c_str(),iter->dwLocalPort,(iter->szRemoteAddr).c_str(),iter->         printf(,(iter->szProcessName).c_str(),iter->           vector<TCPINFO>       vector<TCPINFO>     DWORD dwSize =         HMODULE hModule = ::LoadLibrary();         PFNAllocateAndGetTcpExTableFromStack pAllocateAndGetTcpExTableFromStack = (PFNAllocateAndGetTcpExTableFromStack)::GetProcAddress(hModule,      PFNAllocateAndGetUdpExTableFromStack pAllocateAndGetUdpExTableFromStack = (PFNAllocateAndGetUdpExTableFromStack)::GetProcAddress(hModule,        (pAllocateAndGetTcpExTableFromStack(&pTcpExTable, TRUE, GetProcessHeap(), , ) !=           printf(      (pAllocateAndGetUdpExTableFromStack(&pUdpExTable, TRUE, GetProcessHeap(), , ) !=           printf(      (unsigned  count = ; count < pTcpExTable->dwNumEntries; count++          TcpInfo.dwState = pTcpExTable->         TcpInfo.dwRemotePort = htons((u_short)pTcpExTable->table[count].dwRemotePort);             TcpInfo.dwLocalPort = htons((u_short)pTcpExTable->table[count].dwLocalPort);                 InetLocalAddr.s_addr = pTcpExTable->         InetRemoteAddr.s_addr = pTcpExTable->         TcpInfo.szLocalAddr = inet_ntoa(InetLocalAddr);                 TcpInfo.szRemoteAddr = inet_ntoa(InetRemoteAddr);             TcpInfo.dwProcessId = pTcpExTable->table[count].dwProcessId;                     TcpInfo.szProcessName = GetProcessNameByPid(pTcpExTable->table[count].dwProcessId);             vec.push_back(TcpInfo);                         ProcessPath[] = {          HANDLE h_Process = OpenProcess(PROCESS_ALL_ACCESS,     GetModuleFileNameEx(h_Process,NULL,ProcessPath,               * ProcessName = ( *)strrchr(ProcessPath,                       *ProcessName =              ProcessName++          szProcessName =                            :               :               :               :               :               :               :               :               :               :           :           :          :             }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.