0

I get a InterfaceIndex with GetBestInterface (iphlpapi.dll).

The goal : read other interface properties.

This WMI query is slow :

SELECT MACAddress,Name,GUID,NetConnectionID FROM Win32_NetworkAdapter WHERE InterfaceIndex=

In C# there is, faster,

NetworkInterface.GetAllNetworkInterfaces()

but each NetworkInterface hasn't the property InterfaceIndex ( sic ! ).

I don't know how to optimize this :

    EnumerationOptions wmi_options = new EnumerationOptions();
    wmi_options.Rewindable = false;
    wmi_options.ReturnImmediately = true;
    string wql = "SELECT MACAddress,Name,GUID,NetConnectionID FROM Win32_NetworkAdapter WHERE InterfaceIndex=" + iface;
    ManagementObjectCollection recordset = new ManagementObjectSearcher(@"root\cimv2", wql, wmi_options).Get();
    foreach (ManagementObject mo in recordset)

The options don't seem to help. Can I split the operations and cache any step ?

Or another path : avoid WMI and lookup the interface ( with InterfaceIndex ) from the registry ?

HKLM\SYSTEM\CurrentControlSet\Services\tcpip\Parameters\Interfaces

HKLM\SYSTEM\CurrentControlSet\Control\Network{4D36E972-E325-11CE-BFC1-08002BE10318}

Massimo
  • 3,171
  • 3
  • 28
  • 41

1 Answers1

0

i dont know if this is actually faster but have you tried powershell?

string IfIndex = "15";
        string psscript = "get-netadapter |  Where-Object interfaceindex -eq " + IfIndex;
        PowerShell powershell = PowerShell.Create();
        powershell.Runspace = RunspaceFactory.CreateRunspace();
        powershell.Runspace.Open();
        powershell.AddScript(psscript);
        foreach (PSObject result in powershell.Invoke())
        {
            Console.WriteLine("Name: {0} " + "Status: {1}",result.Members["Name"].Value, result.Members["status"].Value);
        }
        Console.Read();

run this from your application, using System.Management.Automation & System.Management.Automation.Runspaces;

i haven't tested the code yet, this is the top of my head.

VisualBean
  • 4,908
  • 2
  • 28
  • 57