Показать сообщение отдельно
Старый 15.07.2013, 10:25   #14  
michel1971 is offline
michel1971
Участник
 
76 / 63 (3) ++++
Регистрация: 14.01.2011
вот ping через стандартную виндовую DLL (ICMP.DLL)
X++:
client static container ping(str  _ipStr, int _timeOut = 5000)
{
    // IP_STATUS codes returned from IP APIs
    #WinApi
    #define.IP_STATUS_BASE(11000)
    #define.IP_SUCCESS(0)
    #define.IP_BUF_TOO_SMALL(#IP_STATUS_BASE + 1)
    #define.IP_DEST_NET_UNREACHABLE(#IP_STATUS_BASE + 2)
    #define.IP_DEST_HOST_UNREACHABLE(#IP_STATUS_BASE + 3)
    #define.IP_DEST_PROT_UNREACHABLE(#IP_STATUS_BASE + 4)
    #define.IP_DEST_PORT_UNREACHABLE(#IP_STATUS_BASE + 5)
    #define.IP_NO_RESOURCES(#IP_STATUS_BASE + 6)
    #define.IP_BAD_OPTION(#IP_STATUS_BASE + 7)
    #define.IP_HW_ERROR(#IP_STATUS_BASE + 8)
    #define.IP_PACKET_TOO_BIG(#IP_STATUS_BASE + 9)
    #define.IP_REQ_TIMED_OUT(#IP_STATUS_BASE + 10)
    #define.IP_BAD_REQ(#IP_STATUS_BASE + 11)
    #define.IP_BAD_ROUTE(#IP_STATUS_BASE + 12)
    #define.IP_TTL_EXPIRED_TRANSIT(#IP_STATUS_BASE + 13)
    #define.IP_TTL_EXPIRED_REASSEM(#IP_STATUS_BASE + 14)
    #define.IP_PARAM_PROBLEM(#IP_STATUS_BASE + 15)
    #define.IP_SOURCE_QUENCH(#IP_STATUS_BASE + 16)
    #define.IP_OPTION_TOO_BIG(#IP_STATUS_BASE + 17)
    #define.IP_BAD_DESTINATION(#IP_STATUS_BASE + 18)

    // The next group are status codes passed up on status indications to
    // transport layer protocols.
    #define.IP_ADDR_DELETED(#IP_STATUS_BASE + 19)
    #define.IP_SPEC_MTU_CHANGE(#IP_STATUS_BASE + 20)
    #define.IP_MTU_CHANGE(#IP_STATUS_BASE + 21)
    #define.IP_UNLOAD(#IP_STATUS_BASE + 22)
    #define.IP_GENERAL_FAILURE(#IP_STATUS_BASE + 50)
    #define.MAX_IP_STATUS(#IP_GENERAL_FAILURE)
    #define.IP_PENDING(#IP_STATUS_BASE + 255)

    #define.INADDR_NONE(0xFFFFFFFF)

    int ipAddress;
    int ICMPPort;

    Binary PingReply;
    Binary ReplyOption;

    DLL         _winApiDLL;
    DLLFunction IcmpCreateFile;
    DLLFunction IcmpCloseHandle;
    DLLFunction IcmpSendEcho;

    str 50 PingStatusToStr (int StatusCode)
    {
        str ret;

        switch (StatusCode)
        {
        case #IP_SUCCESS: ret = 'IP_SUCCESS'; break;
        case #IP_BUF_TOO_SMALL: ret = 'IP_BUF_TOO_SMALL'; break;
        case #IP_DEST_NET_UNREACHABLE: ret = 'IP_DEST_NET_UNREACHABLE'; break;
        case #IP_DEST_HOST_UNREACHABLE: ret = 'IP_DEST_HOST_UNREACHABLE'; break;
        case #IP_DEST_PROT_UNREACHABLE: ret = 'IP_DEST_PROT_UNREACHABLE'; break;
        case #IP_DEST_PORT_UNREACHABLE: ret = 'IP_DEST_PORT_UNREACHABLE'; break;
        case #IP_NO_RESOURCES: ret = 'IP_NO_RESOURCES'; break;
        case #IP_BAD_OPTION: ret = 'IP_BAD_OPTION'; break;
        case #IP_HW_ERROR: ret = 'IP_HW_ERROR'; break;
        case #IP_PACKET_TOO_BIG: ret = 'IP_PACKET_TOO_BIG'; break;
        case #IP_REQ_TIMED_OUT: ret = 'IP_REQ_TIMED_OUT'; break;
        case #IP_BAD_REQ: ret = 'IP_BAD_REQ'; break;
        case #IP_BAD_ROUTE: ret = 'IP_BAD_ROUTE'; break;
        case #IP_TTL_EXPIRED_TRANSIT: ret = 'IP_TTL_EXPIRED_TRANSIT'; break;
        case #IP_TTL_EXPIRED_REASSEM: ret = 'IP_TTL_EXPIRED_REASSEM'; break;
        case #IP_PARAM_PROBLEM: ret = 'IP_PARAM_PROBLEM'; break;
        case #IP_SOURCE_QUENCH: ret = 'IP_SOURCE_QUENCH'; break;
        case #IP_OPTION_TOO_BIG: ret = 'IP_OPTION_TOO_BIG'; break;
        case #IP_BAD_DESTINATION: ret = 'IP_BAD_DESTINATION'; break;
        case #IP_ADDR_DELETED: ret = 'IP_ADDR_DELETED'; break;
        case #IP_SPEC_MTU_CHANGE: ret = 'IP_SPEC_MTU_CHANGE'; break;
        case #IP_MTU_CHANGE: ret = 'IP_MTU_CHANGE'; break;
        case #IP_UNLOAD: ret = 'IP_UNLOAD'; break;
        case #IP_GENERAL_FAILURE: ret = 'IP_GENERAL_FAILURE'; break;
        default:  ret = ''; break;
        }
        return ret;
    }

    PingReply   = new Binary(256);
    ReplyOption = new Binary(256);
    _winApiDLL  = new DLL(#ICMP);

    IcmpCreateFile  = new DLLFunction(_winApiDLL, 'IcmpCreateFile');
    IcmpCloseHandle = new DLLFunction(_winApiDLL, 'IcmpCloseHandle');
    IcmpSendEcho    = new DLLFunction(_winApiDLL, 'IcmpSendEcho');

    IcmpCreateFile.returns(ExtTypes::DWord);

    IcmpCloseHandle.arg(ExtTypes::DWord);
    IcmpCloseHandle.returns(ExtTypes::DWord);

    IcmpSendEcho.arg(ExtTypes::DWord,
                     ExtTypes::DWord,
                     ExtTypes::String,
                     ExtTypes::DWord,
                     ExtTypes::DWord,
                     ExtTypes::Pointer,
                     ExtTypes::DWord,
                     ExtTypes::DWord);
    IcmpSendEcho.returns(ExtTypes::DWord);

    ipAddress = WinApi::isIPAddress(_ipStr);
    if (ipAddress == #INADDR_NONE)
        throw error(strfmt("Передаваемый параметр %1 не является IP адресом", _ipStr));

    ICMPPort = IcmpCreateFile.call();

   if(ICMPPort == #INVALID_HANDLE_VALUE)
        throw error(strfmt("Вызов функции %1 завершился с ошибкой", "IcmpCreateFile"));

    IcmpSendEcho.call(ICMPPort, ipAddress, "", 0 , 0, PingReply, 256 ,_timeOut);

    IcmpCloseHandle.call(ICMPPort);

    return [PingReply.dWord(#offset4), PingStatusToStr(PingReply.dWord(#offset4))];

}