public static extern bool SetupDiGetDeviceInstanceId(
     IntPtr DeviceInfoSet,
     ref DeviceInfoData did,
     [MarshalAs(UnmanagedType.LPTStr)] StringBuilder DeviceInstanceId,
     int DeviceInstanceIdSize,
     out int RequiredSize
     );
 public static extern bool SetupDiEnumDeviceInfo(SafeDeviceInfoSetHandle deviceInfoSet, int memberIndex,
                                                 ref DeviceInfoData deviceInfoData);
 private static DeviceInfoData[] GetDeviceInfoData(SafeDeviceInfoSetHandle handle)
 {
     var data = new List<DeviceInfoData>();
     var did = new DeviceInfoData();
     var didSize = Marshal.SizeOf(did);
     did.Size = didSize;
     var index = 0;
     while (NativeMethods.SetupDiEnumDeviceInfo(handle, index, ref did))
     {
         data.Add(did);
         index += 1;
         did = new DeviceInfoData();
         did.Size = didSize;
     }
     return data.ToArray();
 }
 // Find the index of the particular DeviceInfoData for the instanceId.
 private static int GetIndexOfInstance(SafeDeviceInfoSetHandle handle, DeviceInfoData[] diData, string instanceId)
 {
     const int ERROR_INSUFFICIENT_BUFFER = 122;
     for (var index = 0; index <= diData.Length - 1; index++)
     {
         var sb = new StringBuilder(1);
         var requiredSize = 0;
         var result = NativeMethods.SetupDiGetDeviceInstanceId(handle.DangerousGetHandle(), ref diData[index],
                                                                sb, sb.Capacity, out requiredSize);
         if (result == false && Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
         {
             sb.Capacity = requiredSize;
             result = NativeMethods.SetupDiGetDeviceInstanceId(handle.DangerousGetHandle(), ref diData[index], sb,
                                                               sb.Capacity, out requiredSize);
         }
         if (result == false)
             throw new Win32Exception();
         if (instanceId.Equals(sb.ToString()))
         {
             return index;
         }
     }
     // not found
     return -1;
 }
        // enable/disable...
        private static void EnableDevice(SafeDeviceInfoSetHandle handle, DeviceInfoData diData, bool enable)
        {
            var @params = new 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.
            @params.Size = 8;
            @params.DiFunction = DiFunction.PropertyChange;
            @params.Scope = Scopes.Global;
            @params.StateChange = enable ? StateChangeAction.Enable : StateChangeAction.Disable;

            var result = NativeMethods.SetupDiSetClassInstallParams(handle, ref diData, ref @params,
                                                                     Marshal.SizeOf(@params));
            if (result == false) throw new Win32Exception();
            result = NativeMethods.SetupDiCallClassInstaller(DiFunction.PropertyChange, handle, ref diData);
            if (result != false) return;
            var err = Marshal.GetLastWin32Error();
            if (err == (int) SetupApiError.NotDisableable)
                throw new ArgumentException("Device can't be disabled (programmatically or in Device Manager).");
            if (err >= (int) SetupApiError.NoAssociatedClass &&
                err <= (int) SetupApiError.OnlyValidateViaAuthenticode)
                throw new Win32Exception("SetupAPI error: " + ((SetupApiError) err).ToString());
            throw new Win32Exception();
        }