⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ping.cpp

📁 你可以通过学习本例子
💻 CPP
字号:
/*

  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  
  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO

  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
	  
  PARTICULAR PURPOSE.
		
		  
  This is "Sample Code" and is distributable subject to the terms of the end user license agreement.
			  
*/

// ****************************************************************************
// FILE: ping.cpp
// ABTRACT: Main implementation file that supports the dialog, gathers user
//			input, resolves an IP if required, pings the ip and displays the
//			ping results (RTT and datasize)
// ****************************************************************************
//

#include "stdafx.h"
#include "ping.h"

#define MAX_ENTRYLEN	255
#define	NUMBEROFREPLIES	4
#define MSTOWAIT		500
// Global Variables:
HWND					g_hwndCB; 		
HINSTANCE				g_hInst;

static SHACTIVATEINFO s_sai;

// Forward declarations of functions included in this code module:
LRESULT CALLBACK	MainDlgProc		(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
HWND				CreateRpCommandBar(HWND);


int WINAPI WinMain( HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPTSTR	 lpCmdLine,
				   int		 nCmdShow)
{
	g_hInst = hInstance;
	DialogBox(hInstance, (LPCTSTR)IDD_MAIN, NULL, (DLGPROC)MainDlgProc);
	
	return 0;
}

// Mesage handler for the IP Address Box
LRESULT CALLBACK MainDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	SHINITDLGINFO shidi;
	
	TCHAR		szUserEntry[MAX_ENTRYLEN];
	CHAR		cIPString[MAX_ENTRYLEN];
	CHAR		cHostName[MAX_ENTRYLEN];
	IPAddr		ipaddr;	 
	WSADATA		wsadata;
	HOSTENT*	pHostent				= NULL;
	IN_ADDR		pInetAddr;
	HANDLE		hIcmp;
	INT			iIndex;
	HWND		hSizeList;
	
	switch (message)
	{
	case WM_INITDIALOG:
		// Create a Done button and size it.  
		shidi.dwMask = SHIDIM_FLAGS;
		shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
		shidi.hDlg = hDlg;
		SHInitDialog(&shidi);

		g_hwndCB = CreateRpCommandBar(hDlg);

		// add entries to the size listbox 
		hSizeList = GetDlgItem(hDlg, IDC_PACKETSIZE);
		iIndex = SendMessage(hSizeList, CB_ADDSTRING, NULL, (LPARAM)(LPTSTR)TEXT("32"));
		SendMessage(hSizeList, CB_SETITEMDATA, (WPARAM)iIndex, (LPARAM)32);
		iIndex = SendMessage(hSizeList, CB_ADDSTRING, NULL, (LPARAM)(LPTSTR)TEXT("128"));
		SendMessage(hSizeList, CB_SETITEMDATA, (WPARAM)iIndex, (LPARAM)128);
		iIndex = SendMessage(hSizeList, CB_ADDSTRING, NULL, (LPARAM)(LPTSTR)TEXT("256"));
		SendMessage(hSizeList, CB_SETITEMDATA, (WPARAM)iIndex, (LPARAM)256);
		iIndex = SendMessage(hSizeList, CB_ADDSTRING, NULL, (LPARAM)(LPTSTR)TEXT("512"));
		SendMessage(hSizeList, CB_SETITEMDATA, (WPARAM)iIndex, (LPARAM)512);
		iIndex = SendMessage(hSizeList, CB_ADDSTRING, NULL, (LPARAM)(LPTSTR)TEXT("1024"));
		SendMessage(hSizeList, CB_SETITEMDATA, (WPARAM)iIndex, (LPARAM)1024);

		// set the default selection to 32
		SendMessage(hSizeList, CB_SETCURSEL, (WPARAM)0, NULL);

		SendDlgItemMessage(hDlg, IDC_IP_EDIT, EM_LIMITTEXT, MAX_ENTRYLEN, 0);

		return TRUE;
		
	case WM_COMMAND:
		if(LOWORD(wParam) == ID_PING)
		{
			// disable the ping button until we finish
			HWND	hPingButton = GetDlgItem(hDlg, ID_PING);
			EnableWindow(hPingButton, FALSE);

			// disable the size selection combo until we finish
			HWND hSizeSelect = GetDlgItem(hDlg, IDC_PACKETSIZE);
			EnableWindow(hSizeSelect, FALSE);
			
			// get what the user entered
			GetDlgItemText(hDlg, IDC_IP_EDIT, szUserEntry, MAX_ENTRYLEN);
			
			// if the user entered valid data, proceed
			if(szUserEntry && wcslen(szUserEntry))
			{
				// start winsock, requesting min version of 1.01
				WSAStartup(0x0101, &wsadata );
				
				//*******************************************************************
				// First we determine if the user entered an IP or a host name
				// if the user entered a host name, we convert it to an IP
				// before proceeding to ping that IP
				//*******************************************************************				
				
				// convert the user data to single byte chars
				wcstombs(cHostName, szUserEntry, MAX_ENTRYLEN);
				
				// if gethostbyname fails, we assume the user entered an IP or the entry is invalid
				// if it does not return null, then the user entered a valid host name
				if((pHostent = gethostbyname(cHostName)) == NULL)
				{
					// we assume the user entered an IP (or invalid data)
					strcpy(cIPString, cHostName);				
				}
				else
				{
					// determine the IP address returned in the HOSTENT structure
					char* pcDottedIP = NULL;
					
					// copy the IP as a IN_ADDR structure out of the HOSTENT structure
					memcpy((void*)&pInetAddr, (void*)pHostent->h_addr_list[0], pHostent->h_length);
						
					// convert the (Ipv4) Internet network address (IN_ADDR structure) 
					// into a string in Internet standard dotted format.
					pcDottedIP = inet_ntoa(pInetAddr);
					if(pcDottedIP)
					{
						strcpy(cIPString, pcDottedIP);				
					}
					
				}
				
				//*******************************************************************
				// Now we ping the IP address resolved above
				//*******************************************************************
				
				// convert the IP char string to a IPAddr representation
				ipaddr = inet_addr((const char*)cIPString);
				
				if(ipaddr == INADDR_NONE)
				{
					MessageBox(hDlg, TEXT("Invalid Host or IP Entered"), TEXT("Ping Status:"), 0);
				}
				else
				{
					INT		iPacketSize		= 0;
					
					hIcmp = IcmpCreateFile();
					
					if(hIcmp == INVALID_HANDLE_VALUE)
					{
						MessageBox(hDlg, TEXT("Could not create ICMP handle"), TEXT("Ping Error:"), 0);
					}
					else
					{
						LPVOID	lpData			= NULL;
						LPVOID	lpRevBuffer		= NULL;
						INT		iCurSel;

						// determine the size of the data packet to send
						iCurSel = SendDlgItemMessage(hDlg, IDC_PACKETSIZE, CB_GETCURSEL, NULL, NULL);
						iPacketSize = SendDlgItemMessage(hDlg, IDC_PACKETSIZE, CB_GETITEMDATA, (WPARAM)iCurSel, NULL);
						
						// allocate memory for the ping function call return and packet size
						lpData = LocalAlloc(LPTR, iPacketSize);
						lpRevBuffer = LocalAlloc(LPTR, sizeof(ICMP_ECHO_REPLY)*iPacketSize);
						
						if(lpData && lpRevBuffer)
						{
							// display the resolved IP address
							TCHAR				szIPAddressToDisplay[MAX_ENTRYLEN];
							TCHAR				szFormattedOutputString[MAX_ENTRYLEN];
							ICMP_ECHO_REPLY*	sIcmp; 
							long				lRtt;
							short				sDsz;
							HWND				hResultList					= NULL;

							// convert the IP address to a wide char array and diaplay in dialog
							mbstowcs(szIPAddressToDisplay, cIPString, MAX_ENTRYLEN);
							SetDlgItemText(hDlg, IDC_IPADDR, szIPAddressToDisplay);
							UpdateWindow(hDlg);
							
							// get result listbox handle and clear its contents
							hResultList = GetDlgItem(hDlg, IDC_REPLIES);
							SendMessage(hResultList, LB_RESETCONTENT, NULL, NULL);
							
							for(INT i = 0; i < NUMBEROFREPLIES; i++)
							{
								// send the ping
								if(IcmpSendEcho(hIcmp, ipaddr, lpData, iPacketSize, NULL, lpRevBuffer, (sizeof(ICMP_ECHO_REPLY)*iPacketSize), MSTOWAIT) == NULL)
								{
									_stprintf(szFormattedOutputString, TEXT("Ping Failed"));						
								}
								else
								{
									// get the ping information and extract the round trip time and data size
									sIcmp = (ICMP_ECHO_REPLY*)lpRevBuffer;
									lRtt = sIcmp->RoundTripTime;
									sDsz = sIcmp->DataSize; 					
									
									// format the string for output
									wsprintf(szFormattedOutputString, _T("RTT: %i ms, DataSize: %i bytes"), lRtt, sDsz); 								
								}
								// add item to the listbox for display
								SendMessage(hResultList, LB_ADDSTRING, NULL, (LPARAM)(LPTSTR)szFormattedOutputString);
								UpdateWindow(hDlg);
							}							
						}
						free(lpData);
						free(lpRevBuffer);						
						IcmpCloseHandle(hIcmp);
					}					
				}
				WSACleanup();
			}
			
			EnableWindow(hSizeSelect, TRUE);
			EnableWindow(hPingButton, TRUE);
		}
		if (LOWORD(wParam) == IDOK) 
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
	return FALSE;
}

HWND CreateRpCommandBar(HWND hwnd)
{
	SHMENUBARINFO mbi;
	
	memset(&mbi, 0, sizeof(SHMENUBARINFO));
	mbi.cbSize	   = sizeof(SHMENUBARINFO);
	mbi.hwndParent = hwnd;
	mbi.nToolBarId = NULL;
	mbi.hInstRes   = g_hInst;
	mbi.nBmpId	   = 0;
	mbi.cBmpImages = 0;
	mbi.dwFlags = SHCMBF_EMPTYBAR;
	
	if (!SHCreateMenuBar(&mbi)) 
		return NULL;
	
	return mbi.hwndMB;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -