win32接口获取ping值

int QAData::Data::PingServerDelay(string strIPAdress)
{
    // Declare and initialize variables
    int i;
    HANDLE hIcmpFile;
    unsigned long ipaddr = INADDR_NONE;
    DWORD dwRetVal = 0;
    char SendData[] = "Data Buffer";
    LPVOID ReplyBuffer = NULL;
    DWORD ReplySize = 0;

    ipaddr = inet_addr(strIPAdress.c_str());
    if (ipaddr == INADDR_NONE) {
        return -1;
    }

    hIcmpFile = IcmpCreateFile();
    if (hIcmpFile == INVALID_HANDLE_VALUE) {
        return -1;
    }
    ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
    ReplyBuffer = (VOID*)malloc(ReplySize);
    if (ReplyBuffer == NULL) {
        return -1;
    }

    dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 1000);
    if (dwRetVal != 0) {
        PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
        return pEchoReply->RoundTripTime;
    }
    else {
        return -1;
    }
}

 

posted @ 2017-11-22 15:11  卖杏花的陆游  阅读(286)  评论(0编辑  收藏  举报