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;
- }