private void InterfacesList_Load(object sender, EventArgs e) { this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; this.dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; NetworkInterfaceList nil = new NetworkInterfaceList(); this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = nil.Interfaces; this.dataGridView2.DataSource = null; this.dataGridView2.DataSource = AdapterInfo.GetAdapters(); }
public AddExistingRDPConnections() { this.DiscoveredConnections = new List<FavoriteConfigurationElement>(); InitializeComponent(); this.dataGridView1.Visible = false; _miv = new MethodInvoker(this.UpdateConnections); try { _nil = new NetworkInterfaceList(); } catch (Exception exc) { Logging.Error("Could not new up Metro.NetworkInterfaceList in AddExistingRDPConnections", exc); } }
private void PortScanner_Load(object sender, EventArgs eargs) { NetworkInterfaceList nil = new NetworkInterfaceList(); try { foreach (NetworkInterface face in nil.Interfaces) { if (face.IsEnabled && !face.isLoopback) { this.endPointAddress = face.Address; string[] parts = this.endPointAddress.ToString().Split('.'); this.a.Text = parts[0]; this.b.Text = parts[1]; this.c.Text = parts[2]; this.d.Text = parts[3]; this.e.Text = parts[3]; break; } } } catch (Exception exc) { Log.Error("Error connecting to the network interfaces.", exc); } }
/// <summary> /// Open the device driver. /// </summary> /// <param name="deviceName"> /// The device driver name. /// </param> /// <exception cref="SystemException"> /// A system error occured when opening the driver. /// </exception> public void OpenDevice (string deviceName) { // attempt to open the device driver m_hDevice = CreateFile (deviceName, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // if the handle is invalid then reset it back to 0 and return false if ((int)m_hDevice <= 0) { m_hDevice = IntPtr.Zero; throw new SystemException (Win32Error.GetErrorMessage (Marshal.GetLastWin32Error())); } // enumerate the adapters m_adapters = EnumerateAdapters (); // now find the adapter info associated with each device int bufferSize = 0; int ret = 0; // make an initial call to return the buffer size ret = GetAdaptersInfo (IntPtr.Zero, ref bufferSize); // if an error other than buffer overflow occured then return false if (ret != ERROR_BUFFER_OVERFLOW) { throw new SystemException (Win32Error.GetErrorMessage (ret)); } // allocate some memory IntPtr pBuffer = Marshal.AllocHGlobal (bufferSize); // make another call, this time with the correct size ret = GetAdaptersInfo (pBuffer, ref bufferSize); // if it didn't succeed, return false if (ret != 0) { throw new SystemException (Win32Error.GetErrorMessage (ret)); } do { // read the adapter id string AdapterID = Marshal.PtrToStringAnsi (new IntPtr(pBuffer.ToInt32() + 8)); // loop through each bindable adapter we know about for (int i = 0; i < m_adapters.Length; i++) { // if this is the adapter we want if (m_adapters[i].AdapterID.Substring (m_adapters[i].AdapterID.LastIndexOf ("\\") + 1) == AdapterID) { // read the address length int addressLength = Marshal.ReadByte (new IntPtr(pBuffer.ToInt32() + MAX_ADAPTER_NAME_LENGTH + MAX_ADAPTER_DESCRIPTION_LENGTH + 8)); // read the physical address byte[] physicalAddress = new byte[addressLength]; for (int j = 0; j < addressLength; j++) { physicalAddress[j] = Marshal.ReadByte (new IntPtr(pBuffer.ToInt32() + MAX_ADAPTER_NAME_LENGTH + MAX_ADAPTER_DESCRIPTION_LENGTH + 12 + j)); } m_adapters[i].MediaAccessControlAddress = new MACAddress (physicalAddress); int IpCount = 0; // calculate the pointer to the IP address list IntPtr pAddressList = new IntPtr(pBuffer.ToInt32() + MAX_ADAPTER_NAME_LENGTH + MAX_ADAPTER_DESCRIPTION_LENGTH + MAX_ADAPTER_ADDRESS_LENGTH + 28); IntPtr pIpAddressString = pAddressList; // follow the pointers until we reach a null pointer, so we know how // many there are do { IpCount++; pIpAddressString = Marshal.ReadIntPtr (pIpAddressString, 0); } while (pIpAddressString.ToInt32() > 0); // now we know how many to allocate m_adapters[i].Interfaces = new NetworkInterface[IpCount]; // create an interface list enumeration NetworkInterfaceList m_interfaces = new NetworkInterfaceList (); // start at the beginning of the list again pIpAddressString = pAddressList; IpCount = 0; do { // read the IP IPAddress ipAddress = IPAddress.Parse (Marshal.PtrToStringAnsi (new IntPtr(pIpAddressString.ToInt32() + 4))); // find the interface which this IP belongs to for (int j = 0; j < m_interfaces.Interfaces.Length; j++) { if (m_interfaces.Interfaces[j].Address.Equals (ipAddress)) { m_adapters[i].Interfaces[IpCount] = m_interfaces.Interfaces[j]; break; } } IpCount++; pIpAddressString = Marshal.ReadIntPtr (pIpAddressString, 0); } while (pIpAddressString.ToInt32() > 0); } } pBuffer = Marshal.ReadIntPtr (pBuffer, 0); } while (pBuffer.ToInt32() > 0); // free the allocated memory Marshal.FreeHGlobal (pBuffer); }
/// <summary> /// Open the device driver. /// </summary> /// <param name="deviceName"> /// The device driver name. /// </param> /// <exception cref="SystemException"> /// A system error occured when opening the driver. /// </exception> public void OpenDevice(string deviceName) { // attempt to open the device driver m_hDevice = CreateFile(deviceName, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // if the handle is invalid then reset it back to 0 and return false if ((int)m_hDevice <= 0) { m_hDevice = IntPtr.Zero; throw new SystemException(Win32Error.GetErrorMessage(Marshal.GetLastWin32Error())); } // enumerate the adapters m_adapters = EnumerateAdapters(); // now find the adapter info associated with each device int bufferSize = 0; int ret = 0; // make an initial call to return the buffer size ret = GetAdaptersInfo(IntPtr.Zero, ref bufferSize); // if an error other than buffer overflow occured then return false if (ret != ERROR_BUFFER_OVERFLOW) { throw new SystemException(Win32Error.GetErrorMessage(ret)); } // allocate some memory IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize); // make another call, this time with the correct size ret = GetAdaptersInfo(pBuffer, ref bufferSize); // if it didn't succeed, return false if (ret != 0) { throw new SystemException(Win32Error.GetErrorMessage(ret)); } do { // read the adapter id string AdapterID = Marshal.PtrToStringAnsi(new IntPtr(pBuffer.ToInt32() + 8)); // loop through each bindable adapter we know about for (int i = 0; i < m_adapters.Length; i++) { // if this is the adapter we want if (m_adapters[i].AdapterID.Substring(m_adapters[i].AdapterID.LastIndexOf("\\") + 1) == AdapterID) { // read the address length int addressLength = Marshal.ReadByte(new IntPtr(pBuffer.ToInt32() + MAX_ADAPTER_NAME_LENGTH + MAX_ADAPTER_DESCRIPTION_LENGTH + 8)); // read the physical address byte[] physicalAddress = new byte[addressLength]; for (int j = 0; j < addressLength; j++) { physicalAddress[j] = Marshal.ReadByte(new IntPtr(pBuffer.ToInt32() + MAX_ADAPTER_NAME_LENGTH + MAX_ADAPTER_DESCRIPTION_LENGTH + 12 + j)); } m_adapters[i].MediaAccessControlAddress = new MACAddress(physicalAddress); int IpCount = 0; // calculate the pointer to the IP address list IntPtr pAddressList = new IntPtr(pBuffer.ToInt32() + MAX_ADAPTER_NAME_LENGTH + MAX_ADAPTER_DESCRIPTION_LENGTH + MAX_ADAPTER_ADDRESS_LENGTH + 28); IntPtr pIpAddressString = pAddressList; // follow the pointers until we reach a null pointer, so we know how // many there are do { IpCount++; pIpAddressString = Marshal.ReadIntPtr(pIpAddressString, 0); }while (pIpAddressString.ToInt32() > 0); // now we know how many to allocate m_adapters[i].Interfaces = new NetworkInterface[IpCount]; // create an interface list enumeration NetworkInterfaceList m_interfaces = new NetworkInterfaceList(); // start at the beginning of the list again pIpAddressString = pAddressList; IpCount = 0; do { // read the IP IPAddress ipAddress = IPAddress.Parse(Marshal.PtrToStringAnsi(new IntPtr(pIpAddressString.ToInt32() + 4))); // find the interface which this IP belongs to for (int j = 0; j < m_interfaces.Interfaces.Length; j++) { if (m_interfaces.Interfaces[j].Address.Equals(ipAddress)) { m_adapters[i].Interfaces[IpCount] = m_interfaces.Interfaces[j]; break; } } IpCount++; pIpAddressString = Marshal.ReadIntPtr(pIpAddressString, 0); }while (pIpAddressString.ToInt32() > 0); } } pBuffer = Marshal.ReadIntPtr(pBuffer, 0); }while (pBuffer.ToInt32() > 0); // free the allocated memory Marshal.FreeHGlobal(pBuffer); }