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); }
private void gv_RowEnter(object sender, DataGridViewCellEventArgs e) { var row = gv.Rows[e.RowIndex]; String id = (String)row.Cells[cId.Index].Value; var bind = (INetCfgComponentBindings)pnetcfg.FindComponent(id); cbPath.Items.Clear(); { foreach (var path in EUt.All(bind.EnumBindingPaths(EBP_ABOVE))) { cbPath.Items.Add(new DevPath { Path = path }); } cbPath.Items.Add("--- Me ---"); foreach (var path in EUt.All(bind.EnumBindingPaths(EBP_BELOW))) { cbPath.Items.Add(new DevPath { Path = path }); } } lvP.Items.Clear(); }
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(); } }