因網(wǎng)絡(luò)課程的作業(yè)需要用到winpcap,不得已又要和C++打交道了。由于winpcap是為vc而設(shè)計(jì)的,但是我又不大喜歡用盜版,于是打算用免費(fèi)的 VC++ 2005 Express版。
1、安裝 Visual Studio 2005 Express Edition 和 Paltform SDK。
如何安裝Visual Studio 2005 Express在這里就不贅述了,很簡(jiǎn)單的。由于VC Express沒(méi)有自帶 Platform SDK,所以需要自己下載安裝(如果不安裝 psdk的話,就會(huì)出現(xiàn) 找不到 winsock2.h 的編譯錯(cuò)誤)。由于微軟現(xiàn)在官網(wǎng)提供的psdk下載比較麻煩,需要windows正版驗(yàn)證,再加上體積比較大,所以我這里就不用,我用的psdk是在這里下載的:
XPSP2 PSDK Full Download with Local Install還有一個(gè),不知道能不能安裝在xp上,有興趣的兄弟可以自己試試
Windows Server 2003 PSDK Full Download with Local Install似乎這兩個(gè)鏈接在官網(wǎng)上是找不到的
下載、解壓、安裝,然后再配置 VC++:
tools --> options? --> Projects and Solutions? --> VC++ Directories?? : 把以下路徑添加到相應(yīng)的下拉節(jié)點(diǎn)中去:(其中psdk是你的sdk安裝目錄)
Executalbe files :psdkdir\Bin
Include files :psdkdir\include
Library files:psdkdir\lib
2、安裝 winpcap:到這里下載
winpcap安裝后按要求重啟,如果沒(méi)安裝這個(gè)包,程序即使編譯成功也不能運(yùn)行,會(huì)提示找不到 winpcap.dll
3、下載?
WinPcap Developer's
Packs解壓后會(huì)得一個(gè)目錄WpdPack四個(gè)子目錄:
?docs
?Examples-pcap
?Examples-remote
?Include
?Lib
然后配置VC++
tools --> options? --> Projects and Solutions? --> VC++ Directories :
其中 WpdPackPath是目錄WpdPack的絕對(duì)路徑
4、新建一個(gè) win32->win32 console application 工程,然后配置工程屬性:
- 右鍵 -> Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Proprocessor Definition?? 往上面添加 WPCAP就可以了(這一步不做似乎也沒(méi)什么問(wèn)題~)
- 右鍵 -> Properties -> Configuration Properties -> Linker -> input -> Additional Dependencies? 往上面添加 wpcap.lib Packet.lib
5、一個(gè)例子:


#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" 也是會(huì)報(bào)錯(cuò)的~
6、鏈接錯(cuò)誤:
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
解決該問(wèn)題,需要只需把ws2_32.lib添加到wpcap.lib Packet.lib后面(見(jiàn)上面第4條)