mattercontrol/PrinterDriverInstaller/InfInstaller.cs

83 lines
2.5 KiB
C#
Raw Normal View History

using System;
2014-01-29 19:09:30 -08:00
using System.Diagnostics;
2018-01-03 10:46:39 -08:00
using System.Globalization;
2015-04-08 15:20:10 -07:00
using System.IO;
using System.Runtime.InteropServices;
2018-01-03 10:46:39 -08:00
using System.Threading;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.InfInstaller
{
2015-04-08 15:20:10 -07:00
public class InfInstallerApp
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
public InfInstallerApp()
{
#if DEBUG
2015-04-08 15:20:10 -07:00
//Debugger.Launch();
#endif
2015-04-08 15:20:10 -07:00
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
public void InstallInfDriverFile(string pathAndDriverToInstall)
{
Process driverInstallerProcess = new Process();
driverInstallerProcess.StartInfo.Arguments = string.Format("-a \"{0}\"", Path.GetFullPath(pathAndDriverToInstall));
2015-04-08 15:20:10 -07:00
driverInstallerProcess.StartInfo.CreateNoWindow = true;
driverInstallerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
2015-04-08 15:20:10 -07:00
string pnpUtilFileName = "PnPUtil.exe";
2015-04-08 15:20:10 -07:00
string pnPUtilPathAndFileName = Path.Combine("C:/Windows/winsxs/amd64_microsoft-windows-pnputil_31bf3856ad364e35_6.1.7600.16385_none_5958b438d6388d15", pnpUtilFileName);
bool fileExists = File.Exists(pnPUtilPathAndFileName);
2015-04-08 15:20:10 -07:00
if (!fileExists)
{
// Disable redirection
IntPtr ptr = new IntPtr();
bool isWow64FsRedirectionDisabled = Wow64DisableWow64FsRedirection(ref ptr);
if (isWow64FsRedirectionDisabled)
{
pnPUtilPathAndFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), pnpUtilFileName);
}
}
2014-11-18 16:24:43 -08:00
2015-04-08 15:20:10 -07:00
driverInstallerProcess.StartInfo.FileName = pnPUtilPathAndFileName;
driverInstallerProcess.StartInfo.Verb = "runas";
driverInstallerProcess.StartInfo.UseShellExecute = false;
2015-04-08 15:20:10 -07:00
driverInstallerProcess.Start();
driverInstallerProcess.WaitForExit();
2015-04-08 15:20:10 -07:00
if (!fileExists)
{
// Restore redirection
IntPtr ptr = new IntPtr();
Wow64RevertWow64FsRedirection(ptr);
}
}
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
[STAThread]
public static void Main(string[] args)
{
2018-01-03 10:46:39 -08:00
// this sets the global culture for the app and all new threads
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
2018-01-24 09:04:33 -08:00
// and make sure the app is set correctly
2018-01-03 10:46:39 -08:00
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
2015-04-08 15:20:10 -07:00
if (args.Length > 0 && File.Exists(args[0]))
{
InfInstallerApp driverInstaller = new InfInstallerApp();
driverInstaller.InstallInfDriverFile(args[0]);
}
}
}
}