vs2008.
1、安裝winpcap
官網下載地址http://www.winpcap.org/install/bin/WinPcap_4_1_1.exe
安裝so easy,不多說了。
2、下載WinPcap SDK
官方下載地址http://www.winpcap.org/install/bin/WpdPack_4_1_1.zip
下載後,解壓到工作目錄即可。
本機是加壓到了E盤根目錄,包含Include、Lib還有一些文檔和示例程序,
3、編程vs環境配置
打開Microsoft Visual Studio 2008,
【工具】->【選項】->【項目和解決方案】->【VC++目錄】
a>
平臺,選擇“Win32”
顯示以下內容的目錄,選擇“包含文件”
添加WpdPack目錄下的Include目錄到此
b>
平臺,選擇“Win32”
顯示以下內容的目錄,選擇“庫文件”
添加WpdPack目錄下的Lib目錄到此
4、針對每個項目,配置環境
新建項目,命名隨意,我這命名爲MYsample;
【項目】->【MYsample項目屬性】->【配置屬性】->【C/C++】->【預處理器】
在預處理器定義字段那,添加“;WPCAP;HAVE_REMOTE”。
ok!
- #include <pcap.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)
- {
- printf("Error in pcap_findalldevs_ex: %s/n", errbuf);
- exit(1);
- }
- /* Print the list */
- for (d = alldevs; d != NULL; d = d->next)
- {
- /* Print the device's name */
- printf("%d. %s", ++ i, d->name);
- /* Print the device's dscription */
- 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 0;
- }
- /* We don't need any more the device list. Free it */
- pcap_freealldevs(alldevs);
- char a;
- scanf(&a);
- return 1;
- }