WinPcap環境配置【1】

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還有一些文檔和示例程序, 
image

3、編程vs環境配置

打開Microsoft Visual Studio 2008, 
【工具】->【選項】->【項目和解決方案】->【VC++目錄】 
image 
a> 
平臺,選擇“Win32” 
顯示以下內容的目錄,選擇“包含文件” 
添加WpdPack目錄下的Include目錄到此 
image  
b> 
平臺,選擇“Win32” 
顯示以下內容的目錄,選擇“庫文件” 
添加WpdPack目錄下的Lib目錄到此 
image

4、針對每個項目,配置環境

新建項目,命名隨意,我這命名爲MYsample; 
【項目】->【MYsample項目屬性】->【配置屬性】->【C/C++】->【預處理器】 
image
在預處理器定義字段那,添加“;WPCAP;HAVE_REMOTE”。

ok!

運行示例: 
image 
程序代碼:

  1. #include <pcap.h>  
  2.   
  3. int main() {  
  4.     pcap_if_t *alldevs;  
  5.     pcap_if_t *d;  
  6.     int i = 0;  
  7.     char errbuf[PCAP_ERRBUF_SIZE];  
  8.   
  9.     /* Retrieve the device list from the local machine*/  
  10.     if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)  
  11.     {  
  12.         printf("Error in pcap_findalldevs_ex: %s/n", errbuf);  
  13.         exit(1);  
  14.     }  
  15.   
  16.     /* Print the list */  
  17.     for (d = alldevs; d != NULL; d = d->next)  
  18.     {  
  19.         /* Print the device's name */  
  20.         printf("%d. %s", ++ i, d->name);  
  21.   
  22.         /* Print the device's dscription */  
  23.         if (d->description)  
  24.         {  
  25.             printf("(%s)/n", d->description);  
  26.         }  
  27.         else  
  28.         {  
  29.              printf("(No description available)/n");  
  30.         }  
  31.     }  
  32.   
  33.     if (i == 0)  
  34.     {  
  35.     printf("/nNo interfaces found! Make sure WinPcap is installed./n");  
  36.     return 0;  
  37.     }  
  38.   
  39.     /* We don't need any more the device list. Free it */  
  40.     pcap_freealldevs(alldevs);  
  41.   
  42.     char a;  
  43.     scanf(&a);  
  44.     return 1;  
  45. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章