public static void InstallDriver( string infPath, NewDev.DIIRFLAG flags = NewDev.DIIRFLAG.ZERO) { bool reboot; Trace.WriteLine( "Installing driver: \'" + Path.GetFileName(infPath) + "\'" ); if (!NewDev.DiInstallDriver( IntPtr.Zero, infPath, flags, out reboot)) { Win32Error.Set("DiInstallDriver"); throw new Exception(Win32Error.GetFullErrMsg()); } Trace.WriteLine("Driver installed successfully"); }
public static void InstallDriver( string name, string infPath, out bool reboot, NewDev.DIIRFLAG flags = NewDev.DIIRFLAG.ZERO) { Trace.WriteLine("Add to driverstore " + infPath); int size = 0; bool success = SetupApi.SetupCopyOEMInf(infPath, "", SetupApi.SPOST.NONE, SetupApi.SP_COPY.NOOVERWRITE, IntPtr.Zero, 0, ref size, IntPtr.Zero); bool bcontinue = true; reboot = false; if (!success) { int error = Marshal.GetLastWin32Error(); Trace.WriteLine("Unable to update driver - code " + error.ToString()); if ((error == 0) || (error == 0x50)) { // We still try to install the driver even the OEMInf file has already been copied, // For the case of install driver not finished, but system shutdown Trace.WriteLine("OEMInf file already copied"); } else { bcontinue = false; // Other error, does not continue } } if (!bcontinue) { return; } Trace.WriteLine( "Installing driver: \'" + Path.GetFileName(infPath) + "\'" ); NewDev.DiInstallDriver( IntPtr.Zero, infPath, flags, out reboot ); Win32Error.Set("DiInstallDriver"); if (Win32Error.GetErrorNo() == WinError.ERROR_SUCCESS) { Trace.WriteLine("Driver installed successfully"); } else if (Win32Error.GetErrorNo() == WinError.ERROR_NO_MORE_ITEMS) // DiInstallDriver() returns ERROR_NO_MORE_ITEMS when the // hardware ID in the inf file is found, but the specified // driver is not a better match than the current one and // DIIRFLAG_FORCE_INF is not used { Trace.WriteLine( "Driver not installed; newer driver already present" ); } else { throw new Exception(Win32Error.GetFullErrMsg()); } }