MFC implements the Ping function. Sorted

Source: Internet
Author: User
// 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.