我正在尝试监控我的每月网络使用情况。因为默认的Windows 10数据使用页面不起作用,所以我借机学习了C ++,因为我熟悉Python和PHP等语言。
经过几个小时的Google搜索,我得出的结论是WinPcap是我应该使用的模块。我从这里下载了它:https://www.winpcap.org/devel.htm
我将.zip解压缩到我的C ++控制台应用程序文件夹中。所以我的申请是在C:\Visual Studio\ProjectName123\
,我把WpdPack/
提取到那里。
我正在尝试使用他们的示例代码:
#include "pch.h"
#include "WpdPack\Include\pcap\pcap.h"
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;
}
/* We don't need any more the device list. Free it */
pcap_freealldevs(alldevs);
}
我得到了几个错误,从:identifier "PCAP_SRC_IF_STRING" is undefined
开始
跟随T的示例非常令人沮丧,并且它没有正确运行。对C ++非常沮丧。
帮助将不胜感激,具体解释为什么我的代码,完全遵循this example,不运行。
该示例代码具有误导性;他们打算用字符串值替换PCAP_SRC_IF_STRING
而不是按原样使用它。要使用pcap_findalldevs_ex()
,您需要将第一个参数作为字符串传递,指定它应该在哪里查找适配器。你应该发现以下更好的方法:
pcap_findalldevs_ex("rpcap://",...
我建议您使用这是一个参考:WinPCAP exported functions