public static IEnumNetCfgBindingPath ToComponentBindings(this INetCfg instance, string componentName) { object rawComponent; if (0 != instance.FindComponent(componentName, out rawComponent)) { throw new Exception(string.Format("Could not retrieve {0} component", componentName)); } var component = rawComponent as INetCfgComponent; if (component == null) { throw new Exception(string.Format("Could not retrieve {0} component", componentName)); } var bindings = component.ToComponentBindings(); object rawBindingPath; if (0 != bindings.EnumBindingPaths((int)BindingPathsFlags.Below, out rawBindingPath)) { throw new Exception("Could not retrieve binding paths"); } return((IEnumNetCfgBindingPath)rawBindingPath); }
public static INetCfgLock ToInitializedLock(this INetCfg instance) { // ReSharper disable SuspiciousTypeConversion.Global // this is legit - c# equivalent of queryinterface com operation var result = (INetCfgLock)instance; // ReSharper restore SuspiciousTypeConversion.Global string lockHeldBy; if (0 != result.AcquireWriteLock(5000, "BindingOrder", out lockHeldBy)) { throw new Exception("Could not acquire lock"); } if (0 != instance.Initialize(IntPtr.Zero)) { throw new Exception("Could not initialize net config"); } return(result); }
protected override void ProcessRecord() { var adapters = new List <NetAdapter>(); INetCfg netConfig = null; INetCfgClass netConfigClass = null; INetCfgLock netLock = null; try { netConfig = NetConfigExtensions.GetNetConfigInstance(); netLock = netConfig.ToInitializedLock(); var netCfgClassObj = new object(); netConfig.QueryNetCfgClass(ref NetCfgGuids.IidDevClassNet, ref NetCfgGuids.IidINetCfgClass, out netCfgClassObj); netConfigClass = (netCfgClassObj as INetCfgClass); var components = netConfigClass.GetComponents(); var component = components.GetNextComponent(); do { var displayName = string.Empty; var bindName = string.Empty; component.GetBindName(out bindName); component.GetDisplayName(out displayName); if (!String.IsNullOrEmpty(DisplayName) && displayName.Replace("*", "").StartsWith(DisplayName)) { var adapter = new NetAdapter(displayName, bindName); adapters.Add(adapter); } else if (!String.IsNullOrEmpty(BindName) && bindName.Replace("*", "").StartsWith(BindName)) { var adapter = new NetAdapter(displayName, bindName); adapters.Add(adapter); } else if (String.IsNullOrEmpty(BindName) && String.IsNullOrEmpty(DisplayName)) { var adapter = new NetAdapter(displayName, bindName); adapters.Add(adapter); } component = components.GetNextComponent(); } while (component != null); } catch (Exception) { throw; } finally { if (netLock != null) { netLock.ReleaseWriteLock(); } netConfig.Uninitialize(); netConfig = null; GC.Collect(); } WriteObject(adapters); }
protected override void ProcessRecord() { INetCfg netConfig = null; INetCfgClass netConfigClass = null; INetCfgLock netLock = null; WriteVerbose("Looking for Bind Name " + Adapter + " to be moved..."); try { netConfig = NetConfigExtensions.GetNetConfigInstance(); netLock = netConfig.ToInitializedLock(); //netConfigClass = (netConfig as INetCfgClass); var netCfgClassObj = new object(); netConfig.QueryNetCfgClass(ref NetCfgGuids.IidDevClassNet, ref NetCfgGuids.IidINetCfgClass, out netCfgClassObj); // Find path tokens for each device we want to move var adapterToMoveToken = string.Empty; var adapterToReplaceToken = string.Empty; var adapterToMove = default(INetCfgComponent); netConfigClass = (netCfgClassObj as INetCfgClass); var components = netConfigClass.GetComponents(); var component = components.GetNextComponent(); do { var displayName = string.Empty; var bindName = string.Empty; var nodeId = string.Empty; component.GetPnpDevNodeId(out nodeId); component.GetBindName(out bindName); component.GetDisplayName(out displayName); if (bindName.Trim().Equals(Adapter.Trim(), StringComparison.InvariantCultureIgnoreCase)) { adapterToMoveToken = nodeId; adapterToMove = component; WriteVerbose("Moving Bind Name: " + bindName + " = " + nodeId); } component = components.GetNextComponent(); } while (component != null); // Enumerate the binding paths and rework the list object rawComponent; var baseComponent = netConfig.FindComponent("ms_tcpip", out rawComponent); var pNetCfgBinding = (rawComponent as INetCfgComponentBindings); object pEnumNetCfgBindingPath; pNetCfgBinding.EnumBindingPaths((int)BindingPathsFlags.Below, out pEnumNetCfgBindingPath); var bindingPathEnum = (pEnumNetCfgBindingPath as IEnumNetCfgBindingPath); bindingPathEnum.Reset(); var bindingToMove = default(INetCfgBindingPath); do { var binding = bindingPathEnum.GetNextBindingPath(); if (binding == null) { break; } var pathToken = string.Empty; binding.GetPathToken(out pathToken); if (pathToken.EndsWith(adapterToMoveToken)) { bindingToMove = binding; } WriteVerbose("Path token found: " + pathToken); }while (true); if (bindingToMove != null) { WriteVerbose("Executing move"); pNetCfgBinding.MoveBefore(bindingToMove, null); netConfig.Apply(); } } catch (Exception) { throw; } finally { if (netLock != null) { netLock.ReleaseWriteLock(); } netConfig.Uninitialize(); netConfig = null; GC.Collect(); } }