2014-06-24 29 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 可以嗎?

所以,如果任何人都可以幫助我 - 這將是非常棒的。

回答