Пример #1
0
 public void SetEnabled(Boolean enabled)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) {
         if (dis.IsInvalid)
         {
             throw new Win32Exception();
         }
         SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);
         if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd))
         {
             throw new Win32Exception();
         }
         SP_PROPCHANGE_PARAMS PropChangeParams = new SP_PROPCHANGE_PARAMS();
         PropChangeParams.ClassInstallHeader.cbSize          = Marshal.SizeOf(PropChangeParams.ClassInstallHeader);
         PropChangeParams.ClassInstallHeader.InstallFunction = UsbApi.DIF_PROPERTYCHANGE;
         PropChangeParams.Scope       = UsbApi.DICS_FLAG_GLOBAL; // or use DICS_FLAG_CONFIGSPECIFIC to limit to current HW profile
         PropChangeParams.HwProfile   = 0;                       //Current hardware profile
         PropChangeParams.StateChange = enabled ? UsbApi.DICS_ENABLE : UsbApi.DICS_DISABLE;
         if (!SetupApi.SetupDiSetClassInstallParams(dis, ref dd, ref PropChangeParams.ClassInstallHeader, Marshal.SizeOf(PropChangeParams)))
         {
             throw new Win32Exception();
         }
         if (!SetupApi.SetupDiCallClassInstaller(UsbApi.DIF_PROPERTYCHANGE, dis, ref dd))
         {
             throw new Win32Exception();
         }
     }
 }
Пример #2
0
 public Byte[] GetCustomProperty(String name)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) {
         if (dis.IsInvalid)
         {
             throw new Win32Exception();
         }
         SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);
         if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd))
         {
             return(null);
         }
         RegistryValueKind propertyType;
         byte[]            propBuffer = new byte[256];
         int requiredSize;
         if (!SetupApi.SetupDiGetCustomDeviceProperty(dis, ref dd, name, DICUSTOMDEVPROP.NONE, out propertyType, propBuffer, propBuffer.Length, out requiredSize))
         {
             return(null);
         }
         if (requiredSize > propBuffer.Length)
         {
             propBuffer = new Byte[requiredSize];
             if (!SetupApi.SetupDiGetCustomDeviceProperty(dis, ref dd, name, DICUSTOMDEVPROP.NONE, out propertyType, propBuffer, propBuffer.Length, out requiredSize))
             {
                 throw new Win32Exception();
             }
         }
         if (requiredSize < propBuffer.Length)
         {
             Array.Resize(ref propBuffer, requiredSize);
         }
         return(propBuffer);
     }
 }
Пример #3
0
 public void SetProperty(SPDRP property, Byte[] value)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) {
         if (dis.IsInvalid)
         {
             throw new Win32Exception();
         }
         SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);
         if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd))
         {
             throw new Win32Exception();
         }
         if (!SetupApi.SetupDiSetDeviceRegistryProperty(dis, ref dd, property, value, (uint)value.Length))
         {
             throw new Win32Exception();
         }
     }
 }
Пример #4
0
 public Boolean Uninstall()
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) {
         if (dis.IsInvalid)
         {
             throw new Win32Exception();
         }
         SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);
         if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd))
         {
             throw new Win32Exception();
         }
         Boolean needsReboot;
         if (!SetupApi.DiUninstallDevice(IntPtr.Zero, dis, ref dd, 0, out needsReboot))
         {
             throw new Win32Exception();
         }
         return(needsReboot);
     }
 }
Пример #5
0
 public RegistryKey OpenRegistryKey(UInt32 scope, UInt32 hwProfile, UInt32 keyType, UInt32 samDesired)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) {
         if (dis.IsInvalid)
         {
             throw new Win32Exception();
         }
         SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);
         if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd))
         {
             return(null);
         }
         IntPtr handle = SetupApi.SetupDiOpenDevRegKey(dis, ref dd, scope, hwProfile, keyType, samDesired);
         if (handle == (IntPtr)(-1))
         {
             return(null);
         }
         return(RegistryKeyFromHandle(handle, true, (samDesired & (0x00000002 | 0x00000004 | 0x00000020)) != 0));
     }
 }
Пример #6
0
 public Byte[] GetDeviceProperty(Guid fmtid, UInt32 pid)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, DeviceID, IntPtr.Zero, DICFG.DEVICEINTERFACE | DICFG.ALLCLASSES)) {
         if (dis.IsInvalid)
         {
             throw new Win32Exception();
         }
         SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);
         if (!SetupApi.SetupDiEnumDeviceInfo(dis, 0, ref dd))
         {
             return(null);
         }
         byte[]     propBuffer = new byte[256];
         UInt32     requiredSize;
         UInt32     propertyType;
         DEVPROPKEY propertyKey = new DEVPROPKEY()
         {
             fmtid = fmtid, pid = pid
         };
         if (!SetupApi.SetupDiGetDeviceProperty(dis, ref dd, ref propertyKey, out propertyType, propBuffer, (uint)propBuffer.Length, out requiredSize, 0))
         {
             return(null);
         }
         if (requiredSize > propBuffer.Length)
         {
             propBuffer = new Byte[requiredSize];
             if (!SetupApi.SetupDiGetDeviceProperty(dis, ref dd, ref propertyKey, out propertyType, propBuffer, (uint)propBuffer.Length, out requiredSize, 0))
             {
                 throw new Win32Exception();
             }
         }
         if (requiredSize < propBuffer.Length)
         {
             Array.Resize(ref propBuffer, (int)requiredSize);
         }
         return(propBuffer);
     }
 }
Пример #7
0
 public static IList <DeviceNode> GetDevices(String enumerator, Boolean present)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, enumerator, IntPtr.Zero, (present ? DICFG.PRESENT : 0) | DICFG.ALLCLASSES)) {
         return(GetDevicesInSet(dis));
     }
 }
Пример #8
0
 public static IList <DeviceNode> GetDevices(Guid classGuid)
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(ref classGuid, null, IntPtr.Zero, DICFG.PRESENT | DICFG.DEVICEINTERFACE)) {
         return(GetDevicesInSet(dis));
     }
 }
Пример #9
0
 public static IList <DeviceNode> GetDevices()
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, null, IntPtr.Zero, DICFG.PRESENT | DICFG.ALLCLASSES)) {
         return(GetDevicesInSet(dis));
     }
 }