static void UpdateAdapterList() { bool succeeded = false; while (!succeeded) { if (!isNdisFilterDriverOpen) { OpenNDISDriver(); } TCP_AdapterList adList = new TCP_AdapterList(); Ndisapi.GetTcpipBoundAdaptersInfo(hNdisapi, ref adList); List<NetworkAdapter> tempList = new List<NetworkAdapter>(); //Populate with current adapters List<NetworkAdapter> notFound = new List<NetworkAdapter>(); for (int x = 0; x < currentAdapters.Count; x++) { bool found = false; for (int y = 0; y < adList.m_nAdapterCount; y++) { if (adList.m_nAdapterHandle[y] == currentAdapters[x].adapterHandle) { currentAdapters[x].UpdateNetworkInterface(Encoding.ASCII.GetString(adList.m_szAdapterNameList, y * 256, 256)); tempList.Add(currentAdapters[x]); found = true; } } if (!found) { notFound.Add(currentAdapters[x]); } } //Deal with no longer existant adapters for (int x = 0; x < notFound.Count; x++) { notFound[x].SetNoLongerAvailable(); } //Adding any new adapters for (int x = 0; x < adList.m_nAdapterCount; x++) { bool found = false; for (int y = 0; y < currentAdapters.Count; y++) { if (adList.m_nAdapterHandle[x] == currentAdapters[y].adapterHandle) found = true; } if (!found) { NetworkAdapter newAdapter = new NetworkAdapter(adList.m_nAdapterHandle[x], Encoding.ASCII.GetString(adList.m_szAdapterNameList, x * 256, 256)); if (newAdapter.InterfaceInformation != null) { tempList.Add(newAdapter); } } } currentAdapters = new List<NetworkAdapter>(tempList); succeeded = true; } }
static void OpenNDISDriver() { if (hNdisapi != IntPtr.Zero) { LogCenter.Instance.Push("NetworkAdapter-static", "Bad state was found, attempting to open the NDIS Filter Driver while the IntPtr != IntPtr.Zero, continuing"); } hNdisapi = Ndisapi.OpenFilterDriver(Ndisapi.NDISRD_DRIVER_NAME); TCP_AdapterList adList = new TCP_AdapterList(); Ndisapi.GetTcpipBoundAdaptersInfo(hNdisapi, ref adList); if (adList.m_nAdapterCount == 0) { LogCenter.WriteErrorLog(new Exception("No adapters found on this driver interface")); return; } isNdisFilterDriverOpen = true; }
public static extern bool GetTcpipBoundAdaptersInfo(IntPtr hOpen, ref TCP_AdapterList Adapters);
public static List<NetworkAdapter> GetNewAdapters() { if (!isNdisFilterDriverOpen) { OpenNDISDriver(); } TCP_AdapterList adList = new TCP_AdapterList(); Ndisapi.GetTcpipBoundAdaptersInfo(hNdisapi, ref adList); List<NetworkAdapter> tempList = new List<NetworkAdapter>(); for (int x = 0; x < currentAdapters.Count; x++) { for (int y = 0; y < adList.m_nAdapterCount; y++) { if (adList.m_nAdapterHandle[y] == currentAdapters[x].adapterHandle) { currentAdapters[x].UpdateNetworkInterface(Encoding.ASCII.GetString(adList.m_szAdapterNameList, y * 256, 256)); } } } for (int x = 0; x < adList.m_nAdapterCount; x++) { bool found = false; for (int y = 0; y < currentAdapters.Count; y++) { if (adList.m_nAdapterHandle[x] == currentAdapters[y].adapterHandle) found = true; } if (!found) { NetworkAdapter newAdapter = new NetworkAdapter(adList.m_nAdapterHandle[x], Encoding.ASCII.GetString(adList.m_szAdapterNameList, x * 256, 256)); if (newAdapter.InterfaceInformation != null) { tempList.Add(newAdapter); currentAdapters.Add(newAdapter); } } } return tempList; }