public static bool InstallSetupPackage(string hardwareId, string infPath) { bool bSuccess = SetupApi.UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, hardwareId, infPath, SetupApi.INSTALLFLAG.FORCE, IntPtr.Zero); if (bSuccess) { InfWizardStatus.Log(CategoryType.InstallSetupPackage, StatusType.Success, "driver update completed"); UpdateDriver(hardwareId); return(true); } // UpdateDriverForPlugAndPlayDevices FAILED ECODE setupError = (ECODE)Marshal.GetLastWin32Error(); if (setupError != ECODE.NO_SUCH_DEVINST) { InfWizardStatus.Log(CategoryType.InstallSetupPackage, StatusType.Win32Error, "UpdateDriverForPlugAndPlayDevices failed"); return(false); } // UpdateDriverForPlugAndPlayDevices = NO_SUCH_DEVINST InfWizardStatus.Log(CategoryType.InstallSetupPackage, StatusType.Warning, "device not detected (copying driver files for next time device is plugged in)"); StringBuilder sbDestInfFilename = new StringBuilder(1024); uint requiredSize; bSuccess = SetupApi.SetupCopyOEMInf(infPath, null, SetupApi.SPOST.SPOST_PATH, 0, sbDestInfFilename, (uint)sbDestInfFilename.Capacity, out requiredSize, null); if (!bSuccess) { InfWizardStatus.Log(CategoryType.InstallSetupPackage, StatusType.Win32Error, "SetupCopyOEMInf failed"); } else { InfWizardStatus.Log(CategoryType.InstallSetupPackage, StatusType.Success, "copied inf to {0}", sbDestInfFilename.ToString()); } CheckRemoved(hardwareId); return(bSuccess); }
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()); } }