static NativePlugAndPlayHelper.DeviceInfoData[] GetDeviceInfoData(NativePlugAndPlayHelper.SafeDeviceInfoSetHandle handle) { List <NativePlugAndPlayHelper.DeviceInfoData> data = new List <NativePlugAndPlayHelper.DeviceInfoData>(); NativePlugAndPlayHelper.DeviceInfoData did = new NativePlugAndPlayHelper.DeviceInfoData(); int didSize = Marshal.SizeOf(did); did.Size = didSize; int index = 0; while (NativePlugAndPlayHelper.SetupDiEnumDeviceInfo(handle, index, ref did)) { data.Add(did); index += 1; did = new NativePlugAndPlayHelper.DeviceInfoData(); did.Size = didSize; } return(data.ToArray()); }
// enable/disable... static void EnableDevice(NativePlugAndPlayHelper.SafeDeviceInfoSetHandle handle, NativePlugAndPlayHelper.DeviceInfoData diData, bool enable) { NativePlugAndPlayHelper.PropertyChangeParameters pars = new NativePlugAndPlayHelper.PropertyChangeParameters(); // The size is just the size of the header, but we've flattened the structure. // The header comprises the first two fields, both integer. pars.Size = 8; pars.DiFunction = NativePlugAndPlayHelper.DiFunction.PropertyChange; pars.Scope = NativePlugAndPlayHelper.Scopes.Global; if (enable) { pars.StateChange = NativePlugAndPlayHelper.StateChangeAction.Enable; } else { pars.StateChange = NativePlugAndPlayHelper.StateChangeAction.Disable; } bool result = NativePlugAndPlayHelper.SetupDiSetClassInstallParams(handle, ref diData, ref pars, Marshal.SizeOf(pars)); if (result == false) { throw new Win32Exception(); } result = NativePlugAndPlayHelper.SetupDiCallClassInstaller(NativePlugAndPlayHelper.DiFunction.PropertyChange, handle, ref diData); if (result == false) { int err = Marshal.GetLastWin32Error(); if (err == (int)NativePlugAndPlayHelper.SetupApiError.NotDisableable) { throw new ArgumentException("Device can't be disabled (programmatically or in Device Manager)."); } else if (err >= (int)NativePlugAndPlayHelper.SetupApiError.NoAssociatedClass && err <= (int)NativePlugAndPlayHelper.SetupApiError.OnlyValidateViaAuthenticode) { throw new Win32Exception("SetupAPI error: " + ((NativePlugAndPlayHelper.SetupApiError)err).ToString()); } else { throw new Win32Exception(); } } }