예제 #1
0
        private static IEnumerable <DriverDetails> LoadDrivers(string environment, DriverArchitecture architecture)
        {
            string driversRegistryPath = @"SYSTEM\CurrentControlSet\Control\Print\Environments\{0}\Drivers\Version-3";

            List <DriverDetails> drivers = new List <DriverDetails>();

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(string.Format(driversRegistryPath, environment)))
            {
                if (key != null)
                {
                    foreach (string driverName in key.GetSubKeyNames())
                    {
                        using (RegistryKey driverKey = key.OpenSubKey(driverName))
                        {
                            // The registry entry for the driver must have the DriverVersion.  If not,
                            // we will consider this entry NOT a driver...
                            if (driverKey?.GetValue("DriverVersion") != null)
                            {
                                DriverDetails driver = CreateDriverDetails(driverKey, driverName);
                                driver.Architecture = architecture;
                                drivers.Add(driver);
                            }
                        }
                    }
                }
            }
            return(drivers);
        }
예제 #2
0
        private void GetDriverProperties(DriverDetails driver, string driverSectionName)
        {
            foreach (string dirtyLine in _reader.GetSection(driverSectionName))
            {
                string   line          = ExpandVariables(dirtyLine);
                string[] parts         = line.Split('=');
                string   propertyName  = parts[0].Trim();
                string   propertyValue = parts[1].Trim();
                switch (propertyName)
                {
                case "PrintProcessor":
                    // The format of the line will be something like...
                    // PrintProcessor="HPCPP15,hpcpp15.dll".  We need to extract
                    // the "HPCPP15" part.
                    driver.PrintProcessor = propertyValue.Trim('"').Split(',')[0];
                    break;

                case "DriverFile":
                    driver.DriverFile = propertyValue;
                    break;

                case "ConfigFile":
                    driver.ConfigurationFile = propertyValue;
                    break;

                case "HelpFile":
                    driver.HelpFile = propertyValue;
                    break;

                case "LanguageMonitor":
                    driver.Monitor = propertyValue.Trim('"').Split(',')[0];
                    break;

                case "DataFile":
                    driver.DataFile = propertyValue;
                    break;

                case "DriverVer":
                    string[] versionParts = propertyValue.Split(',');
                    driver.DriverDate = versionParts[0];
                    driver.Version    = new DriverVersion(versionParts[1]);
                    break;

                case "Provider":
                    driver.Provider = propertyValue.Trim().Trim('"');
                    break;

                case "DataSection":
                    // This is for Xerox inf files, they have another section
                    // that provides this information.
                    GetDriverProperties(driver, propertyValue);
                    break;

                default:
                    // Default is intentionally left blank.
                    break;
                }
            }
        }
예제 #3
0
 /// <summary>
 /// Installs the specified driver.
 /// </summary>
 /// <param name="driver">The <see cref="DriverDetails" /> describing the driver to install.</param>
 /// <param name="forceInstall">if set to <c>true</c> install the driver even if it is already installed.</param>
 public static void Install(DriverDetails driver, bool forceInstall)
 {
     if (!forceInstall && IsInstalled(driver))
     {
         LogInfo($"Driver {driver} already installed.");
     }
     else
     {
         InstallDriver(driver);
     }
 }
예제 #4
0
        /// <summary>
        /// Determines whether the specified driver is installed.
        /// </summary>
        /// <param name="driver">The <see cref="DriverDetails" /> describing the driver to check.</param>
        /// <returns><c>true</c> if the specified driver is installed; otherwise, <c>false</c>.</returns>
        public static bool IsInstalled(DriverDetails driver)
        {
            bool driverFound = (from n in DriverController.GetInstalledPrintDrivers()
                                where n.Name.EqualsIgnoreCase(driver.Name) &&
                                n.Architecture == driver.Architecture &&
                                n.Version == driver.Version &&
                                Path.GetFileName(n.InfPath).EqualsIgnoreCase(Path.GetFileName(driver.InfPath))
                                select n).Any();

            bool processorFound = DriverController.GetInstalledPrintProcessors().Contains(driver.PrintProcessor, StringComparer.OrdinalIgnoreCase);

            return(driverFound && processorFound);
        }
예제 #5
0
        /// <summary>
        /// Upgrades the driver for the specified print queue.
        /// </summary>
        /// <param name="driver">The <see cref="DriverDetails" /> describing the driver to upgrade to.</param>
        /// <param name="queueName">The queue name.</param>
        /// <exception cref="ArgumentNullException"><paramref name="driver" /> is null.</exception>
        public static void Upgrade(DriverDetails driver, string queueName)
        {
            if (driver == null)
            {
                throw new ArgumentNullException(nameof(driver));
            }

            LogInfo($"Upgrading driver of {queueName} to {driver}");

            string printUICommand = $"/Xs /n \"{queueName}\" DriverName \"{driver.Name}\"";

            LogDebug($"Invoking PrintUI command: {printUICommand}");
            NativeMethods.InvokePrintUI(printUICommand);
            LogInfo($"Driver '{driver}' installed.");
        }
예제 #6
0
        private static void InstallDriver(DriverDetails driver)
        {
            if (string.IsNullOrEmpty(driver.Name))
            {
                throw new ArgumentException("Driver name must be provided.", nameof(driver));
            }

            if (string.IsNullOrEmpty(driver.InfPath))
            {
                throw new ArgumentException("Driver INF path must be provided.", nameof(driver));
            }

            LogInfo($"Installing driver {driver}");

            string architecture   = _printUIArchitectures[driver.Architecture];
            string printUICommand = $"/ia /f \"{driver.InfPath}\" /m \"{driver.Name}\" /h \"{architecture}\"";

            LogDebug($"Invoking PrintUI command: {printUICommand}");
            NativeMethods.InvokePrintUI(printUICommand);
            LogInfo($"Driver '{driver}' installed.");
        }
예제 #7
0
        /// <summary>
        /// Gets a list of <see cref="DriverDetails" /> from the specified INF section.
        /// </summary>
        /// <param name="sectionName">The INF section name.</param>
        /// <returns>A collection of <see cref="DriverDetails" /> from the INF section.</returns>
        public IEnumerable <DriverDetails> GetDrivers(string sectionName)
        {
            List <DriverDetails> drivers = new List <DriverDetails>();

            foreach (string line in _reader.GetSection(sectionName))
            {
                // Each line should be formatted as "driverName=installRule,hardwareID".
                // We only care about getting the driver name.
                string[] parts             = line.Split('=');
                string   driverName        = ExpandVariables(parts[0]).Trim().Trim('"');
                string   driverSectionName = parts[1].Split(',')[0].Trim();
                if (!drivers.Any(n => n.Name == driverName))
                {
                    DriverDetails driver = new DriverDetails();
                    driver.Name    = driverName;
                    driver.InfPath = _reader.FileLocation;
                    GetDriverProperties(driver, driverSectionName);
                    GetDriverProperties(driver, "Version");
                    drivers.Add(driver);
                }
            }
            return(drivers);
        }
예제 #8
0
        private static DriverDetails CreateDriverDetails(RegistryKey key, string driverName)
        {
            DriverDetails driver = new DriverDetails()
            {
                Name              = driverName,
                InfPath           = (string)key.GetValue("InfPath"),
                DriverFile        = (string)key.GetValue("Driver"),
                ConfigurationFile = (string)key.GetValue("Configuration File"),
                HelpFile          = (string)key.GetValue("Help File"),
                DataFile          = (string)key.GetValue("Data File"),
                PrintProcessor    = (string)key.GetValue("Print Processor"),
                Monitor           = (string)key.GetValue("Monitor"),
                Provider          = (string)key.GetValue("Provider")
            };

            if (key.GetValueKind("DriverVersion") == RegistryValueKind.Binary)
            {
                byte[] versionValue = (byte[])key.GetValue("DriverVersion");
                driver.Version = new DriverVersion(ParseRegistryDriverVersion(versionValue));
            }
            else
            {
                driver.Version = new DriverVersion((string)key.GetValue("DriverVersion"));
            }

            if (key.GetValueKind("DriverDate") == RegistryValueKind.Binary)
            {
                byte[] dateValue = (byte[])key.GetValue("DriverDate");
                driver.DriverDate = ParseRegistryDriverDate(dateValue);
            }
            else
            {
                driver.DriverDate = (string)key.GetValue("DriverDate");
            }

            return(driver);
        }
예제 #9
0
 /// <summary>
 /// Installs the specified driver.
 /// </summary>
 /// <param name="driver">The <see cref="DriverDetails" /> describing the driver to install.</param>
 public static void Install(DriverDetails driver)
 {
     Install(driver, false);
 }