📄 pingfunc.c
字号:
#include "windows.h"
#include "Icmpapi.h"
#include "winsock.h"
// return : -1 error in address , 0 - ping FALSE ; 1 - ping TRUE
int Ping( TCHAR *Address , int iTtl , int iWaitTime , TCHAR *szName , int *prtt )
{
IPAddr ipAddress ;
IP_OPTION_INFORMATION ipInfo ;
ICMP_ECHO_REPLY icmpEcho;
HANDLE hFile;
char strHost [ MAX_PATH ] ;
TCHAR szPCName [ MAX_PATH ] = { TEXT( "" ) } ;
int iRet = -1 ;
struct in_addr iaDest; // Internet address structure
LPHOSTENT pHost; // Pointer to host entry structure
_tcscpy( szName , TEXT("") );
WideCharToMultiByte( CP_ACP, 0, Address , -1, strHost, sizeof( strHost ), NULL, FALSE );
ipAddress = inet_addr(strHost);
iaDest.s_addr = inet_addr(strHost);
if (iaDest.s_addr == INADDR_NONE)
{
pHost = gethostbyname(strHost);
if( pHost )
{
char *pIP ;
iaDest.S_un.S_addr = *(DWORD *)(*pHost->h_addr_list) ;
pIP = inet_ntoa( iaDest ) ;
if( strlen( pIP ) )
MultiByteToWideChar( CP_ACP, 0, pIP , -1, szPCName, sizeof( szPCName ) );
if( _tcslen( szPCName ) )
_tcscpy( szName , szPCName );
}
}
else
{
pHost = gethostbyaddr((const char *)&iaDest,
sizeof(struct in_addr), AF_INET);
if( pHost )
{
MultiByteToWideChar( CP_ACP, 0, pHost->h_name , -1, szPCName, sizeof( szPCName ) );
if( _tcslen( szPCName ) )
_tcscpy( szName , szPCName );
}
}
if( pHost )
ipAddress = *(DWORD *)(*pHost->h_addr_list) ;
if (ipAddress != INADDR_NONE)
{
iRet = 0 ;
hFile = IcmpCreateFile();
// Set some reasonable default values
ipInfo.Ttl = iTtl ;
ipInfo.Tos = 0;
ipInfo.Flags = 0;
ipInfo.OptionsSize = 0 ;
ipInfo.OptionsData = NULL ;
icmpEcho.Status = IP_SUCCESS ;
IcmpSendEcho( hFile , ipAddress , NULL , 0 ,
(PIP_OPTION_INFORMATION)&ipInfo,
&icmpEcho , sizeof(ICMP_ECHO_REPLY) , iWaitTime ) ;
IcmpCloseHandle(hFile) ;
if ( icmpEcho.Status == IP_SUCCESS )
iRet = 1 ;
*prtt = icmpEcho.RoundTripTime ;
}
return iRet ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -