private SetupDiGetClassDevs ( System &classGuid, string enumerator, int hwndParent, int flags ) : |
||
classGuid | System | |
enumerator | string | |
hwndParent | int | |
flags | int | |
return |
private static IEnumerable <DeviceInfo> EnumerateDevices() { var devices = new List <DeviceInfo>(); var hidClass = HidClassGuid; var deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidClass, null, 0, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE); if (deviceInfoSet.ToInt64() != NativeMethods.INVALID_HANDLE_VALUE) { var deviceInfoData = CreateDeviceInfoData(); var deviceIndex = 0; while (NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, ref deviceInfoData)) { deviceIndex += 1; var deviceInterfaceData = new NativeMethods.SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); var deviceInterfaceIndex = 0; while (NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, ref deviceInfoData, ref hidClass, deviceInterfaceIndex, ref deviceInterfaceData)) { deviceInterfaceIndex++; var devicePath = GetDevicePath(deviceInfoSet, deviceInterfaceData); var description = GetBusReportedDeviceDescription(deviceInfoSet, ref deviceInfoData) ?? GetDeviceDescription(deviceInfoSet, ref deviceInfoData); devices.Add(new DeviceInfo { Path = devicePath, Description = description }); } } NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet); } return(devices); }
public static void reEnableDevice(string deviceInstanceId) { bool success; Guid hidGuid = new Guid(); NativeMethods.HidD_GetHidGuid(ref hidGuid); IntPtr deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidGuid, deviceInstanceId, 0, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE); NativeMethods.SP_DEVINFO_DATA deviceInfoData = new NativeMethods.SP_DEVINFO_DATA(); deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData); success = NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref deviceInfoData); if (!success) { throw new Exception("Error getting device info data, error code = " + Marshal.GetLastWin32Error()); } success = NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 1, ref deviceInfoData); // Checks that we have a unique device if (success) { throw new Exception("Can't find unique device"); } NativeMethods.SP_PROPCHANGE_PARAMS propChangeParams = new NativeMethods.SP_PROPCHANGE_PARAMS(); propChangeParams.classInstallHeader.cbSize = Marshal.SizeOf(propChangeParams.classInstallHeader); propChangeParams.classInstallHeader.installFunction = NativeMethods.DIF_PROPERTYCHANGE; propChangeParams.stateChange = NativeMethods.DICS_DISABLE; propChangeParams.scope = NativeMethods.DICS_FLAG_GLOBAL; propChangeParams.hwProfile = 0; success = NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref propChangeParams, Marshal.SizeOf(propChangeParams)); if (!success) { throw new Exception("Error setting class install params, error code = " + Marshal.GetLastWin32Error()); } success = NativeMethods.SetupDiCallClassInstaller(NativeMethods.DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData); if (!success) { throw new Exception("Error disabling device, error code = " + Marshal.GetLastWin32Error()); } propChangeParams.stateChange = NativeMethods.DICS_ENABLE; success = NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref propChangeParams, Marshal.SizeOf(propChangeParams)); if (!success) { throw new Exception("Error setting class install params, error code = " + Marshal.GetLastWin32Error()); } success = NativeMethods.SetupDiCallClassInstaller(NativeMethods.DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData); if (!success) { throw new Exception("Error enabling device, error code = " + Marshal.GetLastWin32Error()); } NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet); }
public static void reEnableDevice(string deviceInstanceId) { bool success; Guid hidGuid = new Guid(); NativeMethods.HidD_GetHidGuid(ref hidGuid); IntPtr deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidGuid, deviceInstanceId, 0, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE); NativeMethods.SP_DEVINFO_DATA deviceInfoData = new NativeMethods.SP_DEVINFO_DATA(); deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData); success = NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref deviceInfoData); if (!success) { throw new Exception("Error getting device info data, error code = " + Marshal.GetLastWin32Error()); } success = NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 1, ref deviceInfoData); // Checks that we have a unique device if (success) { throw new Exception("Can't find unique device"); } NativeMethods.SP_PROPCHANGE_PARAMS propChangeParams = new NativeMethods.SP_PROPCHANGE_PARAMS(); propChangeParams.classInstallHeader.cbSize = Marshal.SizeOf(propChangeParams.classInstallHeader); propChangeParams.classInstallHeader.installFunction = NativeMethods.DIF_PROPERTYCHANGE; propChangeParams.stateChange = NativeMethods.DICS_DISABLE; propChangeParams.scope = NativeMethods.DICS_FLAG_GLOBAL; propChangeParams.hwProfile = 0; success = NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref propChangeParams, Marshal.SizeOf(propChangeParams)); if (!success) { throw new Exception("Error setting class install params, error code = " + Marshal.GetLastWin32Error()); } success = NativeMethods.SetupDiCallClassInstaller(NativeMethods.DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData); // TEST: If previous SetupDiCallClassInstaller fails, just continue // otherwise device will likely get permanently disabled. /*if (!success) * { * throw new Exception("Error disabling device, error code = " + Marshal.GetLastWin32Error()); * } */ //System.Threading.Thread.Sleep(50); sw.Restart(); while (sw.ElapsedMilliseconds < 50) { // Use SpinWait to keep control of current thread. Using Sleep could potentially // cause other events to get run out of order System.Threading.Thread.SpinWait(100); } sw.Stop(); propChangeParams.stateChange = NativeMethods.DICS_ENABLE; success = NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref propChangeParams, Marshal.SizeOf(propChangeParams)); if (!success) { throw new Exception("Error setting class install params, error code = " + Marshal.GetLastWin32Error()); } success = NativeMethods.SetupDiCallClassInstaller(NativeMethods.DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData); if (!success) { throw new Exception("Error enabling device, error code = " + Marshal.GetLastWin32Error()); } //System.Threading.Thread.Sleep(50); sw.Restart(); while (sw.ElapsedMilliseconds < 50) { // Use SpinWait to keep control of current thread. Using Sleep could potentially // cause other events to get run out of order System.Threading.Thread.SpinWait(100); } sw.Stop(); NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet); }