public void SetupDiGetDeviceInstallParamsTest() { using var deviceInfoSet = SetupDiCreateDeviceInfoList((Guid *)null, IntPtr.Zero); SP_DEVINFO_DATA deviceInfoData = SP_DEVINFO_DATA.Create(); // If DeviceInstanceId is NULL or references a zero-length string, SetupDiOpenDeviceInfo adds a device information element // to the supplied device information set, if one does not already exist, for the root device in the device tree. string deviceId = null; if (!SetupDiOpenDeviceInfo(deviceInfoSet, deviceId, IntPtr.Zero, SetupDiOpenDeviceInfoFlags.None, ref deviceInfoData)) { throw new Win32Exception(); } SP_DEVINSTALL_PARAMS deviceInstallParams = SP_DEVINSTALL_PARAMS.Create(); if (!SetupDiGetDeviceInstallParams(deviceInfoSet, deviceInfoData, ref deviceInstallParams)) { throw new Win32Exception(); } deviceInfoData = SP_DEVINFO_DATA.Create(); Assert.False(SetupDiGetDeviceInstallParams(deviceInfoSet, deviceInfoData, ref deviceInstallParams)); }
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 static extern bool SetupDiGetDeviceInstallParams( IntPtr hDevInfo, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_DEVINSTALL_PARAMS DeviceInstallParams );
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); }