Code:
bool ping(char *destination, HWND hTest)
{
char currentText[MAX_KEY_LENGTH];
char szBuffer[MAX_KEY_LENGTH];
WSADATA wsaData;
WSAStartup(0x0202, &wsaData);
HANDLE hIcmpFile = IcmpCreateFile();
hostent* remoteHost;
remoteHost = gethostbyname(destination);
//Error Checking
if (WSAGetLastError() != 0)
{
_stprintf_s(szBuffer,"The ping request was unsuccessful! \n", "");
GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
strcat_s(currentText, szBuffer);
SetWindowText(hTest, currentText);
WSACleanup();
return false;
}
if (hIcmpFile==INVALID_HANDLE_VALUE)
{
_stprintf_s(szBuffer,"IcmpCreateFile failed! %s", "");
GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
strcat_s(currentText, szBuffer);
SetWindowText(hTest, currentText);
WSACleanup();
return false;
}
IPAddr* ipaddr = reinterpret_cast< IPAddr* > (remoteHost->h_addr_list[0]);
_stprintf_s(szBuffer, "Pinging %s [ %s ] \n\n", destination, inet_ntoa(*reinterpret_cast< in_addr* >(ipaddr)));
GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
strcat_s(currentText, szBuffer);
SetWindowText(hTest, currentText);
LPVOID ReplyBuffer = (VOID*) malloc(sizeof(ICMP_ECHO_REPLY));
for (int i=0; i<3; i++)
{
if (IcmpSendEcho(hIcmpFile, *ipaddr, 0,0,NULL, ReplyBuffer,sizeof(ICMP_ECHO_REPLY),1000)==0)
{
_stprintf_s(szBuffer,"Ping Request has failed: %s", "Can't ping request");
GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
strcat_s(currentText, szBuffer);
SetWindowText(hTest, currentText);
return false;
}
else
{
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
in_addr ipreplied;
ipreplied.S_un.S_addr=pEchoReply->Address;
_stprintf_s(szBuffer,"Reply from %s \tbytes= %i \ttime= %i ms\n\n", inet_ntoa(ipreplied), pEchoReply->DataSize, pEchoReply->RoundTripTime);
GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
strcat_s(currentText, szBuffer);
SetWindowText(hTest, currentText);
return true;
}
Sleep(500);
}
free(ReplyBuffer);
IcmpCloseHandle(hIcmpFile);
WSACleanup();
}
void test()
{
HWND hTest;
char currentText[MAX_KEY_LENGTH];
char szBuffer[MAX_KEY_LENGTH];
pingScreen = CreateWindowEx(0, TEXT("STATIC"), TEXT(""), WS_CHILD | WS_VISIBLE | SS_LEFT, 200, 200, 600, 400, hWnd, NULL, hInst, NULL);
//Do a ping Check to confirm you are connected to the internet
if(ping("www.google.com", pingScreen)== true)
{
MessageBox(NULL, TEXT("First Ping works"), TEXT("Success"), MB_OKCANCEL);
if(ping("www.facebook.com", pingScreen)== true)
{
MessageBox(NULL, TEXT("Second Ping works"), TEXT("Success"), MB_OKCANCEL);
if(ping("www.yahoo.com", pingScreen)== true)
{
MessageBox(NULL, TEXT("Third Ping works"), TEXT("Success"), MB_OKCANCEL);
}
else if(ping("yahoo.com", pingScreen)== false)
{
//MessageBox(NULL, TEXT("Third Ping failed"), TEXT("Error"), MB_OKCANCEL);
_stprintf_s(szBuffer,"Third Ping failed %s", "");
GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
strcat_s(currentText, szBuffer);
SetWindowText(hTest, currentText);
}
}
else if(ping("www.facebook.com", pingScreen)== false)
{
_stprintf_s(szBuffer,"Second Ping failed %s", "");
GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
strcat_s(currentText, szBuffer);
SetWindowText(hTest, currentText);
}
}
else if(ping("www.google.com", pingScreen)== false)
{
_stprintf_s(szBuffer,"First Ping failed %s", "");
GetWindowText(hTest, currentText, MAX_KEY_LENGTH);
strcat_s(currentText, szBuffer);
SetWindowText(hTest, currentText);
}
CenterWindow(pingScreen, hWnd, 500, 300);
}