예제 #1
0
        private void VForm_Load(object sender, EventArgs e)
        {
            INetCfgLock locker = (INetCfgLock)pnetcfg;
            string      client;

            locker.AcquireWriteLock(5000, "INetCfg_viewer", out client);

            pnetcfg.Initialize(IntPtr.Zero);

            rbGUID_DEVCLASS_NET_CheckedChanged(sender, e);
        }
예제 #2
0
        private void bTop_Click(object sender, EventArgs e)
        {
            var    row  = gv.CurrentRow;
            String id   = (String)row.Cells[cId.Index].Value;
            var    bind = (INetCfgComponentBindings)pnetcfg.FindComponent(id);

            DevPath path = (DevPath)cbPath.SelectedItem;

            MessageBox.Show("" + bind.MoveBefore(path.Path, null));

            pnetcfg.Apply();
            pnetcfg.Uninitialize();
            INetCfgLock locker = (INetCfgLock)pnetcfg;

            locker.ReleaseWriteLock();
        }
        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();
            }
        }