Ping.cpp
资源名称:NetFinder.rar [点击查看]
上传用户:qiye66692
上传日期:2022-04-25
资源大小:72k
文件大小:2k
源码类别:
SNMP编程
开发平台:
Visual C++
- #include "StdAfx.h"
- #include ".ping.h"
- CPing::CPing(void)
- {
- bValid = false;
- WSADATA wsaData;
- // Dynamically load the ICMP.DLL
- hIcmp = LoadLibrary("ICMP.DLL");
- if (hIcmp == NULL)
- {
- ::MessageBox(NULL, "Could not load ICMP.DLL", "Error:", MB_OK);
- return;
- }
- // Retrieve ICMP function pointers
- IcmpCreateFile = (HANDLE (WINAPI*) (void))
- GetProcAddress( (HMODULE)hIcmp, "IcmpCreateFile" );
- IcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
- GetProcAddress((HMODULE)hIcmp,"IcmpCloseHandle");
- IcmpSendEcho = (DWORD (WINAPI *) (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))
- GetProcAddress((HMODULE)hIcmp,"IcmpSendEcho");
- // Check all the function pointers
- if (IcmpCreateFile == NULL || IcmpCloseHandle == NULL || IcmpSendEcho == NULL)
- {
- ::MessageBox(NULL, "Error loading ICMP.DLL", "Error:", MB_OK);
- FreeLibrary((HMODULE)hIcmp);
- return;
- }
- // Init WinSock
- if( WSAStartup( MAKEWORD(2,0), &wsaData ) != 0 )
- {
- ::MessageBox(NULL, "WSAStartup error!", "Error:", MB_OK);
- WSACleanup();
- FreeLibrary((HMODULE)hIcmp);
- return;
- }
- // Check WinSock version
- if ( wsaData.wVersion != MAKEWORD(2,0) )
- {
- ::MessageBox(NULL, "No WinSock version 2.0 support found", "Error:", MB_OK);
- WSACleanup();
- FreeLibrary((HMODULE)hIcmp);
- return;
- }
- bValid = TRUE;
- }
- CPing::~CPing(void)
- {
- WSACleanup();
- FreeLibrary((HMODULE)hIcmp);
- }
- bool CPing::ping(CString strHost)
- {
- DWORD dwAddress; // IP Address
- IPINFO ipInfo; // IP Options structure
- ICMPECHO icmpEcho; // ICMP Echo reply buffer
- HANDLE hFile; // Handle for IcmpCreateFile()
- if(!bValid)
- {
- return FALSE;
- }
- dwAddress = inet_addr(strHost);
- // Get an ICMP echo request handle
- hFile = IcmpCreateFile();
- // 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
- IcmpSendEcho(
- hFile, // 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(ICMPECHO),
- 500); // Time to wait in milliseconds
- if (icmpEcho.Status)
- {
- return FALSE;
- }
- // Close the echo request file handle
- IcmpCloseHandle(hFile);
- return TRUE;
- }