问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/16352158/how-to-implement-icmpsendecho2-asynchronous-with-callback-example-vc