Ping (ICMP.dll) 简单实现
ckfan
posted @ 2012年10月31日 17:24
in 技术blog
, 2012 阅读
代码: .h #pragma once #include <WinSock.h> // ping 操作接口 typedef struct ip_option_information { UCHAR Ttl; UCHAR Tos; UCHAR Flags; UCHAR OptionsSize; PUCHAR OptionsData; } IP_OPTION_INFORMATION, *PIP_OPTION_INFORMATION; typedef struct icmp_echo_reply { ULONG Address; ULONG Status; ULONG RoundTripTime; // 往返时间 USHORT DataSize; USHORT Reserved; PVOID Data; IP_OPTION_INFORMATION Options; } ICMP_ECHO_REPLY, *PICMP_ECHO_REPLY; typedef HANDLE (WINAPI IcmpCreateFile)(VOID); typedef BOOL (WINAPI IcmpCloseHandle)(HANDLE IcmpHandle); typedef DWORD (WINAPI IcmpSendEcho)( // 传入由IcmpCreateFile创建的一个测试句柄 HANDLE IcmpHandle, // 要测试的IP地址,(需要用到inet_addr() 函数来转换) ULONG DestinationAddress, // 需要发送到IP地址的数据,传入指针 LPVOID RequestData, // 发送的数据大小 WORD RequestSize, // IP头选项,传入指针 PIP_OPTION_INFORMATION RequestOptions, // 测试后返回的数据,用icmp_echo_reply来接收 LPVOID ReplyBuffer, // ReplyBuffer的大小,一般是 sizeof(icmp_echo_reply) DWORD ReplySize, // 一个以MS为单位的值,代表着一个超时值 DWORD Timeout); class CPingI { public: CPingI(); ~CPingI(); BOOL Ping(int timeout, LPCTSTR pHost); private: HINSTANCE hIcmpDLL; IcmpCreateFile* lpIcmpCreateFile; IcmpCloseHandle* lpIcmpCloseHandle; IcmpSendEcho* lpIcmpSendEcho; }; .cpp #include "stdafx.h" #include "PingI.h" CPingI::CPingI() { // load hIcmpDLL = LoadLibrary("ICMP.DLL"); if (hIcmpDLL == NULL) { return; } lpIcmpCreateFile = (IcmpCreateFile*)GetProcAddress(hIcmpDLL,"IcmpCreateFile"); lpIcmpCloseHandle = (IcmpCloseHandle*)GetProcAddress(hIcmpDLL,"IcmpCloseHandle"); lpIcmpSendEcho = (IcmpSendEcho*)GetProcAddress(hIcmpDLL,"IcmpSendEcho"); if (lpIcmpSendEcho == NULL || lpIcmpCloseHandle == NULL || lpIcmpCreateFile == NULL) { return; } } CPingI::~CPingI() { if (hIcmpDLL != NULL) { FreeLibrary(hIcmpDLL); hIcmpDLL = NULL; } } // ping x.x.x.x BOOL CPingI::Ping(int timeout, LPCTSTR pHost) { IP_OPTION_INFORMATION ip_option; UCHAR sendData[128]; UCHAR recData[128]; HANDLE handle; PICMP_ECHO_REPLY pEchoReply; // 应答个数 int nReply; // error if (lpIcmpSendEcho == NULL || lpIcmpCloseHandle == NULL || lpIcmpCreateFile == NULL) { return FALSE; } memset(&ip_option, 0, sizeof(IP_OPTION_INFORMATION)); // TTL ip_option.Ttl = 128; // send Data memset(sendData, '1', 128); // rec Data memset(recData, 0, 128); pEchoReply = (PICMP_ECHO_REPLY)recData; // create handle handle = lpIcmpCreateFile(); if (handle == INVALID_HANDLE_VALUE) { return FALSE; } // send nReply = lpIcmpSendEcho(handle, inet_addr(pHost), sendData, 64, &ip_option, pEchoReply, sizeof(ICMP_ECHO_REPLY) + 64, timeout); // close lpIcmpCloseHandle(handle); if (nReply == 0) { // GetLastErr SetLastError(pEchoReply->Status); return FALSE; } return TRUE; }
2021年9月01日 16:53
The position of being a parent involves managing lots involving mess. Moms (specially housewives) are generally mostly in the heart of messy work opportunities. Sometimes these are found wiping his or her sticky fingers and also other times these are sorting by way of mental hardship. For these people, the fact of calmness for quite a while tends to get painfully incredibly elusive. For these people, standard home cleaning services are generally nothing below a great asset. The moods involving moms are actually observed to switch for better, once his or her household cleanup work does.
2024年1月30日 21:58
Prints are one the perfect ways so that you can decorate dwellings and workplaces. There is not a one who seem to doesn't appreciate a nice oil painting them. While getting oil prints is an element that most people today aspire to get, the amount factor can stop people from completing this task, more normally than never. You don't have to worry while, for there's a simple solution available by using wholesale petrol paintings.