0

I have a need to use the IcmpSendEcho2 API command asynchronously using the ApcRoutine callback routine.

A question I have is what would the signature look like for the ApcRoutine callback routine I need to define?

When I call IcmpSendEcho2 what would the third parameter look like?

I have some 15 proxy request to be sent. should I request with IcmpSendEcho2 only once or multiple times.

If I will need to send many IcmpSendEcho2 requests at one time. How will the callback ApcRoutine know which IcmpSendEcho2 call is done. I guess this is where the ApcContext parameter comes into play?

I can't find any example code on MSDN or elsewhere that demonstrates how to use the IcmpSendEcho2 command asynchronously.

Kate
  • 1
  • 2

1 Answers1

2
int ReplyCame(PVOID param)
{
 char* szAddr = (char*) param;

 printf("Replay Came for %s......\n", szAddr);

 return 0;
}

char* szAddr1 = "172.18.1.1";
char* szAddr2 = "172.18.1.4";

int _tmain(int argc, _TCHAR* argv[])
{
 char *SendData = "Data Buffer";
 LPVOID ReplyBuffer;

 HANDLE IcmpHandle = IcmpCreateFile();

 IPAddr addr1 = inet_addr(szAddr1);
 IPAddr addr2 = inet_addr(szAddr2);

 ReplyBuffer = (VOID*) malloc(sizeof(ICMP_ECHO_REPLY) + sizeof(SendData));

 IcmpSendEcho2(IcmpHandle, NULL, (FARPROC)ReplyCame, szAddr1, addr1, 
SendData, sizeof(SendData), NULL, ReplyBuffer, 8*sizeof(ReplyBuffer) + 
sizeof(ICMP_ECHO_REPLY), 1000);
 IcmpSendEcho2(IcmpHandle, NULL, (FARPROC)ReplyCame, szAddr2, addr2, 
SendData, sizeof(SendData), NULL, ReplyBuffer, 8*sizeof(ReplyBuffer) + 
sizeof(ICMP_ECHO_REPLY), 1000);

 SleepEx(5000, TRUE);

 return 0;
}

Do notice that if you want to use the replyBuffer you need to parse it before with IcmpParseReplies.

GalDude33
  • 7,071
  • 1
  • 28
  • 38
  • 1
    (1) sizeof(SendData) is 4, should be strlen(SendData); sizeof(ReplyBuffer) is 4, should be the actual allocated length. (2) ReplyCame should be **void** return. (3) This assumes XP. On Vista, the signature is **void ReplyCame(PVOID context, PIO_STATUS_BLOCK pio, DWORD reserved);** – Jesse Chisholm Jan 12 '14 at 17:17
  • The docs say that asynchronous calls should be done from an "alertable thread" but fails to mention that this is _NOT_ the main thread. Experience shows that hEvent style works better than callback style. – Jesse Chisholm Jan 12 '14 at 17:26