packet里有一控制台例程TESTAPP
里面有一小段:
while(!kbhit())
{
    // capture the packets
if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)==FALSE)
{
printf("Error: PacketReceivePacket failed");
return (-1);
} PrintPackets(lpPacket);
}
请将其修改成windows下的,即用什么代替khhit()

解决方案 »

  1.   

    设置一个全局变量吧int bwhile=0;while(!bwhile)
    {
    }停止的时候将bwhile=1
      

  2.   

    int  WINAPI GetPacketThread(LPVOID param)
    {
    ///////////////////////////////////
    //define a pointer to an ADAPTER structure
    LPADAPTER  lpAdapter = 0;
    //define a pointer to a PACKET structure
    LPPACKET   lpPacket;
    int        i;
    DWORD      dwErrorCode;
    int AdapterNum=0;
    ULONG AdapterLength;
    char buffer[8192];  // buffer to hold the data coming from the driver
    // char buffer[256000];  // buffer to hold the data coming from the driver// struct bpf_stat stat;

    // obtain the name of the adapters installed on this machine
    AdapterLength=4096;
    i=0;
    // the data returned by PacketGetAdapterNames is different in Win95 and in WinNT.
    // We have to check the os on which we are running
    //这里对网卡进行选择
    char Adapter[MAX_PATH];
    memset(Adapter, 0, MAX_PATH);
    WideCharToMultiByte(CP_ACP, 0, (LPWSTR)AdapterList[g_AdapterNum], wcslen((LPWSTR)AdapterList[g_AdapterNum])+1, Adapter, MAX_PATH, NULL, NULL); lpAdapter = (LPADAPTER) PacketOpenAdapter(Adapter);
    // lpAdapter = (LPADAPTER) PacketOpenAdapter((LPTSTR) AdapterList[0]);
    // lpAdapter = (LPADAPTER) PacketOpenAdapter((LPTSTR) AdapterList[g_AdapterNum]);
    if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
    {
    dwErrorCode=GetLastError();
    AfxMessageBox("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); 
    return -1;
    } // set the network adapter in promiscuous mode

    if(PacketSetHwFilter(lpAdapter,NDIS_PACKET_TYPE_PROMISCUOUS)==FALSE){
    AfxMessageBox("Warning: unable to set promiscuous mode!\n");
    } // set a 512K buffer in the driver
    if(PacketSetBuff(lpAdapter,512000)==FALSE){
    AfxMessageBox("Unable to set the kernel buffer!\n");
    return -1;
    } // set a 1 second read timeout
    if(PacketSetReadTimeout(lpAdapter,1000)==FALSE){
    AfxMessageBox("Warning: unable to set the read tiemout!\n");
    } //allocate and initialize a packet structure that will be used to
    //receive the packets.
    if((lpPacket = PacketAllocatePacket())==NULL){
    AfxMessageBox("\nError: failed to allocate the LPPACKET structure.");
    return (-1);
    }
    PacketInitPacket(lpPacket,(char*)buffer,8192);
    //main capture loop
    while(1)//this you change it to 1
    {
        // capture the packets
    if(PacketReceivePacket(lpAdapter,lpPacket,TRUE)==FALSE){
    AfxMessageBox("Error: PacketReceivePacket failed");
    return (-1);
    }
    AnalysePackage(lpPacket,lpAdapter);//this is your analyse packet thread 
        }
    PacketFreePacket(lpPacket);
    PacketCloseAdapter(lpAdapter);
    return 0;
    }
      

  3.   

    kingzai:
    如何用消息令此线程退出
      

  4.   

    PostThreadMessage,
    or get thread handle ,then ExitThread
      

  5.   

    不用,创建工作线程的时候可以得到线程ID,可以直接Kill掉的。
      

  6.   

    CreateEvent SetEvent WaitForSinleObject...
      

  7.   

    按你的例程,在函数analypackage()里只放了return;结果老出现非法操作;
      

  8.   

    you should init adapter configuration  first,your email, i post my sniffer example to you.
      

  9.   

    [email protected]
    thanks a lot
    and if i can not debug it,I will post it to you
    can i?
      

  10.   

    [email protected]   OR  [email protected]
      

  11.   

    very sorry ,i can't find my former code because i write it one year ago.