#include "MojPing.h"

void PingClass::SprawdzPing(CEdit & edit,char * addr)
{
	unsigned long ipaddr = INADDR_NONE;
	HANDLE hIcmp = IcmpCreateFile();

    if (hIcmp == INVALID_HANDLE_VALUE)
	{
		return ;
    }   
	char request[32] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
	int replySize = sizeof(ICMP_ECHO_REPLY) + sizeof(request);
	LPVOID reply = (LPVOID)malloc(replySize);

	int ret = IcmpSendEcho(hIcmp,inet_addr(addr),request,sizeof(request),NULL,reply,replySize,1000);
	if(ret != 0)
	{
		PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)reply;
		in_addr replyAddr;
        replyAddr.s_addr = pEchoReply->Address;
        char * szAddr = inet_ntoa( replyAddr );
		wchar_t  szAddrWide[16] = {'\0'};
		mbstowcs(szAddrWide,szAddr,16);
		CString strReq;
        edit.GetWindowTextW(strReq);
        strReq.AppendFormat(L"Odebrano od %s; Ilość bajtów %ld; Czas %ld ms;\r\n",szAddrWide,pEchoReply->DataSize, pEchoReply->RoundTripTime);
		edit.SetWindowTextW(strReq);
	}

	IcmpCloseHandle(hIcmp);
}