c++ - 无法在 Visual Studio 中编译 PCAP 程序

标签 c++ visual-studio-2010 pcap winpcap

以下是从我的 AirPcap 适配器检索 MAC 地址的代码。但是我在执行程序时遇到了问题:请帮我解决这个错误。

#include <stdio.h>
#include <conio.h>
#include "packet32.h"
#include <ntddndis.h>
#include "StdAfx.h"
#define Max_Num_Adapter 10
char        AdapterList[Max_Num_Adapter][1024];

int main()
{
    LPADAPTER   lpAdapter = 0;
    int         i;
    DWORD       dwErrorCode;
    WCHAR       AdapterName[8192];
    WCHAR       *temp,*temp1;
    int         AdapterNum=0,Open;
    ULONG       AdapterLength;
    PPACKET_OID_DATA  OidData;
    BOOLEAN     Status;

    //
    // Obtain the name of the adapters installed on this machine
    //

    printf("Packet.dll test application. Library version:%s\n", PacketGetVersion());

    printf("Adapters installed:\n");
    i=0;    

    AdapterLength = sizeof(AdapterName);

    if(PacketGetAdapterNames(AdapterName,&AdapterLength)==FALSE){
        printf("Unable to retrieve the list of the adapters!\n");
        return -1;
    }
    temp=AdapterName;
    temp1=AdapterName;

    while ((*temp!='\0')||(*(temp-1)!='\0'))
    {
        if (*temp=='\0') 
        {
            memcpy(AdapterList[i],temp1,temp-temp1);
            temp1=temp+1;
            i++;
        }
        temp++;
    }

    AdapterNum=i;
    for (i=0;i<AdapterNum;i++)
        printf("\n%d- %s\n",i+1,AdapterList[i]);
    printf("\n");


    do 
    {
        printf("Select the number of the adapter to open : ");
        scanf_s("%d",&Open);
        if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); 
    } while (Open>AdapterNum);


    //
    // Open the selected adapter
    //

    lpAdapter =   PacketOpenAdapter(AdapterList[Open-1]);

    if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE))
    {
        dwErrorCode=GetLastError();
        printf("Unable to open the adapter, Error Code : %lx\n",dwErrorCode); 

        return -1;
    }   

    // 
    // Allocate a buffer to get the MAC adress
    //

    OidData = (PPACKET_OID_DATA)malloc(6 + sizeof(PACKET_OID_DATA));
    if (OidData == NULL) 
    {
        printf("error allocating memory!\n");
        PacketCloseAdapter(lpAdapter);
        return -1;
    }

    // 
    // Retrieve the adapter MAC querying the NIC driver
    //

    OidData->Oid = OID_802_3_CURRENT_ADDRESS;

    OidData->Length = 6;
    ZeroMemory(OidData->Data, 6);

    Status = PacketRequest(lpAdapter, FALSE, OidData);
    if(Status)
    {
        printf("The MAC address of the adapter is %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
            (OidData->Data)[0],
            (OidData->Data)[1],
            (OidData->Data)[2],
            (OidData->Data)[3],
            (OidData->Data)[4],
            (OidData->Data)[5]);
    }
    else
    {
        printf("error retrieving the MAC address of the adapter!\n");
    }

    free(OidData);
    PacketCloseAdapter(lpAdapter);
    return (0);
}

错误如下:

1>------ Build started: Project: qqq, Configuration: Release Win32 ------
1>Build started 5/29/2015 3:10:00 PM.
1>InitializeBuildStatus:
1>  Touching "Release\qqq.unsuccessfulbuild".
1>ClCompile:
1>  All outputs are up-to-date.
1>  ppp.cpp
1>ppp.cpp(35): warning C4627: '#include <conio.h>': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>ppp.cpp(36): warning C4627: '#include "packet32.h"': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>ppp.cpp(37): warning C4627: '#include <ntddndis.h>': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>ppp.cpp(44): error C2065: 'LPADAPTER' : undeclared identifier
1>ppp.cpp(44): error C2146: syntax error : missing ';' before identifier 'lpAdapter'
1>ppp.cpp(44): error C2065: 'lpAdapter' : undeclared identifier
1>ppp.cpp(46): error C2065: 'DWORD' : undeclared identifier
1>ppp.cpp(46): error C2146: syntax error : missing ';' before identifier 'dwErrorCode'
1>ppp.cpp(46): error C2065: 'dwErrorCode' : undeclared identifier
1>ppp.cpp(47): error C2065: 'WCHAR' : undeclared identifier
1>ppp.cpp(47): error C2146: syntax error : missing ';' before identifier 'AdapterName'
1>ppp.cpp(47): error C2065: 'AdapterName' : undeclared identifier
1>ppp.cpp(48): error C2065: 'WCHAR' : undeclared identifier
1>ppp.cpp(48): error C2065: 'temp' : undeclared identifier
1>ppp.cpp(48): error C2065: 'temp1' : undeclared identifier
1>ppp.cpp(50): error C2065: 'ULONG' : undeclared identifier
1>ppp.cpp(50): error C2146: syntax error : missing ';' before identifier 'AdapterLength'
1>ppp.cpp(50): error C2065: 'AdapterLength' : undeclared identifier
1>ppp.cpp(51): error C2065: 'PPACKET_OID_DATA' : undeclared identifier
1>ppp.cpp(51): error C2146: syntax error : missing ';' before identifier 'OidData'
1>ppp.cpp(51): error C2065: 'OidData' : undeclared identifier
1>ppp.cpp(52): error C2065: 'BOOLEAN' : undeclared identifier
1>ppp.cpp(52): error C2146: syntax error : missing ';' before identifier 'Status'
1>ppp.cpp(52): error C2065: 'Status' : undeclared identifier
1>ppp.cpp(58): error C3861: 'PacketGetVersion': identifier not found
1>ppp.cpp(63): error C2065: 'AdapterLength' : undeclared identifier
1>ppp.cpp(63): error C2065: 'AdapterName' : undeclared identifier
1>ppp.cpp(63): error C2070: ''unknown-type'': illegal sizeof operand
1>ppp.cpp(65): error C2065: 'AdapterName' : undeclared identifier
1>ppp.cpp(65): error C2065: 'AdapterLength' : undeclared identifier
1>ppp.cpp(65): error C2065: 'FALSE' : undeclared identifier
1>ppp.cpp(65): error C3861: 'PacketGetAdapterNames': identifier not found
1>ppp.cpp(69): error C2065: 'temp' : undeclared identifier
1>ppp.cpp(69): error C2065: 'AdapterName' : undeclared identifier
1>ppp.cpp(70): error C2065: 'temp1' : undeclared identifier
1>ppp.cpp(70): error C2065: 'AdapterName' : undeclared identifier
1>ppp.cpp(72): error C2065: 'temp' : undeclared identifier
1>ppp.cpp(72): error C2065: 'temp' : undeclared identifier
1>ppp.cpp(72): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>  qqq.cpp
1>qqq.cpp(36): warning C4627: '#include <conio.h>': skipped when looking for precompiled header use
1>          Add directive to 'StdAfx.h' or rebuild precompiled header
1>qqq.cpp(155): error C3861: '_kbhit': identifier not found
1>qqq.cpp(197): error C2440: '=' : cannot convert from 'PVOID' to 'char *'
1>          Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>qqq.cpp(202): error C3861: '_kbhit': identifier not found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.33
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我面临未声明的标识符问题。请指导我解决这个问题。

最佳答案

stdafx.h 需要包含在所有其他 header 之前首先。这是 VC++ 编译器的要求。如果 stdafx.h 包含在其他头文件之后,编译器将跳过这些头文件。发生这种情况时,它无法解析在这些 header 中声明的符号。

关于c++ - 无法在 Visual Studio 中编译 PCAP 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30537468/

相关文章:

c++ - std::min 的实现

C++,directx 12 : Color Picking Questions

c - mulcross数据生成器运行错误: free():invalid next size

c# - 将列表绑定(bind)到 gridview C#

c++ - 如何使用 C++ 在没有 readline 的情况下显示字符串中的多个单词?

c++ - Visual C++ 资源文件符号

c# - 查找未关闭的FileStream

c++ - 如何在 C++ 中分析流量转储文件中的数据包信息?

c - 结构复制到字节数组中..对齐错误?

csv - 将 pcap 数据导出到 csv : timestamp, 字节,上行/下行,额外信息