2014-06-24 30 views
0

这里是测试代码,我试图了解与IcmpSendEcho2回调;我的回调不会触发。 我试图将icmp函数移动到其他线程(link),但没有得到任何结果。 我试图使用XP回调风格(在这里我并不惊讶也得不到Win7上)IcmpSendEcho2异步回调不起作用(C++,vs2010,win7)

#include <WinSock2.h> //includes Windows.h 
#include <Ws2tcpip.h> 

/** 
    struct from WinDDK Wdm.h (include Wdm.h, Ntddk.h, or Ntifs.h) 
*/ 
typedef struct _IO_STATUS_BLOCK { 
    union { 
    NTSTATUS Status; 
    PVOID Pointer; 
    }; 
    ULONG_PTR Information; 
} IO_STATUS_BLOCK, *PIO_STATUS_BLOCK; 

/** 
    typedef for PIO_APC_ROUTINE from MSDN 
*/ 
typedef 
VOID 
(NTAPI *PIO_APC_ROUTINE) (
    IN PVOID ApcContext, 
    IN PIO_STATUS_BLOCK IoStatusBlock, 
    IN ULONG Reserved 
    ); 
//defined 
#define PIO_APC_ROUTINE_DEFINED 

#include <iphlpapi.h> 
#include <icmpapi.h> 
#include <stdio.h> 

#include <string> 
using namespace std; 

#pragma comment(lib, "Ws2_32.lib") 
#pragma comment(lib, "iphlpapi.lib") 

bool icmpLoop = false; 

VOID NTAPI callbackVistaPlus(PVOID ApcContext,PIO_STATUS_BLOCK IoStatusBlock,ULONG Reserved) 
{ 
    MessageBoxA(0,"wow!","msg",MB_OK); 
} 

DWORD WINAPI IcmpThread(PVOID Parameter) 
{ 
    unsigned long _serverAddr = inet_addr("192.168.1.2"); //my linux machine 
    HANDLE _hIcmp = IcmpCreateFile(); 
    bool init = true; 
    if (_hIcmp == INVALID_HANDLE_VALUE) 
    { 
     printf("cant icmpcreatefile\n"); 
     return 0; 
    } 
    //for test i create large buffer. it's testing code, not real 
    char * _replysBuffer = (char*)malloc(sizeof(ICMP_ECHO_REPLY)*1000+8); 
    if (!_replysBuffer) 
    { 
     printf("cant buffer\n"); 
     return 0; 
    } 
    char * data = "hi all"; //my testing data to see with tcpdump at linux machine 
// bool loop = true; 
    DWORD ret = IcmpSendEcho2(
      _hIcmp, 
      0, 
      callbackVistaPlus, 
      0, 
      _serverAddr, 
      data, 
      strlen(data), 
      NULL, 
      _replysBuffer, 
      sizeof(ICMP_ECHO_REPLY)*1000+8, 
      1000); 
    /**/ 
    printf("ping sended\n"); 
    getchar(); 
// while (icmpLoop){Sleep(100);} 
    printf("exit\n"); 
    IcmpCloseHandle(_hIcmp); 
} 

int main(int argc, char* argv[]) 
{ 
    icmpLoop = true; 
// HANDLE hThread = CreateThread(NULL,0,IcmpThread,NULL,0,NULL); 
    IcmpThread(0); 
// getchar(); 
// icmpLoop = false; 
    getchar(); 
    return 0; 
} 

我完全肯定正确现有replyBuffer,而IcmpCloseHandle没有关闭之前,我得到答复(循环线程风格,getchar()“暂停”为非线程)

我在linux机器(我用它发送回声)检查tcpdump,我看到传入和传出的ICMP数据包与我的数据 - 它的工作! 我调试编译后的代码,看到replyBuffer后我的数据更改IcmpSendEcho2 .. 因此.. ICMP确实有效,但不是回调。

我不想要移动到活动式的,因为它会产生问题,我的代码

我也很惊讶了解下情况: MSDN:

When called asynchronously, the IcmpSendEcho2 function 
returns ERROR_IO_PENDING to indicate the operation is in progress. 

IcmpSendEcho2返回0,GetLastError()返回ERROR_IO_PENDING 可以吗?

所以,如果任何人都可以帮助我 - 这将是非常棒的。

回答