// Parameter: // sztarget address (which can be a domain name or IP address) // dwtime return time; if it is time-out, it is equal to infinitebool Ping (lpctstr sztarget, DWORD & dwtime) {bool bsuccess = false; dwtime = infinite; If (sztarget = NULL) {trace0 ("target is null \ n"); Return false;} tchar sz1_line [80]; if (_ sntprintf (szcmdline, sizeof (szcmdline)/sizeof (tchar), _ T ("ping.exe-N 1% s"), sztarget) = sizeof (szcmdline) /sizeof (tchar) {trace0 ("target is too long \ n "); Return false;} Handle hwritepipe = NULL; handle hreadpipe = NULL; handle hwriteshell = NULL; handle hreadshell = NULL; security_attributes SA; memset (& SA, 0, sizeof (SA); SA. nlength = sizeof (SA); SA. binherithandle = true; SA. lpsecuritydescriptor = NULL; If (createpipe (& hreadpipe, & hreadshell, & SA, 0) & createpipe (& hwriteshell, & hwritepipe, & SA, 0) {startupinfo Si; memset (& Si, 0, sizeof (SI )); Si. CB = sizeof (SI); SI. dwflags = startf_useshowwindow | startf_usestdhandles; SI. hstdinput = hwriteshell; SI. hstdoutput = hreadshell; SI. hstderror = hreadshell; SI. wshowwindow = sw_hide; process_information PI; memset (& Pi, 0, sizeof (PI); int Nmin =-1, Nmax =-1, navg =-1; if (CreateProcess (null, szcmdline, null, null, true, 0, null, null, & Si, & PI) {If (waitforsingleobject (Pi. hprocess, 10000) = = Wait_object_0) {tchar szbuffer [1024]; DWORD dwbytes; If (readfile (hreadpipe, szbuffer, sizeof (szbuffer), & dwbytes, null )) {szbuffer [dwbytes] = '\ 0'; lptstr lpsztime = NULL; lpsztime = _ tcsstr (szbuffer, _ T ("request timed out ")); if (lpsztime = NULL) {lpsztime = _ tcsstr (szbuffer, _ T ("minimum"); If (lpsztime! = NULL) {If (_ stscanf (lpsztime, _ T ("minimum = % DMS, maximum = % DMS, average = % DMS"), & Nmin, & Nmax, & navg) = 3) {trace3 ("% d, % d, % d \ n", Nmin, Nmax, navg); dwtime = navg; bsuccess = true ;}} else {trace0 ("Ping format is error \ n") ;}} else {trace0 ("Ping is Time Out \ n"); bsuccess = true ;}}} else {trace1 ("process (% d) is time out \ n", Pi. dwprocessid); terminateprocess (Pi. hprocess, 0);} closeha Ndle (Pi. hthread); closehandle (Pi. hprocess); trace3 (_ T ("minimum = % DMS, maximum = % DMS, average = % DMS \ n"), Nmin, Nmax, navg );}} if (hwritepipe! = NULL) closehandle (hwritepipe); If (hreadpipe! = NULL) closehandle (hreadpipe); If (hwriteshell! = NULL) closehandle (hwriteshell); If (hreadshell! = NULL) closehandle (hreadshell); Return bsuccess ;}