private void EnumerateChildren()
        {
            IntPtr ptrFirstChild = IntPtr.Zero;

            if (Win32.CM_Get_Child_Ex(
                    ref ptrFirstChild,
                    _deviceHandle,
                    0,
                    _machineHandle) != 0)
            {
                return;
            }

            var ptrChild   = ptrFirstChild;
            var ptrSibling = IntPtr.Zero;

            do
            {
                var childDevice = new DeviceNode(ptrChild, this);
                _children.Add(childDevice);

                if (Win32.CM_Get_Sibling_Ex(
                        ref ptrSibling,
                        ptrChild,
                        0,
                        _machineHandle) != 0)
                {
                    break;
                }

                ptrChild = ptrSibling;
            }while (true);
        }
 public DeviceNode(IntPtr deviceHandle, DeviceNode parentDevice)
     : this(
         deviceHandle,
         parentDevice,
         parentDevice._machineHandle)
 {
 }
        public DeviceNode(
            IntPtr deviceHandle,
            DeviceNode parentDevice,
            IntPtr machineHandle)
        {
            _deviceProperties = new Dictionary <int, string>();
            _children         = new List <DeviceNode>();

            _deviceHandle  = deviceHandle;
            _machineHandle = machineHandle;
            _parentDevice  = parentDevice;

            EnumerateDeviceProperties();
            EnumerateChildren();
        }
        private void EnumerateDevices()
        {
            this.DisconnectFromMachine();

            // local machine assumed
            if (Win32.CM_Connect_Machine(
                    null, ref this._machineHandle) != 0)
            {
                return;
            }

            try
            {
                Win32.CM_Locate_DevNode_Ex(
                    ref this._rootDeviceHandle,
                    0,
                    0,
                    this._machineHandle);

                // recursive enumeration
                this._rootNode = new DeviceNode(
                    this._rootDeviceHandle,
                    null,
                    this._machineHandle);
            }
            finally
            {
                this.DisconnectFromMachine();

                if (this._rootNode != null)
                {
                    this._deviceNodes = this._rootNode
                                        .Flatten(node => node.Children).ToList();
                }
            }
        }