//网卡列表处理函数 int NetworkInterface::RefreshAdapterList() { char AdapterListStr[10][1024]; int AdapterNum = 10; GetAdapterList(AdapterListStr, AdapterNum); //清除原有的网卡信息 for(int i = 0; i < AdapterList.size(); ++i) FreeAdapter(&(AdapterList[i])); AdapterList.clear(); Adapter adapter; for(int i = 0; i < AdapterNum; ++i) { adapter.AdapterName = new char[strlen(AdapterListStr[i]) + 2]; strcpy(adapter.AdapterName, AdapterListStr[i]); AdapterList.push_back(adapter); } return 0; }
// I/O控制派遣例程 NTSTATUS DispatchIoctl(PDEVICE_OBJECT pDevObj, PIRP pIrp) { // 假设失败 NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST; // 取得此IRP(pIrp)的I/O堆栈指针 PIO_STACK_LOCATION pIrpStack = IoGetCurrentIrpStackLocation(pIrp); // 取得I/O控制代码 ULONG uIoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode; // 取得I/O缓冲区指针和它的长度 PVOID pIoBuffer = pIrp->AssociatedIrp.SystemBuffer; ULONG uInSize = pIrpStack->Parameters.DeviceIoControl.InputBufferLength; ULONG uOutSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength; if(uIoControlCode == IOCTL_ENUM_ADAPTERS) { ULONG nDataLen = 0; if(pDevObj != g_data.pControlDevice) status = STATUS_INVALID_DEVICE_REQUEST; else { status = GetAdapterList(pIoBuffer, uOutSize, &nDataLen); if(status != STATUS_SUCCESS) DbgPrint("GetAdapterList error "); } pIrp->IoStatus.Information = nDataLen; pIrp->IoStatus.Status = status; IoCompleteRequest(pIrp, IO_NO_INCREMENT); return status; } OPEN_INSTANCE *pOpen = (OPEN_INSTANCE *)pDevObj->DeviceExtension; if(pOpen == NULL || !pOpen->bBound) { pIrp->IoStatus.Status = STATUS_UNSUCCESSFUL; IoCompleteRequest(pIrp, IO_NO_INCREMENT); return STATUS_UNSUCCESSFUL; } IoIncrement(pOpen); IoMarkIrpPending(pIrp); if(uIoControlCode == IOCTL_PROTOCOL_RESET) { // 插入此IRP到重置IRP列表 ExInterlockedInsertTailList( &pOpen->ResetIrpList, &pIrp->Tail.Overlay.ListEntry, &pOpen->ResetQueueLock); // 发出重置请求 NdisReset( &status, pOpen->hAdapter ); if(status != NDIS_STATUS_PENDING) { ProtocolResetComplete( pOpen, status); } } // 获取或者设置OID信息 else if(uIoControlCode == IOCTL_PROTOCOL_SET_OID || uIoControlCode == IOCTL_PROTOCOL_QUERY_OID) // 输入参数是一个自定义的PROTOCOL_OID_DATA结构 { PPROTOCOL_OID_DATA pOidData = (PPROTOCOL_OID_DATA)pIoBuffer; // 申请一个INTERNAL_REQUEST结构 PINTERNAL_REQUEST pInterRequest = (PINTERNAL_REQUEST)ExAllocatePool(NonPagedPool, sizeof(INTERNAL_REQUEST)); if(pInterRequest == NULL) { pIrp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES; IoCompleteRequest(pIrp, IO_NO_INCREMENT); IoDecrement(pOpen); return STATUS_PENDING; } pInterRequest->pIrp = pIrp; if(uOutSize == uInSize && uOutSize >= sizeof(PROTOCOL_OID_DATA) && uOutSize >= sizeof(PROTOCOL_OID_DATA) - 1 + pOidData->Length) // 缓冲区可用? { // 初始化NDIS_REQUEST结构 if(uIoControlCode == IOCTL_PROTOCOL_SET_OID) { pInterRequest->Request.RequestType = NdisRequestSetInformation; pInterRequest->Request.DATA.SET_INFORMATION.Oid = pOidData->Oid; pInterRequest->Request.DATA.SET_INFORMATION.InformationBuffer = pOidData->Data; pInterRequest->Request.DATA.SET_INFORMATION.InformationBufferLength = pOidData->Length; } else { pInterRequest->Request.RequestType = NdisRequestQueryInformation; pInterRequest->Request.DATA.QUERY_INFORMATION.Oid = pOidData->Oid; pInterRequest->Request.DATA.QUERY_INFORMATION.InformationBuffer = pOidData->Data; pInterRequest->Request.DATA.QUERY_INFORMATION.InformationBufferLength = pOidData->Length; } // 提交这个请求 NdisRequest(&status, pOpen->hAdapter, &pInterRequest->Request); } else { status = NDIS_STATUS_FAILURE; pInterRequest->Request.DATA.SET_INFORMATION.BytesRead = 0; pInterRequest->Request.DATA.QUERY_INFORMATION.BytesWritten = 0; } if(status != NDIS_STATUS_PENDING) { ProtocolRequestComplete(pOpen, &pInterRequest->Request, status); } } return STATUS_PENDING; }
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndToolBar.CreateEx(this) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } if (!m_wndReBar.Create(this) || !m_wndReBar.AddBar(&m_wndToolBar)) { TRACE0("Failed to create rebar\n"); return -1; // fail to create } if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } m_wndStatusBar.SetPaneInfo(1, ID_BYTES_SEC, SBPS_NORMAL, 200); // TODO: Remove this if you don't want tool tips m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY); // update application title CString sTitle; sTitle.LoadString(AFX_IDS_APP_TITLE); SetWindowText(sTitle); // init toolbar adapter combo #define ADAPTER_WIDTH 180 //the width of the combo box //First get the index of the placeholder's position in the toolbar int index = 0; while(m_wndToolBar.GetItemID(index) != IDC_ADAPTER_COMBO) index++; //next convert that button to a seperator and get its position m_wndToolBar.SetButtonInfo(index, IDC_ADAPTER_COMBO, TBBS_SEPARATOR, ADAPTER_WIDTH); CRect rect; m_wndToolBar.GetItemRect(index, &rect); //expand the rectangle to allow the combo box room to drop down rect.top += 1; rect.bottom += 200; // then .Create the combo box and show it if (!m_wndAdapter.Create(WS_CHILD|WS_VISIBLE | CBS_AUTOHSCROLL | CBS_DROPDOWNLIST | CBS_HASSTRINGS , rect, &m_wndToolBar, IDC_ADAPTER_COMBO)) { TRACE0("Failed to create combo-box\n"); return -1; } // set combo box font CFont *pFnt = m_wndStatusBar.GetFont(); m_wndAdapter.SetFont(pFnt); m_wndAdapter.ShowWindow(SW_SHOW); //fill the combo box GetAdapterList(); m_wndAdapter.SetCurSel(0); g_pwndAdapter = &m_wndAdapter; g_pwndStatusBar = &m_wndStatusBar; // init from .ini file if (g_Settings.m_bAlwaysOnTop) ::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); else ::SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); g_NSCashe.EnableNameResolve(g_Settings.m_bNSResolve); g_ServicesParser.EnableServicesResolve(g_Settings.m_bPortResolve); return 0; }