Use Winpcap to obtain the NIC list

Source: Internet
Author: User
The application Winpcap obtains the Network Driver list. The application Winpcap writes applications. The first step is to obtain the local Nic list. Next, let's take a look at how to obtain the NIC list. First, briefly describe the Winpcap function to be used. Int pcap_findalldevs (pcap_if_t ** alldevsp, char * errbuf) function: This API returns a linked list of pcap_if structures, each item in the table contains comprehensive Nic information: field name, description containing the name, and easy-to-read information about the drive. Construct Pcap_open_live (). List of Enis enabled by the function. Note: Some NICs may not be Pcap_open_live ()Open the. function. The possible cause is that you do not have sufficient permissions. If so, these Nic devices will not appear in the list. Alldevsp is set to point to the first element in the list. Each element in the list is Pcap_if_tStructure. Returned value:-1 is returned for failure. The error message is filled in errbuf. 0 is returned successfully. /*************************************** * ***********************************/more one function used is void pcap_freealldevs (pcap_if_t * alldevsp ), this function is used with the previous function for release. Pcap_open_live ()List of created NICs. Appendix :( Pcap_if_tStruct prototype) struct Pcap_if{Pcap_if * Next // if not null, a pointer to the next element in the list; null for the last element of the List char * Name // a pointer to a string giving a name for the device to pass to pcap_open_live () char * description // if not null, A pointer to a string giving a human-readable description of the device pcap_addr * addresses // a pointer to the first element of a list of addresses for the I Nterface u_int flags // pcap_if _ interface flags. currently the only possible flag is pcap_if_loopback, that is set if // The interface is a loopback interface .} typedef pacp_if pacp_if_t code example: (write in C ++ by yourself. If you find any error, let me know.) # include <pcap. h> # include <iostream. h> int main () {pcap_if_t * alldevs; pcap_if_t * temp; int idx = 0; char errbuf [pcap_errbuf_size];/* apply pcap_findalldevs () obtain the NIC list */If (-1 = pcap_findalldevs (& alldevs, Errbuf) {cout <"pcap_findalldevs () failed!: "<Errbuf <Endl; return 1 ;}for (temp = alldevs; temp = temp-> next) {idx ++; cout <idx <":" <temp-> name <Endl; If (temp-> description) cout <"Description: "<temp-> description <Endl; elsecout <" no available description. "<Endl; cout <"======================================== =====================================" <Endl ;} if (idx = 0) {cout <"no available Nic found. "<Endl; return 1;}/* Call pcap_freealldevs () to release resources */pcap_freealldevs (alldevs); return 1 ;}

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.