/// <param name="hardwareID">enumerator-specific-device-id</param>
        public static string DetectExportedDeviceInstanceID(string path, string hardwareID, out string deviceID)
        {
            Console.WriteLine("Searching for '" + hardwareID + "' in " + path);
            deviceID = String.Empty; // sometimes the device presents longer hardware ID than the one specified in the driver

            string enumerator = PNPDriverIntegratorUtils.GetEnumeratorNameFromHardwareID(hardwareID);

            if (enumerator == "*")
            {
                return(String.Empty); // unsupported enumerator;
            }

            string deviceInstanceID     = String.Empty;
            ExportedRegistryKey hiveKey = new ExportedRegistry(path).LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum" + enumerator);

            foreach (string deviceKeyName in hiveKey.GetSubKeyNames())
            {
                ExportedRegistryKey deviceKey = hiveKey.OpenSubKey(deviceKeyName);
                if (deviceKey != null)
                {
                    foreach (string instanceKeyName in deviceKey.GetSubKeyNames())
                    {
                        ExportedRegistryKey instanceKey = deviceKey.OpenSubKey(instanceKeyName);
                        if (instanceKey != null)
                        {
                            object compatibleIDsEntry = instanceKey.GetValue("CompatibleIDs", new string[0]);
                            if (compatibleIDsEntry is string[])
                            {
                                string[] compatibleIDs = (string[])compatibleIDsEntry;

                                foreach (string compatibleID in compatibleIDs)
                                {
                                    if (compatibleID.Equals(hardwareID, StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        deviceID         = RegistryKeyUtils.GetShortKeyName(deviceKey.Name);
                                        deviceInstanceID = RegistryKeyUtils.GetShortKeyName(instanceKey.Name);
                                        // Irrelevant Note: if a device is present but not installed in Windows then ConfigFlags entry will not be present
                                        // and it doesn't matter anyway because we don't care about how existing installation configure the device

                                        // there are two reasons not to use DeviceDesc from the local machine:
                                        // 1. on Windows 6.0+ (or just Windows PE?) the format is different and not compatible with Windows 5.x
                                        // 2. If the hadrware is present but not installed, the DeviceDesc will be a generic description (e.g. 'Ethernet Controller')

                                        Console.WriteLine("Found matching device: '" + deviceID + "'");
                                        return(deviceInstanceID);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(deviceInstanceID);
        }
        public void RegisterPhysicalDevice(PNPDriverINFFile pnpDriverInf)
        {
            if (m_preconfigure && pnpDriverInf.IsNetworkAdapter && (m_useLocalHardwareConfig || m_enumExportPath != String.Empty))
            {
                string deviceID;
                string deviceInstanceID;

                if (m_useLocalHardwareConfig)
                {
                    deviceInstanceID = PNPLocalHardwareDetector.DetectLocalDeviceInstanceID(this.HardwareID, out deviceID);
                    if (deviceInstanceID == String.Empty)
                    {
                        Console.WriteLine("Warning: Could not detect matching device installed locally, configuration will not be applied!");
                    }
                }
                else // m_enumExportPath != String.Empty
                {
                    deviceInstanceID = PNPExportedHardwareDetector.DetectExportedDeviceInstanceID(m_enumExportPath, this.HardwareID, out deviceID);
                    if (deviceInstanceID == String.Empty)
                    {
                        Console.WriteLine("Warning: Could not detect matching device in the exported registry, configuration will not be applied!");
                    }
                }

                if (deviceInstanceID != String.Empty)
                {
                    // m_netDeviceServices is now populated
                    if (this.NetworkDeviceServices.Count > 0)
                    {
                        // unlike other types of hardware (SCSI controllers etc.), it's not enough to add a NIC to the
                        // Criticla Device Database (CDDB) to make it usable during boot, as mentioned in the comments above RegisterNicAsCriticalDevice()
                        // at the very least, a CDDB entry and a "Device" registry value under Enum\Enumerator\DeviceID\DeviceInstanceID is required
                        // (as well as DeviceDesc if not automatically added by the kernel-PNP)
                        // here we manually register the hardware in advance, but it's better to use NICBootConf to do this during boot,
                        // NICBootConf will also work if the NIC has been moved to another PCI slot since creating the installation media.

                        // the first item in m_netDeviceServices should be the actual NIC (CHECKME: what about NIC / bus driver combination like nVIdia)
                        NetworkDeviceService deviceService = this.NetworkDeviceServices[0];
                        string enumerator = PNPDriverIntegratorUtils.GetEnumeratorNameFromHardwareID(this.HardwareID);
                        PreconfigureDeviceInstance(pnpDriverInf, enumerator, deviceID, deviceInstanceID, deviceService);
                    }
                    else
                    {
                        Console.WriteLine("Warning: failed to install '{0}', because the service for this network adapter has not been registered!", this.HardwareID);
                    }
                }
            }
            else
            {
                // if it's a NIC, We assume the user will integrate NICBootConf, which will configure the network adapter during boot.
                // we'll just add the device to the Criticla Device Database (CDDB), and let kernel-PNP and NICBootConf do the rest.
                if (pnpDriverInf.IsNetworkAdapter)
                {
                    // NICBootConf needs the ClassGUID in place for each DeviceInstance Key,
                    // if we put the ClassGUID in the CDDB, the ClassGUID will be applied to each DeviceInstance with matching hardwareID
                    m_installation.TextSetupInf.AddDeviceToCriticalDeviceDatabase(this.HardwareID, this.DeviceServices[0].ServiceName, PNPDriverINFFile.NetworkAdapterClassGUID);
                    m_installation.HiveSystemInf.AddDeviceToCriticalDeviceDatabase(this.HardwareID, this.DeviceServices[0].ServiceName, PNPDriverINFFile.NetworkAdapterClassGUID);
                }
                else
                {
                    m_installation.TextSetupInf.AddDeviceToCriticalDeviceDatabase(this.HardwareID, this.DeviceServices[0].ServiceName);
                    m_installation.HiveSystemInf.AddDeviceToCriticalDeviceDatabase(this.HardwareID, this.DeviceServices[0].ServiceName);
                }
            }
        }
Exemple #3
0
        /// <param name="hardwareID">enumerator-specific-device-id</param>
        public static string DetectLocalDeviceInstanceID(string hardwareID, out string deviceID)
        {
            Console.WriteLine("Searching for '" + hardwareID + "' on local machine");
            deviceID = String.Empty; // sometimes the device presents longer hardware ID than the one specified in the driver

            string enumerator = PNPDriverIntegratorUtils.GetEnumeratorNameFromHardwareID(hardwareID);

            if (enumerator == "*")
            {
                return(String.Empty); // unsupported enumerator;
            }

            string      deviceInstanceID = String.Empty;
            RegistryKey hiveKey          = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\" + enumerator);

            foreach (string deviceKeyName in hiveKey.GetSubKeyNames())
            {
                RegistryKey deviceKey = hiveKey.OpenSubKey(deviceKeyName);
                if (deviceKey != null)
                {
                    foreach (string instanceKeyName in deviceKey.GetSubKeyNames())
                    {
                        RegistryKey instanceKey = deviceKey.OpenSubKey(instanceKeyName);
                        if (instanceKey != null)
                        {
                            List <string> hardwareIDs = new List <string>();

                            object hardwareIDEntry = instanceKey.GetValue("HardwareID", new string[0]);
                            if (hardwareIDEntry is string[])
                            {
                                hardwareIDs.AddRange((string[])hardwareIDEntry);
                            }

                            object compatibleIDsEntry = instanceKey.GetValue("CompatibleIDs", new string[0]);
                            if (compatibleIDsEntry is string[])
                            {
                                hardwareIDs.AddRange((string[])compatibleIDsEntry);
                            }

                            if (StringUtils.ContainsCaseInsensitive(hardwareIDs, hardwareID))
                            {
                                deviceID         = RegistryKeyUtils.GetShortKeyName(deviceKey.Name);
                                deviceInstanceID = RegistryKeyUtils.GetShortKeyName(instanceKey.Name);
                                // Irrelevant Note: if a device is present but not installed in Windows then ConfigFlags entry will not be present
                                // and it doesn't matter anyway because we don't care about how existing installation configure the device

                                // there are two reasons not to use DeviceDesc from the local machine:
                                // 1. on Windows 6.0+ (or just Windows PE?) the format is different and not compatible with Windows 5.x
                                // 2. If the hadrware is present but not installed, the DeviceDesc will be a generic description (e.g. 'Ethernet Controller')

                                Console.WriteLine("Found matching device: '" + deviceID + "'");
                                instanceKey.Close();
                                deviceKey.Close();
                                hiveKey.Close();
                                return(deviceInstanceID);
                            }

                            instanceKey.Close();
                        }
                    }
                    deviceKey.Close();
                }
            }

            hiveKey.Close();
            return(deviceInstanceID);
        }