private bool ResetDevice(IntPtr hDevInfo, Native.SP_DEVINFO_DATA devInfoData) { Native.SP_PROPCHANGE_PARAMS pcp = new Native.SP_PROPCHANGE_PARAMS { ClassInstallHeader = { cbSize = Marshal.SizeOf(typeof(Native.SP_CLASSINSTALL_HEADER)), InstallFunction = Native.DIF_PROPERTYCHANGE }, StateChange = Native.DICS_PROPCHANGE, Scope = Native.DICS_FLAG_CONFIGSPECIFIC, HwProfile = 0 }; int szOfPcp = Marshal.SizeOf(pcp); IntPtr ptrToPcp = Marshal.AllocHGlobal(szOfPcp); Marshal.StructureToPtr(pcp, ptrToPcp, true); int szDevInfoData = Marshal.SizeOf(devInfoData); IntPtr ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData); Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true); bool rslt1 = Native.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Native.SP_PROPCHANGE_PARAMS))); bool rstl2 = Native.SetupDiCallClassInstaller(Native.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData); if (rslt1 && rstl2) { return(true); } return(false); }
public bool ResetDevice(string match) { bool retour = false; try { Guid myGUID = Guid.Empty; IntPtr hDevInfo = Native.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, Native.DIGCF_ALLCLASSES | Native.DIGCF_PRESENT); if (hDevInfo.ToInt32() == Native.INVALID_HANDLE_VALUE) { return(false); } Native.SP_DEVINFO_DATA DeviceInfoData = new Native.SP_DEVINFO_DATA(); DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); //is devices exist for class DeviceInfoData.devInst = 0; DeviceInfoData.classGuid = Guid.Empty; DeviceInfoData.reserved = UIntPtr.Zero; UInt32 i; StringBuilder deviceName = new StringBuilder("") { Capacity = Native.MAX_DEV_LEN }; for (i = 0; Native.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++) { Native.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Native.SPDRP_DEVICEDESC, 0, deviceName, Native.MAX_DEV_LEN, IntPtr.Zero); if (deviceName.ToString().Trim().ToLower().Equals(match.Trim().ToLower())) { retour = ResetDevice(hDevInfo, DeviceInfoData); break; } } Native.SetupDiDestroyDeviceInfoList(hDevInfo); } catch (Exception ex) { throw new Exception("Failed to enumerate device tree!", ex); } return(retour); }