// method to return an array of the active Network adapters on your system private AdaptersInfo[] EnumerateAdapters() { int adapterIndex = 0; // the current adapter index bool validAdapter = true; // are we still getting a valid adapter // we are going to look for up to 10 adapters // temp array to hold the adapters that we find AdaptersInfo[] aiTemp = new AdaptersInfo[10]; //start a loop while we look for adapters one by one starting at index 0 do { // buffer to hold the adapter information that we get byte[] buf = new byte[1024]; // uint to hold the number of bytes that we read uint iBytesRead = 0; // NDISPROT_QUERY_BINDING structure containing the index // that we want to query for NDISPROT_QUERY_BINDING ndisprot = new NDISPROT_QUERY_BINDING(); ndisprot.BindingIndex = (ulong)adapterIndex; // uint to hold the length of the ndisprot uint bufsize = (uint)Marshal.SizeOf(ndisprot); // perform the following in and unsafe context unsafe { // create a void pointer to buf fixed (void* vpBuf = buf) { // use the DeviceIoControl API to query the adapters validAdapter = DeviceIoControl(this.m_iHandle, IOCTL_NDISPROT_QUERY_BINDING, (void*)&ndisprot, bufsize, vpBuf, (uint)buf.Length, &iBytesRead, 0); } } // if DeviceIoControl returns false, then there are no // more valid adapters, so break the loop if (!validAdapter) break; // add the adapter information to the temp AdaptersInfo struct array // first, get a string containing the info from buf string tmpinfo = Encoding.Unicode.GetString(buf).Trim((char)0x00); tmpinfo = tmpinfo.Substring(tmpinfo.IndexOf("\\")); // add the info to aiTemp aiTemp[adapterIndex].Index = adapterIndex; aiTemp[adapterIndex].AdapterID = tmpinfo.Substring(0, tmpinfo.IndexOf("}")+1); aiTemp[adapterIndex].AdapterName = tmpinfo.Substring( tmpinfo.IndexOf("}")+1).Trim((char)0x00); // Increment the adapterIndex count adapterIndex++; // loop while we have a valid adapter }while (validAdapter || adapterIndex < 10); // Copy the temp adapter struct to the return struct AdaptersInfo[] aiReturn = new AdaptersInfo[adapterIndex]; for (int i=0;i<aiReturn.Length;i++) aiReturn[i] = aiTemp[i]; // return aiReturn struct return aiReturn; }