This section mainly describes how to get a list of devices, relatively simple
The Get device list is primarily done in Cadpdlg, which corresponds to the selection adapter module created earlier,
When the Select Adapter dialog box is opened, a description of all adapters and adapters for the current host is displayed in the List view control, and when an adapter is selected, the currently checked adapter is displayed in the edit box below, and clicking the binding will prompt the NIC to bind successfully.
The code in this section is done in AdpDlg.cpp, and of course the declaration of the variable is to be done in its corresponding header file, which is no longer described here.
To get the list of devices first, add the following code to the OnInitDialog function:
1 if (PCAP_FINDALLDEVS_EX (pcap_src_if_string, NULL, &alldevs, errbuf) = =-1) 2 return FALSE;
So all the devices on the host are put into the Alldevs list, and each of the devices in the chain is sequentially output to the listbox:
1 for (D=alldevs; D; d=d->next) 2 { 3 M_list1. InsertItem (0 , (CString) d->name); // d->name is char * and needs to be cast to CString to display in InsertItem 4 m_list1. Setitemtext (0 , 1 , (CString) D->description); 5 6 d = NULL; // empty so that other functions use the
Note String variables of type char are not accepted in MFC, and all string variables should be of type CString, similar to string types in C + +. Next we want to select a device in the List view control and then display it in the edit box below, which requires a trigger function for the list view control. Select the properties of the corresponding list view space in the properties, select the control event (Lightning), then find the Nm_click, click on it will generate the function, add the following code in the function:
1 voidCadpdlg::onnmclicklist1 (NMHDR *pnmhdr, LRESULT *PResult)2 {3Lpnmitemactivate pnmitemactivate = reinterpret_cast<lpnmitemactivate>(PNMHDR);4 //TODO: Add control notification handler code here5*presult =0;6 7 8Nmlistview *pnmlistview = (nmlistview*) Pnmhdr; 9 Ten if(-1! = Pnmlistview->iitem)//if IItem is not-1, it means that a list item is selected One { A //gets the text of the first subkey of a selected list item -Adpname = M_list1. GetItemText (Pnmlistview->iitem,0); - //Displays the selected language in the edit box the Setdlgitemtext (idc_edit1, adpname); - } -}
The acquired device is then returned based on the user's choice:
1 //returns the selected device2pcap_if_t*Cadpdlg::getdevice ()3 {4 if(Adpname)5 {6 for(D=alldevs; D; d=d->next)7 if(D->name = =adpname)8 returnD;9 }Ten returnNULL; One}
Click Bind to check if it is legitimate:
1 voidCadpdlg::onbnclickedok ()2 {3 //TODO: Add control notification handler code here4D =GetDevice ();5 if(d)6 {7MessageBox (_t ("NIC binding successful!"));8 Cdialogex::onok ();9 }Ten Else OneMessageBox (_t ("Please select the network card to bind")); A}
This gets the module, it's almost done.
Next section Mfc+winpcap write a sniffer five (filter module)
Mfc+winpcap Write a sniffer quad (get module)