无法在Visual Studio中编译PCAP程序

时间:2015-05-29 19:18:31

标签: 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 ==========

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

1 个答案:

答案 0 :(得分:0)

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