📄 cping.cpp
字号:
// CPing.cpp: implementation of the CPing class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "try4.h"
#include "CPing.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPing::CPing()
{
bValid = FALSE;
WSADATA wsaData;
// HANDLE (WINAPI *pIcmpCreateFile)(VOID);
// BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);
// DWORD (WINAPI *pIcmpSendEcho)(HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);
hndlIcmp = LoadLibrary("ICMP.DLL");
if (hndlIcmp == NULL)
{
::MessageBox(NULL, "Could not load ICMP.DLL", "Error:", MB_OK);
return;
}
// Retrieve ICMP function pointers
pIcmpCreateFile = (HANDLE (WINAPI *)(void))GetProcAddress((HMODULE)hndlIcmp,"IcmpCreateFile");
pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))GetProcAddress((HMODULE)hndlIcmp,"IcmpCloseHandle");
pIcmpSendEcho = (DWORD (WINAPI *)(HANDLE,DWORD,LPVOID,WORD,LPVOID,LPVOID,DWORD,DWORD))GetProcAddress((HMODULE)hndlIcmp,"IcmpSendEcho");
// Check all the function pointers
if (pIcmpCreateFile == NULL||pIcmpCloseHandle == NULL||pIcmpSendEcho == NULL)
{
::MessageBox(NULL, "Error loading ICMP.DLL", "Error:", MB_OK);
FreeLibrary((HMODULE)hndlIcmp);
return;
}
// Init WinSock
int nRet = WSAStartup(0x0101, &wsaData );
if (nRet)
{
::MessageBox(NULL, "WSAStartup() error:", "Error:", MB_OK);
WSACleanup();
FreeLibrary((HMODULE)hndlIcmp);
return;
}
// Check WinSock version
if (0x0101 != wsaData.wVersion)
{
::MessageBox(NULL, "No WinSock version 1.1 support found", "Error:", MB_OK);
WSACleanup();
FreeLibrary((HMODULE)hndlIcmp);
return;
}
bValid = TRUE;
}
CPing::~CPing()
{
WSACleanup();
FreeLibrary((HMODULE)hndlIcmp);
}
BOOL CPing::Ping(char* strHost)
{
struct in_addr iaDest; // Internet address structure
LPHOSTENT pHost; // Pointer to host entry structure
DWORD *dwAddress; // IP Address
IPINFO ipInfo; // IP Options structure
ICMPECHO icmpEcho; // ICMP Echo reply buffer
HANDLE hndlFile; // Handle for IcmpCreateFile()
if(!bValid)
{
return FALSE;
}
// Lookup destination
// Use inet_addr() to determine if we're dealing with a name
// or an address
iaDest.S_un.S_addr = inet_addr(strHost);
if (iaDest.S_un.S_addr == INADDR_NONE)
pHost = gethostbyname(strHost);
else
pHost = gethostbyaddr((const char *)&iaDest,
sizeof(struct in_addr), AF_INET);
if (pHost == NULL)
{
return FALSE;
}
// Copy the IP address
dwAddress = (DWORD *)(*pHost->h_addr_list);
// Get an ICMP echo request handle
hndlFile = pIcmpCreateFile();
// Set some reasonable default values
ipInfo.Ttl = 255;
ipInfo.Tos = 0;
ipInfo.IPFlags = 0;
ipInfo.OptSize = 0;
ipInfo.Options = NULL;
icmpEcho.Status = 0;
// Reqest an ICMP echo
pIcmpSendEcho(
hndlFile, // Handle from IcmpCreateFile()
*dwAddress, // Destination IP address
NULL, // Pointer to buffer to send
0, // Size of buffer in bytes
&ipInfo, // Request options
&icmpEcho, // Reply buffer
sizeof(struct tagICMPECHO),
1000); // Time to wait in milliseconds
// Print the results
iaDest.S_un.S_addr = icmpEcho.Address;
// AfxMessageBox(icmpEcho.Source);
if (icmpEcho.Status)
{
return FALSE;
}
// Close the echo request file handle
pIcmpCloseHandle(hndlFile);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -