Ping & Milliseconds

I've been trying to replicate the ping function in cmd. It does not however provide the time in milliseconds. So in short how can I get the milliseconds of a ping using this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void main ()
{
	HANDLE hIcmp;
	char *SendData = "789789746546512315648977894564565464789545";
	LPVOID ReplyBuffer;
	DWORD dwRetVal;
	DWORD buflen;
	PICMP_ECHO_REPLY pIcmpEchoReply;
	hIcmp = IcmpCreateFile();
	buflen = sizeof(ICMP_ECHO_REPLY) + strlen(SendData) + 1;
	ReplyBuffer = (VOID*) malloc(buflen);
	if (ReplyBuffer == NULL)
	{
		//return 1;
	}
		memset(ReplyBuffer, 0, buflen);
		pIcmpEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
		dwRetVal = IcmpSendEcho(hIcmp,inet_addr("www.google.com"),SendData, strlen(SendData),NULL, ReplyBuffer,buflen,1000);
	if (dwRetVal != 0)
	{
		printf("Received %ld messages.\n", dwRetVal);
		printf("\n");
		printf("RTT: %d\n", pIcmpEchoReply->RoundTripTime);
		printf("Data Size: %d\n", pIcmpEchoReply->DataSize);
		printf("Message: %s\n", pIcmpEchoReply->Data);
	} 
	else 
	{
		printf("Call to IcmpSendEcho() failed.\n");
		printf("Error: %ld\n", GetLastError());
	}
	IcmpCloseHandle(hIcmp);
	system("pause");
}
Use the "QueryPerformanceCounter()" function: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx to grab one timestamp before you make the call and another immediatly afterward then calculate the difference. Do this a few times and average out the difference for a meaningful answer.
> It does not however provide the time in milliseconds.

Microsoft says that it does.
RoundTripTime
Type: ULONG
The round trip time, in milliseconds.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366053(v=vs.85).aspx
So does anyone have an example in the form of code, this would be a life saver.
Sure: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366050(v=vs.85).aspx

Use ctrl+F and search for the string "Examples". Sorry, MSDN doesn't use HTML anchors much for some reason. The 'example' that you want is between Lines 47 and 66 within that code.
Last edited on
@ OP: I am only reluctant to answer the question you PM'd me because, although "QueryPerformanceCounter()" is a valid answer it is also the wrong answer. This may seem confusing at first but it's a common occurrence in any Turing complete language just because something can be done a certain way, doesn't mean that it should. JLBorges is right, you already have the data that you want stored in 'ReplyBuffer', just use that. If you're getting an error then post it here and we can help you with it.
Last edited on
Ok I'll try to proceed from here.
Well I tried to implement it in the following code. I am going to assume that it is incorrect and my understanding seems to be the limiting point.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int main()
{
	HANDLE hIcmp;
	char *SendData = "7897897564651321546894564654651";
	LPVOID ReplyBuffer;
	DWORD dwRetVal;
	DWORD buflen;
	PICMP_ECHO_REPLY pIcmpEchoReply;
	hIcmp = IcmpCreateFile();
	buflen = sizeof(ICMP_ECHO_REPLY) + strlen(SendData) + 1;
	ReplyBuffer = (VOID*) malloc(buflen);
	if (ReplyBuffer == NULL) 
	{
		return 1;
	}
	memset(ReplyBuffer, 0, buflen);
	pIcmpEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
	
	//dwRetVal = IcmpSendEcho(hIcmp,inet_addr("suzhoupingjiangfu.com"), SendData, strlen(SendData), NULL, ReplyBuffer, buflen,1000);
	LARGE_INTEGER t1 = { 0 };
	LARGE_INTEGER t2 = { 0 };
	//QueryPerformanceCounter(&t1);
	dwRetVal = IcmpSendEcho(hIcmp,inet_addr("suzhoupingjiangfu.com"), SendData, strlen(SendData), NULL, ReplyBuffer, buflen,1000);
	QueryPerformanceCounter(&t1);
	//t2 = t1;
	cout << t1.QuadPart << endl;
	//if (dwRetVal != 0) 
	//{
		printf("Received %ld messages.\n", dwRetVal);
		printf("\n");
		printf("RTT: %d\n", pIcmpEchoReply->RoundTripTime);
		printf("Data Size: %d\n", pIcmpEchoReply->DataSize);
		printf("Message: %s\n", pIcmpEchoReply->Data);
	//} 
	/*
	else 
	{
		printf("Call to IcmpSendEcho() failed.\n");
		printf("Error: %ld\n", GetLastError());
	}
	*/
	cout << t1.QuadPart << endl;
	IcmpCloseHandle(hIcmp);
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.