所有使用 WinPcap API 的原始檔開頭:
#include
專案 → Settings → C/C++ → General → Preprocessor definitions 加入:
WPCAP
啟用 WinPcap Win32 特定函數宣告。
專案 → Settings → Link → Object/library modules 加入:
wpcap.lib wsock32.lib
wpcap.lib:WinPcap 匯入庫 (來自開發套件 Lib 目錄)。wsock32.lib:Windows Socket 1.1 庫,libpcap 部分函數相依。Tools → Options → Directories → Include files 加入 WinPcap Include 目錄 (含 pcap.h, pcap-stdinc.h 等)。
Tools → Options → Directories → Library files 加入 WinPcap Lib 目錄。
#include
#include
int main()
{
char errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t* alldevs;
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr, "Error: %s\n", errbuf);
return 1;
}
for (pcap_if_t* d = alldevs; d; d = d->next)
printf("%s\n", d->name);
pcap_freealldevs(alldevs);
return 0;
}
編譯執行若能列出網卡名稱 (如 \\Device\\NPF_{...}) 即配置成功。
| 錯誤現象 | 原因與解決 |
|---|---|
| 找不到 pcap.h | Include 路徑未正確設定 |
| LNK2001: 未解析的外部符號 pcap_open_live | 未加入 wpcap.lib 或 Library 路徑錯誤 |
| 執行時載入 wpcap.dll 失敗 | 系統 PATH 未包含 WinPcap 安裝目錄,或未安裝 WinPcap 執行階段 |
| 權限錯誤 | 需管理員權限執行 (開啟網卡需原始 Socket 權限) |