In general, the first thing to write a WinPcap-based application is to get a list of connected network adapters. Both Libpcap and WinPcap provide the PCAP_FINDALLDEVS_EX () function to implement this function: This function returns a list of pcap_if structures, each of which contains the details of an adapter. It is important to note that the data field name and description represent an adapter name and a description that people can understand.
We use the Microsoft Visual Studio 2010 Compilation tool to compile the program, locate the Get device list under the module in the WinPcap document, click Open to have the program code to get the device list, open the Microsoft Visual Studio 2010 Compilation Tool , click on the file, create a new project, and then set the project name, save the path can be changed to the path you want to save, you can use the default path, click Confirm, Next, complete; At this point we compile the inside of the code will be error, so next click on the compiler project--the bottom has a * * Properties (ALT+F7)
Configuration Properties---inventory tools--input and output--embedded list--No
So that the code inside can run, we will winpcap get the device list of the program copied to the program, in addition to the header files are replaced, at this time see the code in the function below is wavy line, yes, because we have not set, first look at the code, the code is as follows:
ListInterface.cpp: Defines the entry point of the console application.
//
#include "stdafx.h"
#include "Pcap.h"
int _tmain (int argc, _tchar* argv[])
{
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
Char Errbuf[pcap_errbuf_size];
/* Get a list of local machine devices */
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 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 0;
}
/* No more device list required, release it */
Pcap_freealldevs (Alldevs);
}
Make the following settings for the compiler:
Item-->** Properties (ALT+F7)
Configuration Properties-->c/c++--> General--Additional include directory--(the file path where the head file is located is added to the additional directory)
Item-->** Properties (ALT+F7)
Configure the properties----------Add the Packet.lib;wpcap.lib to the additional library directory-------
Item-->** Properties (ALT+F7)
Configuration Properties--linker--Add dependency--add "; Packet.lib;wpcap.lib "
Item-->** Properties (ALT+F7)
Configuration Properties-->c/c++--> Preprocessor---preprocessor definition--add "; Have_remote "
Here is the result graph obtained:
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/89/4A/wKioL1gPC47xsohWAAA7mn5WSQo703.png-wh_500x0-wm_3 -wmp_4-s_857095814.png "title=" ~fi%zj[oyns05_4b06sk7kp.png "alt=" Wkiol1gpc47xsohwaaa7mn5wsqo703.png-wh_50 "/>
This article from the "12034896" blog, reproduced please contact the author!
Get a list of devices (Microsoft Visual Studio 2010)