// 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); }
/// <summary> /// Enumerate network adapters to bind to. /// </summary> /// <returns> /// Returns an array of network adapters. /// </returns> private NetworkAdapter[] EnumerateAdapters() { int adapterIndex = 0; // the current adapter index bool validAdapter = true; // whether or not we are reading valid adapters // temporary array to store adapter info in NetworkAdapter[] adapters = new NetworkAdapter[10]; do { // buffer to hold returned data byte[] buf = new byte[1024]; // number of bytes read into the buffer uint bytesRead = 0; // the query binding struct NDISPROT_QUERY_BINDING ndisprot = new NDISPROT_QUERY_BINDING(); // size of the query binding struct uint bufsize = (uint)Marshal.SizeOf(ndisprot); // set the current index to the next adapter ndisprot.BindingIndex = (ulong)adapterIndex; // read the adapter info unsafe { fixed(void *vpBuf = buf) { validAdapter = DeviceIoControl(m_hDevice, IOCTL_NDISPROT_QUERY_BINDING, (void *)&ndisprot, bufsize, vpBuf, (uint)buf.Length, &bytesRead, 0); } } // if not a valid adapter, break out of the loop if (!validAdapter) { break; } string tmpinfo = Encoding.Unicode.GetString(buf).Trim((char)0x00); tmpinfo = tmpinfo.Substring(tmpinfo.IndexOf("\\")); // store the adapter information adapters[adapterIndex] = new NetworkAdapter(); adapters[adapterIndex].Index = adapterIndex; adapters[adapterIndex].AdapterID = tmpinfo.Substring(0, tmpinfo.IndexOf("}") + 1); adapters[adapterIndex].AdapterName = tmpinfo.Substring(tmpinfo.IndexOf("}") + 1).Trim((char)0x00); // increment the adapterIndex count adapterIndex++; }while (validAdapter && adapterIndex < 10); // copy the temp adapter struct to the return struct NetworkAdapter[] returnInfo = new NetworkAdapter[adapterIndex]; for (int i = 0; i < returnInfo.Length; i++) { returnInfo[i] = adapters[i]; } // return the adapters return(returnInfo); }
/// <summary> /// Enumerate network adapters to bind to. /// </summary> /// <returns> /// Returns an array of network adapters. /// </returns> private NetworkAdapter[] EnumerateAdapters () { int adapterIndex = 0; // the current adapter index bool validAdapter = true; // whether or not we are reading valid adapters // temporary array to store adapter info in NetworkAdapter[] adapters = new NetworkAdapter[10]; do { // buffer to hold returned data byte[] buf = new byte[1024]; // number of bytes read into the buffer uint bytesRead = 0; // the query binding struct NDISPROT_QUERY_BINDING ndisprot = new NDISPROT_QUERY_BINDING(); // size of the query binding struct uint bufsize = (uint)Marshal.SizeOf(ndisprot); // set the current index to the next adapter ndisprot.BindingIndex = (ulong)adapterIndex; // read the adapter info unsafe { fixed (void* vpBuf = buf) { validAdapter = DeviceIoControl (m_hDevice, IOCTL_NDISPROT_QUERY_BINDING, (void*)&ndisprot, bufsize, vpBuf, (uint)buf.Length, &bytesRead, 0); } } // if not a valid adapter, break out of the loop if (!validAdapter) { break; } string tmpinfo = Encoding.Unicode.GetString(buf).Trim((char)0x00); tmpinfo = tmpinfo.Substring(tmpinfo.IndexOf("\\")); // store the adapter information adapters[adapterIndex] = new NetworkAdapter (); adapters[adapterIndex].Index = adapterIndex; adapters[adapterIndex].AdapterID = tmpinfo.Substring(0, tmpinfo.IndexOf("}")+1); adapters[adapterIndex].AdapterName = tmpinfo.Substring(tmpinfo.IndexOf("}")+1).Trim((char)0x00); // increment the adapterIndex count adapterIndex++; } while (validAdapter && adapterIndex < 10); // copy the temp adapter struct to the return struct NetworkAdapter[] returnInfo = new NetworkAdapter[adapterIndex]; for (int i=0; i < returnInfo.Length; i++) { returnInfo[i] = adapters[i]; } // return the adapters return returnInfo; }
// 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; }