2019-12-18
展开全部
//关键代码如下:
#include "icmpapi.h"
#include "iphlpapi.h"
#pragma comment(lib,"iphlpapi.lib");
bool CPINGDlg::startping(char* IP) //成功返回true, 失败返回false
{
HANDLE hIcmpFile = NULL;
unsigned long ipaddr = INADDR_NONE;
DWORD dwRetVal = 0;
char SendData[] = "Data Buffer\0";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
// PICMP_ECHO_REPLY reply;
ipaddr = inet_addr(IP);
ReplySize = sizeof(ICMP_ECHO_REPLY)+sizeof(SendData);
ReplyBuffer = (void*)malloc(ReplySize);
hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Ping 内部代码错误"));
return false;
}
dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 1000);
// CloseHandle(hIcmpFile);
// free(ReplyBuffer);
if (dwRetVal != 0)
{
return true;
}
else
{
return false;
}
}
#include "icmpapi.h"
#include "iphlpapi.h"
#pragma comment(lib,"iphlpapi.lib");
bool CPINGDlg::startping(char* IP) //成功返回true, 失败返回false
{
HANDLE hIcmpFile = NULL;
unsigned long ipaddr = INADDR_NONE;
DWORD dwRetVal = 0;
char SendData[] = "Data Buffer\0";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
// PICMP_ECHO_REPLY reply;
ipaddr = inet_addr(IP);
ReplySize = sizeof(ICMP_ECHO_REPLY)+sizeof(SendData);
ReplyBuffer = (void*)malloc(ReplySize);
hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Ping 内部代码错误"));
return false;
}
dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 1000);
// CloseHandle(hIcmpFile);
// free(ReplyBuffer);
if (dwRetVal != 0)
{
return true;
}
else
{
return false;
}
}
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询