/// <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>
		///		Bind the driver to an adapter for use. Note that an adapter
		///		can only be bound to by one driver at a time.
		/// </summary>
		/// <param name="adapter">
		///		The adapter to bind to.
		///	</param>
		/// <exception cref="SystemException">
		///		If the driver cannot be bound to, a system exception is thrown.
		///	</exception>
		public void BindAdapter (NetworkAdapter adapter)
		{
			// char array to hold the adapterID string
			char[] ndisAdapter = new char[256];
			
			// convert the string to a non-localized unicode char array
			int nameLength = 0;
			int i = 0;

			for (i=0; i < adapter.AdapterID.Length; i++)
			{
				ndisAdapter[i] = adapter.AdapterID[i];
				nameLength++;
			}
			
			// add the null char to terminate the string
			ndisAdapter[i] = '\0';
			
			uint bytesReturned;
			bool ret = false;
			
			// attempt to bind to the adapter
			unsafe 
			{
				fixed (void* vpNdisAdapter = &ndisAdapter[0])
				{
					ret = DeviceIoControl (m_hDevice, IOCTL_NDISPROT_OPEN_DEVICE, vpNdisAdapter, (uint)(nameLength*sizeof(char)), null, 0, &bytesReturned, 0);
				}
			}
			
			if (ret)
			{
				m_boundAdapter = adapter;
			}
			else
			{
				throw new SystemException (Win32Error.GetErrorMessage (Marshal.GetLastWin32Error()));
			}
		}
示例#3
0
        /// <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);
        }