因网络课程的作业需要用到winpcap,不得已又要和C++打交道了。由于winpcap是为vc而设计的,但是我又不大喜欢用盗版,于是打算用免费的 VC++ 2005 Express版。
1、安装 Visual Studio 2005 Express Edition 和 Paltform SDK。
如何安装Visual Studio 2005 Express在这里就不赘述了,很简单的。由于VC Express没有自带 Platform SDK,所以需要自己下载安装(如果不安装 psdk的话,就会出现 找不到 winsock2.h 的编译错误)。由于微软现在官网提供的psdk下载比较麻烦,需要windows正版验证,再加上体积比较大,所以我这里就不用,我用的psdk是在这里下载的:
XPSP2 PSDK Full Download with Local Install还有一个,不知道能不能安装在xp上,有兴趣的兄弟可以自己试试
Windows Server 2003 PSDK Full Download with Local Install似乎这两个链接在官网上是找不到的
下载、解压、安装,然后再配置 VC++:
tools --> options --> Projects and Solutions --> VC++ Directories : 把以下路径添加到相应的下拉节点中去:(其中psdk是你的sdk安装目录)
Executalbe files :psdkdir\Bin
Include files :psdkdir\include
Library files:psdkdir\lib
2、安装 winpcap:到这里下载
winpcap安装后按要求重启,如果没安装这个包,程序即使编译成功也不能运行,会提示找不到 winpcap.dll
3、下载
WinPcap Developer's
Packs解压后会得一个目录WpdPack四个子目录:
docs
Examples-pcap
Examples-remote
Include
Lib
然后配置VC++
tools --> options --> Projects and Solutions --> VC++ Directories :
其中 WpdPackPath是目录WpdPack的绝对路径
4、新建一个 win32->win32 console application 工程,然后配置工程属性:
- 右键 -> Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Proprocessor Definition 往上面添加 WPCAP就可以了(这一步不做似乎也没什么问题~)
- 右键 -> Properties -> Configuration Properties -> Linker -> input -> Additional Dependencies 往上面添加 wpcap.lib Packet.lib
5、一个例子:
#include "pcap.h"
#include "remote-ext.h"
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the device list from the local machine */
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
exit(1);
}
/* Print the list */
for(d= alldevs; d != NULL; d= d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
printf(" (No description available)\n");
}
if (i == 0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return 1;
}
/* We don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
return 0;
}
注意,如果不添加 #include "remote-ext.h" 也是会报错的~
6、链接错误:
anothertest.obj : error LNK2019: unresolved external symbol __imp__WSASetLastError@4 referenced in function _WspiapiGetNameInfo@28
anothertest.obj : error LNK2019: unresolved external symbol __imp__inet_ntoa@4 referenced in function _WspiapiLegacyGetAddrInfo@16
anothertest.obj : error LNK2019: unresolved external symbol __imp__htonl@4 referenced in function _WspiapiLegacyGetAddrInfo@16
anothertest.obj : error LNK2019: unresolved external symbol __imp__getservbyname@8 referenced in function _WspiapiLegacyGetAddrInfo@16
anothertest.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function _WspiapiLegacyGetAddrInfo@16
anothertest.obj : error LNK2019: unresolved external symbol __imp__inet_addr@4 referenced in function _WspiapiParseV4Address@8
anothertest.obj : error LNK2019: unresolved external symbol __imp__WSAGetLastError@0 referenced in function _WspiapiQueryDNS@24
anothertest.obj : error LNK2019: unresolved external symbol __imp__gethostbyname@4 referenced in function _WspiapiQueryDNS@24
anothertest.obj : error LNK2019: unresolved external symbol __imp__gethostbyaddr@12 referenced in function _WspiapiLegacyGetNameInfo@28
anothertest.obj : error LNK2019: unresolved external symbol __imp__getservbyport@8 referenced in function _WspiapiLegacyGetNameInfo@28
anothertest.obj : error LNK2019: unresolved external symbol __imp__ntohs@4 referenced in function _WspiapiLegacyGetNameInfo@28
解决该问题,需要只需把ws2_32.lib添加到wpcap.lib Packet.lib后面(见上面第4条)