• vc枚举本机端口信息API


    常用的获取端口信息的函数:

    GetTcpTable
    GetExtendedTcpTable
    GetUdpTable
    GetExtendedUdpTable

    GetTcpTable function

    The GetTcpTable function retrieves the IPv4 TCP connection table.

    Syntax

    C++
    ULONG GetTcpTable(
      PMIB_TCPTABLE TcpTable,
      PULONG        SizePointer,
      BOOL          Order
    );
    

    Parameters

    TcpTable

    A pointer to a buffer that receives the TCP connection table as a MIB_TCPTABLE structure.

    SizePointer

    On input, specifies the size in bytes of the buffer pointed to by the pTcpTable parameter.

    On output, if the buffer is not large enough to hold the returned connection table, the function sets this parameter equal to the required buffer size in bytes.

    On the Windows SDK released for Windows Vista and later, the data type for this parameter is changed to a PULONG which is equivalent to a PDWORD.

    Order

    A Boolean value that specifies whether the TCP connection table should be sorted. If this parameter is TRUE, the table is sorted in the order of:

    1. Local IP address
    2. Local port
    3. Remote IP address
    4. Remote port

    Return Value

    If the function succeeds, the return value is NO_ERROR.

    If the function fails, the return value is one of the following error codes.

    Return codeDescription
    ERROR_INSUFFICIENT_BUFFER
    The buffer pointed to by the pTcpTable parameter is not large enough. The required size is returned in the DWORD variable pointed to by the pdwSizeparameter.

    This error is also returned if the pTcpTable parameter is NULL.

    ERROR_INVALID_PARAMETER
    The pdwSize parameter is NULL, or GetTcpTable is unable to write to the memory pointed to by the pdwSize parameter.
    ERROR_NOT_SUPPORTED
    This function is not supported on the operating system in use on the local system.
    STATUS_UNSUCCESSFUL
    If you receive this return code then calling the function again is usually enough to clear the issue and get the desired result. This return code can be a consequence of the system being under high load. For example, if the size of the TCP connection table changes by more than 2 additional items 3 consecutive times.
    Other
    Use FormatMessage to obtain the message string for the returned error.

    Remarks

    On the Windows SDK released for Windows Vista and later, the return value from the GetTcpTable function is changed to a data type of ULONG which is equivalent to a DWORD.

    Examples

    The following example retrieves the TCP connection table for IPv4 and prints the state of each connection.

    C++
    // Need to link with Iphlpapi.lib and Ws2_32.lib
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iphlpapi.h>
    #include <stdio.h>
    
    #pragma comment(lib, "iphlpapi.lib")
    #pragma comment(lib, "ws2_32.lib")
    
    #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
    #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
    
    /* Note: could also use malloc() and free() */
    
    int main()
    {
    
        // Declare and initialize variables
        PMIB_TCPTABLE pTcpTable;
        DWORD dwSize = 0;
        DWORD dwRetVal = 0;
    
        char szLocalAddr[128];
        char szRemoteAddr[128];
    
        struct in_addr IpAddr;
    
        int i;
    
        pTcpTable = (MIB_TCPTABLE *) MALLOC(sizeof (MIB_TCPTABLE));
        if (pTcpTable == NULL) {
            printf("Error allocating memory
    ");
            return 1;
        }
    
        dwSize = sizeof (MIB_TCPTABLE);
    // Make an initial call to GetTcpTable to
    // get the necessary size into the dwSize variable
        if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) ==
            ERROR_INSUFFICIENT_BUFFER) {
            FREE(pTcpTable);
            pTcpTable = (MIB_TCPTABLE *) MALLOC(dwSize);
            if (pTcpTable == NULL) {
                printf("Error allocating memory
    ");
                return 1;
            }
        }
    // Make a second call to GetTcpTable to get
    // the actual data we require
        if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == NO_ERROR) {
            printf("	Number of entries: %d
    ", (int) pTcpTable->dwNumEntries);
            for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
                IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
                strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));
                IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
                strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));
    
                printf("
    	TCP[%d] State: %ld - ", i,
                       pTcpTable->table[i].dwState);
                switch (pTcpTable->table[i].dwState) {
                case MIB_TCP_STATE_CLOSED:
                    printf("CLOSED
    ");
                    break;
                case MIB_TCP_STATE_LISTEN:
                    printf("LISTEN
    ");
                    break;
                case MIB_TCP_STATE_SYN_SENT:
                    printf("SYN-SENT
    ");
                    break;
                case MIB_TCP_STATE_SYN_RCVD:
                    printf("SYN-RECEIVED
    ");
                    break;
                case MIB_TCP_STATE_ESTAB:
                    printf("ESTABLISHED
    ");
                    break;
                case MIB_TCP_STATE_FIN_WAIT1:
                    printf("FIN-WAIT-1
    ");
                    break;
                case MIB_TCP_STATE_FIN_WAIT2:
                    printf("FIN-WAIT-2 
    ");
                    break;
                case MIB_TCP_STATE_CLOSE_WAIT:
                    printf("CLOSE-WAIT
    ");
                    break;
                case MIB_TCP_STATE_CLOSING:
                    printf("CLOSING
    ");
                    break;
                case MIB_TCP_STATE_LAST_ACK:
                    printf("LAST-ACK
    ");
                    break;
                case MIB_TCP_STATE_TIME_WAIT:
                    printf("TIME-WAIT
    ");
                    break;
                case MIB_TCP_STATE_DELETE_TCB:
                    printf("DELETE-TCB
    ");
                    break;
                default:
                    printf("UNKNOWN dwState value
    ");
                    break;
                }
                printf("	TCP[%d] Local Addr: %s
    ", i, szLocalAddr);
                printf("	TCP[%d] Local Port: %d 
    ", i,
                       ntohs((u_short)pTcpTable->table[i].dwLocalPort));
                printf("	TCP[%d] Remote Addr: %s
    ", i, szRemoteAddr);
                printf("	TCP[%d] Remote Port: %d
    ", i,
                       ntohs((u_short)pTcpTable->table[i].dwRemotePort));
            }
        } else {
            printf("	GetTcpTable failed with %d
    ", dwRetVal);
            FREE(pTcpTable);
            return 1;
        }
    
        if (pTcpTable != NULL) {
            FREE(pTcpTable);
            pTcpTable = NULL;
        }    
    
        return 0;    
    }

    Requirements

      
    Minimum supported client Windows 2000 Professional [desktop apps | UWP apps]
    Minimum supported server Windows 2000 Server [desktop apps | UWP apps]
    Target Platform Windows
    Header iphlpapi.h
    Library Iphlpapi.lib
    DLL Iphlpapi.dll
  • 相关阅读:
    nginx相关总结
    nginx 虚拟主机+反向代理+负载均衡
    linux文本查看与搜索
    mysqldump导出数据出现问题
    转载 | 缩小浏览器窗口右边出现空白
    转载 | Sublime Text3 安装以及初次配置
    转载 | Sublime text3 实用快捷键整理
    转载 | SVG向下兼容优雅降级方法
    CSS等分布局方法
    如何用实现文字环绕图片?
  • 原文地址:https://www.cnblogs.com/2018shawn/p/10316978.html
Copyright © 2020-2023  润新知