/// <summary> /// Gets the matching drivers for a device. /// </summary> /// <param name="devInfoSet">The device info set.</param> /// <param name="devInfoData">The device info data.</param> /// <returns>An array of matching device drivers.</returns> private static DriverInstance[] GetMatchingDrivers( IntPtr devInfoSet, SP_DEVINFO_DATA devInfoData) { List <DriverInstance> result = null; SP_DRVINFO_DATA drvInfoData = new SP_DRVINFO_DATA(); drvInfoData.Size = Marshal.SizeOf(typeof(SP_DRVINFO_DATA)); if (Win32SetupApi.SetupDiBuildDriverInfoList(devInfoSet, ref devInfoData, Constants.SPDIT_COMPATDRIVER)) { result = new List <DriverInstance>(); for (int i = 0; Win32SetupApi.SetupDiEnumDriverInfo(devInfoSet, ref devInfoData, Constants.SPDIT_COMPATDRIVER, i, ref drvInfoData); i++) { result.Add(new DriverInstance { Description = drvInfoData.Description, Version = drvInfoData.DriverVersion, ManufacturerName = drvInfoData.MfgName, ProviderName = drvInfoData.ProviderName, DriverDate = drvInfoData.DriverDate.ToDateTime() }); } } return(result != null?result.ToArray() : new DriverInstance[] { }); }
public static SP_DRVINFO_DATA[] GetDrivers(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, SPDIT driverType = SPDIT.SPDIT_COMPATDRIVER) { var list = new List <SP_DRVINFO_DATA>(); var installParams = new SP_DEVINSTALL_PARAMS(); installParams.Initialize(); // Retrieve installation parameters for a device information set or a particular device information element. if (!NativeMethods.SetupDiGetDeviceInstallParams(deviceInfoSet, ref deviceInfoData, ref installParams)) { var error = new Win32Exception(Marshal.GetLastWin32Error()); // Return if failed return(list.ToArray()); } // Set the flags that tell SetupDiBuildDriverInfoList to include just currently installed drivers. installParams.FlagsEx |= DI_FLAGSEX_INSTALLEDDRIVER; // Set the flags that tell SetupDiBuildDriverInfoList to allow excluded drivers. installParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS; // Set the flags. if (!NativeMethods.SetupDiSetDeviceInstallParams(deviceInfoSet, ref deviceInfoData, ref installParams)) { // Return if failed return(list.ToArray()); } if (NativeMethods.SetupDiBuildDriverInfoList(deviceInfoSet, ref deviceInfoData, driverType)) { var item = new SP_DRVINFO_DATA(); item.Initialize(); for (int i = 0; NativeMethods.SetupDiEnumDriverInfo(deviceInfoSet, ref deviceInfoData, driverType, i, ref item); i++) { //Console.WriteLine("{0} {1} - {2}", drvInfo.ProviderName, drvInfo.Description, drvInfo.GetVersion()); list.Add(item); } } return(list.ToArray()); }
public void SetupDiEnumDriverInfoListTest() { Guid usbDeviceId = DeviceSetupClass.Net; using var deviceInfoSet = SetupDiCreateDeviceInfoList(&usbDeviceId, IntPtr.Zero); Assert.True(SetupDiBuildDriverInfoList(deviceInfoSet, (SP_DEVINFO_DATA *)null, DriverType.SPDIT_CLASSDRIVER)); uint i = 0; SP_DRVINFO_DATA driverInfoData = SP_DRVINFO_DATA.Create(); Collection <SP_DRVINFO_DATA> driverInfos = new Collection <SP_DRVINFO_DATA>(); while (SetupDiEnumDriverInfo(deviceInfoSet, null, DriverType.SPDIT_CLASSDRIVER, i++, ref driverInfoData)) { driverInfos.Add(driverInfoData); } // We should have enumerated at least one driver Assert.NotEmpty(driverInfos); var loopbackDrivers = driverInfos .Where(d => new string(d.Description).IndexOf("loopback", StringComparison.OrdinalIgnoreCase) >= 0).ToArray(); var loopbackDriver = Assert.Single(loopbackDrivers); Assert.Equal("Microsoft KM-TEST Loopback Adapter", new string(loopbackDriver.Description)); Assert.Equal(DriverType.SPDIT_CLASSDRIVER, loopbackDriver.DriverType); Assert.NotEqual(0u, loopbackDriver.DriverVersion); Assert.Equal("Microsoft", new string(loopbackDriver.MfgName)); Assert.Equal("Microsoft", new string(loopbackDriver.ProviderName)); }
public List <SP_DRVINFO_DATA> GetCompatibleDrivers(DeviceInfo devInfoFromTheSameSet) { List <SP_DRVINFO_DATA> result = new List <SP_DRVINFO_DATA>(); if (!SetupDiBuildDriverInfoList(_HardwareDeviceInfo, ref devInfoFromTheSameSet.DevinfoData, 2)) { throw new Exception("Cannot build driver list: error " + Marshal.GetLastWin32Error()); } for (int i = 0; ; i++) { SP_DRVINFO_DATA driver = new SP_DRVINFO_DATA(); driver.cbSize = Marshal.SizeOf(driver); if (!SetupDiEnumDriverInfoW(_HardwareDeviceInfo, ref devInfoFromTheSameSet.DevinfoData, 2 /* SPDIT_COMPATDRIVER */, i, ref driver)) { int Error = Marshal.GetLastWin32Error(); if (Error == 259) //ERROR_NO_MORE_ITEMS { break; } } result.Add(driver); } return(result); }
private static extern bool DiInstallDevice( IntPtr hParent, IntPtr lpInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_DRVINFO_DATA DriverInfoData, DiFlags Flags, ref bool NeedReboot);
public unsafe void SetupDiGetDriverInfoDetailTest() { Guid usbDeviceId = DeviceSetupClass.Net; using var deviceInfoSet = SetupDiCreateDeviceInfoList(&usbDeviceId, IntPtr.Zero); Assert.True(SetupDiBuildDriverInfoList(deviceInfoSet, (SP_DEVINFO_DATA *)null, DriverType.SPDIT_CLASSDRIVER)); uint i = 0; SP_DRVINFO_DATA driverInfoData = SP_DRVINFO_DATA.Create(); Collection <SP_DRVINFO_DATA> driverInfos = new Collection <SP_DRVINFO_DATA>(); while (SetupDiEnumDriverInfo(deviceInfoSet, null, DriverType.SPDIT_CLASSDRIVER, i++, ref driverInfoData)) { driverInfos.Add(driverInfoData); } // We should have enumerated at least one driver Assert.NotEmpty(driverInfos); var loopbackDrivers = driverInfos .Where(d => new string(d.Description).IndexOf("loopback", StringComparison.OrdinalIgnoreCase) >= 0).ToArray(); var loopbackDriver = Assert.Single(loopbackDrivers); byte[] buffer = new byte[0x1000]; fixed(byte *ptr = buffer) { var drvInfoDetailData = (SP_DRVINFO_DETAIL_DATA *)ptr; *drvInfoDetailData = SP_DRVINFO_DETAIL_DATA.Create(); if (!SetupDiGetDriverInfoDetail( deviceInfoSet, null, &loopbackDriver, ptr, buffer.Length, out int requiredSize)) { throw new Win32Exception(); } Assert.Equal(0, (int)drvInfoDetailData->CompatIDsLength); Assert.Equal(0x8, drvInfoDetailData->CompatIDsOffset); Assert.Equal("Microsoft KM-TEST Loopback Adapter", new string(drvInfoDetailData->DrvDescription)); Assert.NotEqual(0, drvInfoDetailData->InfDate.dwHighDateTime); Assert.NotEqual(0, drvInfoDetailData->InfDate.dwLowDateTime); Assert.Equal(@"C:\WINDOWS\INF\netloop.inf", new string(drvInfoDetailData->InfFileName), ignoreCase: true); Assert.Equal("kmloop.ndi", new string(drvInfoDetailData->SectionName), ignoreCase: true); Assert.Equal("*msloop", new string(drvInfoDetailData->HardwareID)); } }
public static SP_DRVINFO_DATA[] GetDrivers(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, SPDIT driverType = SPDIT.SPDIT_COMPATDRIVER) { var list = new List <SP_DRVINFO_DATA>(); if (NativeMethods.SetupDiBuildDriverInfoList(deviceInfoSet, ref deviceInfoData, driverType)) { var item = new SP_DRVINFO_DATA(); item.Initialize(); for (int i = 0; NativeMethods.SetupDiEnumDriverInfo(deviceInfoSet, ref deviceInfoData, driverType, i, ref item); i++) { //Console.WriteLine("{0} {1} - {2}", drvInfo.ProviderName, drvInfo.Description, drvInfo.GetVersion()); list.Add(item); } } return(list.ToArray()); }
/// <summary> /// 18-05-2012 - Code FAILS! /// </summary> /// <param name="pdevinfoset"></param> /// <param name="deviceinfodata"></param> /// <returns></returns> internal static String GetProviderName(IntPtr pdevinfoset, ref SP_DEVINFO_DATA deviceinfodata) { Int32 memIndex = 0; SP_DRVINFO_DATA drvData = new SP_DRVINFO_DATA(); drvData.cbSize = Marshal.SizeOf(typeof(SP_DRVINFO_DATA)); if (SetupDiEnumDriverInfo(pdevinfoset, IntPtr.Zero, (uint)SPDIT.CLASSDRIVER, memIndex, ref drvData)) // ref deviceinfodata { return(drvData.ProviderName); } Debug.WriteLine(new Win32Exception(Marshal.GetHRForLastWin32Error())); return(String.Empty); }
/// <summary> /// Gets a driver handle from matching. /// </summary> /// <param name="driverInstance">The driver instance.</param> /// <param name="deviceInfoSet">The device info set.</param> /// <param name="deviceInfoData">The device info data.</param> /// <param name="drvInfoData">The DRV info data.</param> private static void GetDriverHandleFromMatch( DriverInstance driverInstance, IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, out SP_DRVINFO_DATA drvInfoData) { drvInfoData = new SP_DRVINFO_DATA(); drvInfoData.Size = Marshal.SizeOf(typeof(SP_DRVINFO_DATA)); if (Win32SetupApi.SetupDiBuildDriverInfoList(deviceInfoSet, ref deviceInfoData, Constants.SPDIT_COMPATDRIVER)) { for (int i = 0; Win32SetupApi.SetupDiEnumDriverInfo(deviceInfoSet, ref deviceInfoData, Constants.SPDIT_COMPATDRIVER, i, ref drvInfoData); i++) { if (driverInstance.Description.Equals(drvInfoData.Description)) { return; } } } drvInfoData = new SP_DRVINFO_DATA(); return; }
private static string GetDriverInf(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfo) { // Get Currently Installed Driver. SP_DEVINSTALL_PARAMS deviceInstallParams = new SP_DEVINSTALL_PARAMS { cbSize = Marshal.SizeOf(typeof(SP_DEVINSTALL_PARAMS)), FlagsEx = DI_FLAGS.ALLOWEXCLUDEDDRVS | DI_FLAGS.INSTALLEDDRIVER }; if (!NativeMethods.SetupDiSetDeviceInstallParams(deviceInfoSet, ref deviceInfo, ref deviceInstallParams)) { throw new Win32Exception(); } if (!NativeMethods.SetupDiBuildDriverInfoList(deviceInfoSet, ref deviceInfo, SPDIT.COMPATDRIVER)) { throw new Win32Exception(); } try { if (Environment.Is64BitProcess) { SP_DRVINFO_DATA drvInfo = new SP_DRVINFO_DATA { cbSize = Marshal.SizeOf(typeof(SP_DRVINFO_DATA)) }; if (NativeMethods.SetupDiEnumDriverInfo(deviceInfoSet, ref deviceInfo, SPDIT.COMPATDRIVER, 0, ref drvInfo)) { SP_DRVINFO_DETAIL_DATA driverInfoDetailData = new SP_DRVINFO_DETAIL_DATA { cbSize = Marshal.SizeOf(typeof(SP_DRVINFO_DETAIL_DATA)) }; if (NativeMethods.SetupDiGetDriverInfoDetail(deviceInfoSet, ref deviceInfo, ref drvInfo, ref driverInfoDetailData, Marshal.SizeOf(driverInfoDetailData), out _) || Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER) { return(driverInfoDetailData.InfFileName); } else { throw new Win32Exception(); } } else if (Marshal.GetLastWin32Error() != ERROR_NO_MORE_ITEMS) { throw new Win32Exception(); } } else { SP_DRVINFO_DATA32 drvInfo = new SP_DRVINFO_DATA32 { cbSize = Marshal.SizeOf(typeof(SP_DRVINFO_DATA32)) }; if (NativeMethods.SetupDiEnumDriverInfo32(deviceInfoSet, ref deviceInfo, SPDIT.COMPATDRIVER, 0, ref drvInfo)) { SP_DRVINFO_DETAIL_DATA32 driverInfoDetailData = new SP_DRVINFO_DETAIL_DATA32 { cbSize = Marshal.SizeOf(typeof(SP_DRVINFO_DETAIL_DATA32)) }; if (NativeMethods.SetupDiGetDriverInfoDetail32(deviceInfoSet, ref deviceInfo, ref drvInfo, ref driverInfoDetailData, Marshal.SizeOf(driverInfoDetailData), out _) || Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER) { return(driverInfoDetailData.InfFileName); } else { throw new Win32Exception(); } } else if (Marshal.GetLastWin32Error() != ERROR_NO_MORE_ITEMS) { throw new Win32Exception(); } } } finally { NativeMethods.SetupDiDestroyDriverInfoList(deviceInfoSet, ref deviceInfo, SPDIT.COMPATDRIVER); } return(null); }
public void InstallSpecificDriverForDevice(DeviceInfo deviceInstanceFromThisSet, SP_DRVINFO_DATA driverFromThisSet, IntPtr parentWindowHandle) { if (!SetupDiSetSelectedDevice(_HardwareDeviceInfo, ref deviceInstanceFromThisSet.DevinfoData)) { throw new LastWin32ErrorException("Cannot select the device for driver installation."); } if (!SetupDiSetSelectedDriver(_HardwareDeviceInfo, ref deviceInstanceFromThisSet.DevinfoData, ref driverFromThisSet)) { throw new LastWin32ErrorException("Cannot select the driver for installation."); } int needRestart; if (!InstallSelectedDriver(parentWindowHandle, _HardwareDeviceInfo, IntPtr.Zero, false, out needRestart)) { throw new LastWin32ErrorException("Cannot install the selected driver"); } }
static extern bool SetupDiSetSelectedDriver(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_DRVINFO_DATA DriverInfoData);
static extern bool SetupDiEnumDriverInfoW(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, int driverType, int memberIndex, ref SP_DRVINFO_DATA driverInfoData);
public static extern bool SetupDiEnumDriverInfo(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, SPDIT DriverType, int MemberIndex, ref SP_DRVINFO_DATA DriverInfoData);
private static extern bool SetupDiEnumDriverInfo(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, uint DriverType, int MemberIndex, ref SP_DRVINFO_DATA DriverInfoData);