示例#1
0
        public static bool ChangePropertyDevice(Guid guidClass, string deviceInstanceId, DiChangeState changeState)
        {
            IntPtr deviceInfoList = IntPtr.Zero;

            try
            {
                SP_DEVICE_INFO_DATA deviceInfoData = new SP_DEVICE_INFO_DATA();
                deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);

                //Get device information
                deviceInfoList = SetupDiGetClassDevs(guidClass, deviceInstanceId, IntPtr.Zero, DiGetClassFlag.DIGCF_DEVICEINTERFACE);
                if (!SetupDiEnumDeviceInfo(deviceInfoList, 0, ref deviceInfoData))
                {
                    Debug.WriteLine("SetupDi: Failed getting device info.");
                    return(false);
                }

                //Set property change param
                SP_PROPCHANGE_PARAMS propertyParams = new SP_PROPCHANGE_PARAMS();
                propertyParams.classInstallHeader.cbSize          = Marshal.SizeOf(propertyParams.classInstallHeader);
                propertyParams.classInstallHeader.installFunction = DiFunction.DIF_PROPERTYCHANGE;
                propertyParams.changeStateFlag = DiChangeStateFlag.DICS_FLAG_GLOBAL;
                propertyParams.stateChange     = changeState;

                //Prepare the device
                if (!SetupDiSetClassInstallParams(deviceInfoList, ref deviceInfoData, ref propertyParams, Marshal.SizeOf(propertyParams)))
                {
                    Debug.WriteLine("SetupDi: Failed to set install params.");
                    return(false);
                }

                //Change the property
                if (!SetupDiCallClassInstaller(DiFunction.DIF_PROPERTYCHANGE, deviceInfoList, ref deviceInfoData))
                {
                    Debug.WriteLine("SetupDi: Failed to change property.");
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to change property: " + ex.Message);
                return(false);
            }
            finally
            {
                if (deviceInfoList != IntPtr.Zero)
                {
                    SetupDiDestroyDeviceInfoList(deviceInfoList);
                }
            }
        }
示例#2
0
        public static bool DeviceCreateNode(string className, Guid classGuid, string propertyNode)
        {
            IntPtr deviceInfoList = IntPtr.Zero;

            try
            {
                SP_DEVICE_INFO_DATA deviceInfoData = new SP_DEVICE_INFO_DATA();
                deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);
                deviceInfoList        = SetupDiCreateDeviceInfoList(ref classGuid, IntPtr.Zero);
                if (deviceInfoList == IntPtr.Zero)
                {
                    return(false);
                }

                if (!SetupDiCreateDeviceInfo(deviceInfoList, className, ref classGuid, null, IntPtr.Zero, DiCreateDevice.DICD_GENERATE_ID, ref deviceInfoData))
                {
                    return(false);
                }

                if (!SetupDiSetDeviceRegistryProperty(deviceInfoList, ref deviceInfoData, DiDeviceRegistryProperty.SPDRP_HARDWAREID, propertyNode, propertyNode.Length * 2))
                {
                    return(false);
                }

                if (!SetupDiCallClassInstaller(DiFunction.DIF_REGISTERDEVICE, deviceInfoList, ref deviceInfoData))
                {
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to create device node: " + ex.Message);
                return(false);
            }
            finally
            {
                if (deviceInfoList != IntPtr.Zero)
                {
                    SetupDiDestroyDeviceInfoList(deviceInfoList);
                }
            }
        }
示例#3
0
        public static bool DeviceRemove(Guid classGuid, string instanceId)
        {
            IntPtr deviceInfoList = IntPtr.Zero;

            try
            {
                SP_DEVICE_INFO_DATA deviceInfoData = new SP_DEVICE_INFO_DATA();
                deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);

                //Get device information
                deviceInfoList = SetupDiGetClassDevs(classGuid, null, IntPtr.Zero, DiGetClassFlag.DIGCF_DEVICEINTERFACE);
                if (!SetupDiOpenDeviceInfo(deviceInfoList, instanceId, IntPtr.Zero, 0, ref deviceInfoData))
                {
                    Debug.WriteLine("SetupDi: Failed getting device info.");
                    return(false);
                }

                SP_REMOVEDEVICE_PARAMS removeParams = new SP_REMOVEDEVICE_PARAMS();
                removeParams.classInstallHeader                 = new SP_CLASSINSTALL_HEADER();
                removeParams.classInstallHeader.cbSize          = Marshal.SizeOf(removeParams.classInstallHeader);
                removeParams.classInstallHeader.installFunction = DiFunction.DIF_REMOVE;
                removeParams.removeDevice = DiRemoveDevice.DI_REMOVEDEVICE_GLOBAL;

                if (SetupDiSetClassInstallParams(deviceInfoList, ref deviceInfoData, ref removeParams, Marshal.SizeOf(removeParams)))
                {
                    return(SetupDiCallClassInstaller(DiFunction.DIF_REMOVE, deviceInfoList, ref deviceInfoData));
                }

                return(false);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to remove device: " + ex.Message);
                return(false);
            }
            finally
            {
                if (deviceInfoList != IntPtr.Zero)
                {
                    SetupDiDestroyDeviceInfoList(deviceInfoList);
                }
            }
        }
示例#4
0
        private static string GetBusReportedDeviceDescription(IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA devinfoData)
        {
            try
            {
                byte[] descriptionBuffer = new byte[1024];
                ulong  propertyType      = 0;
                int    requiredSize      = 0;

                if (SetupDiGetDeviceProperty(deviceInfoList, ref devinfoData, ref DEVPKEY_Device_BusReportedDeviceDesc, ref propertyType, descriptionBuffer, descriptionBuffer.Length, ref requiredSize, 0))
                {
                    return(descriptionBuffer.ToUTF16String());
                }
                else
                {
                    return(string.Empty);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to get bus device description: " + ex.Message);
                return(string.Empty);
            }
        }
示例#5
0
        private static string GetDeviceDescription(IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA devinfoData)
        {
            try
            {
                byte[] descriptionBuffer = new byte[1024];
                int    propertyType      = 0;
                int    requiredSize      = 0;

                if (SetupDiGetDeviceRegistryProperty(deviceInfoList, ref devinfoData, DiDeviceRegistryProperty.SPDRP_DEVICEDESC, ref propertyType, descriptionBuffer, descriptionBuffer.Length, ref requiredSize))
                {
                    return(descriptionBuffer.ToUTF8String());
                }
                else
                {
                    return(string.Empty);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to get device description: " + ex.Message);
                return(string.Empty);
            }
        }
示例#6
0
        private static string GetDeviceHardwareId(IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA devinfoData)
        {
            try
            {
                byte[] hardwareBuffer = new byte[1024];
                int    propertyType   = 0;
                int    requiredSize   = 0;

                if (SetupDiGetDeviceRegistryProperty(deviceInfoList, ref devinfoData, DiDeviceRegistryProperty.SPDRP_HARDWAREID, ref propertyType, hardwareBuffer, hardwareBuffer.Length, ref requiredSize))
                {
                    return(hardwareBuffer.ToUTF8String());
                }
                else
                {
                    Debug.WriteLine("Failed to get hardware id.");
                    return(string.Empty);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to get hardware id: " + ex.Message);
                return(string.Empty);
            }
        }
示例#7
0
 public static extern bool SetupDiSetDeviceRegistryProperty(IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA DeviceInfoData, DiDeviceRegistryProperty Property, [MarshalAs(UnmanagedType.LPWStr)] string PropertyBuffer, int PropertyBufferSize);
示例#8
0
 public static extern bool SetupDiCallClassInstaller(DiFunction installFunction, IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA deviceInfoData);
示例#9
0
 public static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA DeviceInterfaceData, ref SP_REMOVEDEVICE_PARAMS ClassInstallParams, int ClassInstallParamsSize);
示例#10
0
 public static extern bool SetupDiOpenDeviceInfo(IntPtr deviceInfoList, string DeviceInstanceId, IntPtr hWndParent, DiOpenDevice flags, ref SP_DEVICE_INFO_DATA deviceInfoData);
示例#11
0
 public static extern bool SetupDiEnumDeviceInterfaces(IntPtr deviceInfoList, SP_DEVICE_INFO_DATA deviceInfoData, Guid interfaceClassGuid, int memberIndex, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
示例#12
0
 public static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoList, int memberIndex, ref SP_DEVICE_INFO_DATA deviceInfoData);
示例#13
0
 public static extern bool SetupDiGetDeviceProperty(IntPtr deviceInfo, ref SP_DEVICE_INFO_DATA deviceInfoData, ref DEVPROPKEY propKey, ref ulong propertyDataType, byte[] propertyBuffer, int propertyBufferSize, ref int requiredSize, uint flags);
示例#14
0
 public static extern bool SetupDiGetDeviceRegistryProperty(IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA deviceInfoData, DiDeviceRegistryProperty propertyVal, ref int propertyRegDataType, byte[] propertyBuffer, int propertyBufferSize, ref int requiredSize);
示例#15
0
 public static extern bool SetupDiCreateDeviceInfo(IntPtr deviceInfoList, string DeviceName, ref Guid ClassGuid, string DeviceDescription, IntPtr hWndParent, DiCreateDevice flags, ref SP_DEVICE_INFO_DATA deviceInfoData);
示例#16
0
        public static List <EnumerateInfo> EnumerateDevicesDi(Guid enumerateGuid, bool isPresent)
        {
            IntPtr deviceInfoList = IntPtr.Zero;
            List <EnumerateInfo> enumeratedInfoList = new List <EnumerateInfo>();

            try
            {
                int deviceIndexInfo = 0;
                SP_DEVICE_INFO_DATA deviceInfoData = new SP_DEVICE_INFO_DATA();
                deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);

                //Get device information
                if (isPresent)
                {
                    deviceInfoList = SetupDiGetClassDevs(enumerateGuid, null, IntPtr.Zero, DiGetClassFlag.DIGCF_PRESENT | DiGetClassFlag.DIGCF_DEVICEINTERFACE);
                }
                else
                {
                    deviceInfoList = SetupDiGetClassDevs(enumerateGuid, null, IntPtr.Zero, DiGetClassFlag.DIGCF_DEVICEINTERFACE);
                }

                while (SetupDiEnumDeviceInfo(deviceInfoList, deviceIndexInfo, ref deviceInfoData))
                {
                    try
                    {
                        deviceIndexInfo++;
                        int deviceIndexInterfaces = 0;
                        SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
                        deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);

                        while (SetupDiEnumDeviceInterfaces(deviceInfoList, deviceInfoData, enumerateGuid, deviceIndexInterfaces, ref deviceInterfaceData))
                        {
                            try
                            {
                                deviceIndexInterfaces++;
                                string devicePath  = GetDevicePath(deviceInfoList, deviceInterfaceData);
                                string description = GetBusReportedDeviceDescription(deviceInfoList, ref deviceInfoData);
                                if (string.IsNullOrWhiteSpace(description))
                                {
                                    description = GetDeviceDescription(deviceInfoList, ref deviceInfoData);
                                }
                                string hardwareId = GetDeviceHardwareId(deviceInfoList, ref deviceInfoData);
                                string modelId    = GetDeviceModelId(devicePath);

                                EnumerateInfo foundDevice = new EnumerateInfo();
                                foundDevice.DevicePath  = devicePath;
                                foundDevice.Description = description;
                                foundDevice.HardwareId  = hardwareId;
                                foundDevice.ModelId     = modelId;
                                enumeratedInfoList.Add(foundDevice);
                            }
                            catch { }
                        }
                    }
                    catch { }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed enumerating devices di: " + ex.Message);
            }
            finally
            {
                if (deviceInfoList != IntPtr.Zero)
                {
                    SetupDiDestroyDeviceInfoList(deviceInfoList);
                }
            }
            return(enumeratedInfoList);
        }
示例#17
0
 public static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA deviceInfoData, ref SP_PROPCHANGE_PARAMS classInstallParams, int classInstallParamsSize);