Get a list of devices

Source: Internet
Author: User

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.

The following code can get the list of adapters and display them on the screen, and if the adapter is not found, an error message will be printed.

First, PCAP_FINDALLDEVS_EX (), like the other Libpcap functions, has a errbuf parameter. Once an error occurs, this parameter is Libpcap written to the string type error message.

Second, remember that not all operating systems support the LIBPCAP provided by the network program interface, so if we want to write a portable application, we have to consider under what circumstances, description is null. In this program, when we encounter this situation, the prompt statement "No description available" is printed.

Finally, remember that when we complete the use of the device list, we call the Pcap_freealldevs () function to release the memory resources it occupies.

Specific code:

#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 "


Experimental result diagram:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/89/59/wKiom1gQSeTjMMI8AAAN7DAbz3I547.png "title=" QQ picture 20161026141441.png "alt=" Wkiom1gqsetjmmi8aaan7dabz3i547.png "/>



Get a list of devices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.