ip helper 可能需要wdk支持, 我本机装了wdk, 没试过不用会怎样. 由于icmpsendecho2ex()回调的形式乎与我的wdk有所冲突, 所以这里只演示阻塞模式. event应该不受影响. demo下载地址: http://download.csdn.net/detail/ren0065/8388105 主要代码: void cpingte
ip helper 可能需要wdk支持, 我本机装了wdk, 没试过不用会怎样.
由于icmpsendecho2ex()回调的形式似乎与我的wdk有所冲突, 所以这里只演示阻塞模式. event应该不受影响.
demo下载地址: http://download.csdn.net/detail/ren0065/8388105
主要代码:
void cpingtest1dlg::onbnclickedbtnping(){ // todo: 在此添加控件通知处理程序代码 updatedata(true); handle h_icmp = icmpcreatefile(); ipaddr ip_source = inet_addr(cs_source_ip_); ipaddr ip_destination = inet_addr(cs_destination_ip_); word w_request_data_size = atoi(cs_request_size_); lpvoid p_request_data = calloc(1, w_request_data_size); pip_option_information p_option_info = null; lpvoid p_reply_data = calloc(1, sizeof(icmp_echo_reply) + 8 + w_request_data_size); dword dw_reply_data_size = sizeof(icmp_echo_reply) + 8 + w_request_data_size; dword dw_timeout = atoi(cs_timeout_); dword dw_result = icmpsendecho2ex( h_icmp , null , null , null , ip_source , ip_destination , p_request_data , w_request_data_size , p_option_info , p_reply_data , dw_reply_data_size , dw_timeout); free(p_request_data); cs_output_.empty(); if (dw_result == 0) { dword dw_error = getlasterror(); cstring cs_error_msg; switch (dw_error) { case error_invalid_parameter: cs_error_msg = 无效参数. 当icmphandle是一个无效handle或replysize的值小于icmp_echo_reply或icmp_echo_reply32时会返回该错误.; break; case error_io_pending: cs_error_msg = 异步处理正在进行中.调用icmpsendecho2ex异步模式成功时会返回该值, 不是错误.; break; case error_not_enough_memory: cs_error_msg = 内存不足; break; case error_not_supported: cs_error_msg = 不支持该请求.如果本地计算机没有ipv4协议栈将返回该错误.; break; case ip_buf_too_small: cs_error_msg = replysize指定的太小.; break; default: { hlocal hlocal = null; dword dwerr = getlasterror(); dword systemlocale = makelangid(lang_neutral, sublang_neutral); dword fok = formatmessage(format_message_from_system | format_message_ignore_inserts | format_message_allocate_buffer, null, dwerr, systemlocale, (lptstr)&hlocal, 0, null); cs_error_msg = (lpctstr)locallock(hlocal); break; } } cs_output_ = cs_error_msg; } else { cstring cs_buffer; picmp_echo_reply pechoreply = (picmp_echo_reply)p_reply_data; struct in_addr replyaddr; replyaddr.s_un.s_addr = pechoreply->address; cs_buffer.format(sent icmp message from %s to %s\r\n, cs_source_ip_, cs_destination_ip_); cs_output_ += cs_buffer; if (dw_result > 1) { cs_buffer.format(received %ld icmp message responses\r\n, dw_result); cs_output_ += cs_buffer; cs_buffer.format(information from the first response:\r\n); cs_output_ += cs_buffer; } else { cs_buffer.format(received %ld icmp message response\r\n, dw_result); cs_output_ += cs_buffer; cs_buffer.format(information from this response:\r\n); cs_output_ += cs_buffer; } cs_buffer.format(received from %s\r\n, inet_ntoa(replyaddr)); cs_output_ += cs_buffer; cs_buffer.format(status = %ld , pechoreply->status); cs_output_ += cs_buffer; if (pechoreply->status != ip_success) { dword dw_status_err_msg_size = 1024; pwstr cw_str_error = (pwstr)calloc(1, dw_status_err_msg_size); getiperrorstring(pechoreply->status, cw_str_error, &dw_status_err_msg_size); cs_output_ = ws2s(cw_str_error).c_str(); free(cw_str_error); } else { cs_buffer.format((successful)\r\n); cs_output_ += cs_buffer; cs_buffer.format(rtt: %d\r\n, pechoreply->roundtriptime); cs_output_ += cs_buffer; cs_buffer.format(size: %d\r\n, pechoreply->datasize); cs_output_ += cs_buffer; cs_buffer.format(ttl: %d\r\n, pechoreply->options.ttl); cs_output_ += cs_buffer; } } updatedata(false); free(p_reply_data); icmpclosehandle(h_icmp);}