示例#1
0
        public static IEnumerable<DeviceInterfaceData> SetupDiEnumDeviceInterfaces(
            SafeDeviceInfoSetHandle lpDeviceInfoSet,
            DeviceInfoData deviceInfoData,
            Guid interfaceClassGuid)
        {
            uint index = 0;
            while (true)
            {
                var data = DeviceInterfaceData.Create();

                var result = SetupDiEnumDeviceInterfaces(
                    lpDeviceInfoSet,
                    deviceInfoData,
                    ref interfaceClassGuid,
                    index,
                    ref data);

                if (!result)
                {
                    var lastError = GetLastError();
                    if (lastError != Win32ErrorCode.ERROR_NO_MORE_ITEMS)
                    {
                        throw new Win32Exception(lastError);
                    }

                    yield break;
                }

                yield return data;
                index++;
            }
        }
示例#2
0
        private static IEnumerable<SP_DEVICE_INTERFACE_DATA> SetupDiEnumDeviceInterfacesHelper(
            SafeDeviceInfoSetHandle lpDeviceInfoSet,
            SP_DEVINFO_DATA? deviceInfoData,
            Guid interfaceClassGuid)
        {
            int index = 0;
            while (true)
            {
                var data = SP_DEVICE_INTERFACE_DATA.Create();

                bool result = SetupDiEnumDeviceInterfaces(
                    lpDeviceInfoSet,
                    deviceInfoData,
                    ref interfaceClassGuid,
                    index,
                    ref data);

                if (!result)
                {
                    var lastError = GetLastError();
                    if (lastError != Win32ErrorCode.ERROR_NO_MORE_ITEMS)
                    {
                        throw new Win32Exception(lastError);
                    }

                    yield break;
                }

                yield return data;
                index++;
            }
        }
示例#3
0
        public static string SetupDiGetDeviceInterfaceDetail(
            SafeDeviceInfoSetHandle lpDeviceInfoSet,
            DeviceInterfaceData oInterfaceData,
            IntPtr lpDeviceInfoData)
        {
            var requiredSize = new NullableUInt32();

            // First call to get the size to allocate
            SetupDiGetDeviceInterfaceDetail(
                lpDeviceInfoSet,
                ref oInterfaceData,
                IntPtr.Zero,
                0,
                requiredSize,
                null);

            // As we passed an empty buffer we know that the function will fail, not need to check the result.
            var lastError = GetLastError();
            if (lastError != Win32ErrorCode.ERROR_INSUFFICIENT_BUFFER)
            {
                throw new Win32Exception(lastError);
            }

            var buffer = Marshal.AllocHGlobal((int)requiredSize.Value);

            try
            {
                Marshal.WriteInt32(buffer, DeviceInterfaceDetailDataSize.Value);

                // Second call to get the value
                var success = SetupDiGetDeviceInterfaceDetail(
                    lpDeviceInfoSet,
                    ref oInterfaceData,
                    buffer,
                    (uint)requiredSize,
                    null,
                    null);

                if (!success)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    return null;
                }

                var strPtr = new IntPtr(buffer.ToInt64() + 4);
                return Marshal.PtrToStringAuto(strPtr);
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
示例#4
0
 public static unsafe IEnumerable<SP_DEVICE_INTERFACE_DATA> SetupDiEnumDeviceInterfaces(
     SafeDeviceInfoSetHandle lpDeviceInfoSet,
     SP_DEVINFO_DATA* deviceInfoData,
     Guid interfaceClassGuid)
 {
     // Copy out the value of the struct pointed to (if any) so that
     // the caller does not need to remember to keep the pointer fixed
     // for the entire enumeration.
     var deviceInfoDataCopy = deviceInfoData != null ? (SP_DEVINFO_DATA?)*deviceInfoData : null;
     return SetupDiEnumDeviceInterfacesHelper(
         lpDeviceInfoSet,
         deviceInfoDataCopy,
         interfaceClassGuid);
 }
示例#5
0
        public static unsafe string SetupDiGetDeviceInterfaceDetail(
            SafeDeviceInfoSetHandle deviceInfoSet,
            SP_DEVICE_INTERFACE_DATA interfaceData,
            SP_DEVINFO_DATA* deviceInfoData)
        {
            int requiredSize;

            // First call to get the size to allocate
            SetupDiGetDeviceInterfaceDetail(
                deviceInfoSet,
                ref interfaceData,
                null,
                0,
                &requiredSize,
                deviceInfoData);

            // As we passed an empty buffer we know that the function will fail, not need to check the result.
            var lastError = GetLastError();
            if (lastError != Win32ErrorCode.ERROR_INSUFFICIENT_BUFFER)
            {
                throw new Win32Exception(lastError);
            }

            fixed (byte* pBuffer = new byte[requiredSize])
            {
                var pDetail = (SP_DEVICE_INTERFACE_DETAIL_DATA*)pBuffer;
                pDetail->cbSize = SP_DEVICE_INTERFACE_DETAIL_DATA.ReportableStructSize;

                // Second call to get the value
                var success = SetupDiGetDeviceInterfaceDetail(
                    deviceInfoSet,
                    ref interfaceData,
                    pDetail,
                    requiredSize,
                    null,
                    null);

                if (!success)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    return null;
                }

                return SP_DEVICE_INTERFACE_DETAIL_DATA.GetDevicePath(pDetail);
            }
        }
示例#6
0
            // enable/disable...
            private static void EnableDevice(SafeDeviceInfoSetHandle handle, DeviceInfoData diData, bool enable)
            {
                PropertyChangeParameters @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;
                if (enable)
                {
                    @params.StateChange = StateChangeAction.Enable;
                }
                else
                {
                    @params.StateChange = StateChangeAction.Disable;
                }

                bool 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)
                {
                    int err = Marshal.GetLastWin32Error();
                    if (err == (int)SetupApiError.NotDisableable)
                    {
                        throw new ArgumentException("Device can't be disabled (programmatically or in Device Manager).");
                    }
                    else if (err >= (int)SetupApiError.NoAssociatedClass && err <= (int)SetupApiError.OnlyValidateViaAuthenticode)
                    {
                        throw new Win32Exception("SetupAPI error: " + ((SetupApiError)err).ToString());
                    }
                    else
                    {
                        throw new Win32Exception();
                    }
                }
            }
示例#7
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);
     }
 }
示例#8
0
        private static IList <DeviceNode> GetDevicesInSet(SafeDeviceInfoSetHandle dis)
        {
            List <DeviceNode> list = new List <DeviceNode>();

            if (dis.IsInvalid)
            {
                throw new Win32Exception();
            }
            SP_DEVINFO_DATA dd = new SP_DEVINFO_DATA(true);

            for (int index = 0; ; index++)
            {
                if (!SetupApi.SetupDiEnumDeviceInfo(dis, index, ref dd))
                {
                    break;
                }
                list.Add(new DeviceNode(dd.DevInst));
            }
            return(list);
        }
示例#9
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));
     }
 }
示例#10
0
        private static void EnableDevice(SafeDeviceInfoSetHandle handle, DeviceInfoData diData, bool enable)
        {
            PropertyChangeParameters propertyChangeParameters = new PropertyChangeParameters
            {
                Size        = 8,
                DiFunction  = DiFunction.PropertyChange,
                Scope       = Scopes.Global,
                StateChange =
                    enable
                                                                            ? StateChangeAction.Enable
                                                                            : StateChangeAction.Disable
            };

            // Set the parameters
            bool result = Win32.SetupDiSetClassInstallParams(handle, ref diData, ref propertyChangeParameters,
                                                             Marshal.SizeOf(propertyChangeParameters));

            if (result == false)
            {
                throw new Win32Exception();
            }

            // Apply the parameters
            result = Win32.SetupDiCallClassInstaller(DiFunction.PropertyChange, handle, ref diData);
            if (result == false)
            {
                int 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));
                }
                throw new Win32Exception();
            }
        }
示例#11
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);
     }
 }
        public static void SetDeviceState(string instanceId, DICS state, DeviceClass?deviceClass = null)
        {
            SafeDeviceInfoSetHandle diSetHandle = null;

            try
            {
                GCHandle guid = new GCHandle();

                if (deviceClass.HasValue)
                {
                    var classGuid = Device.DeviceClassGuids[(int)deviceClass];
                    guid        = GCHandle.Alloc(classGuid, GCHandleType.Pinned);
                    diSetHandle = NativeMethods.SetupDiGetClassDevs(guid.AddrOfPinnedObject(), null, IntPtr.Zero, SetupDiGetClassDevsFlags.Null);
                }
                else
                {
                    diSetHandle = NativeMethods.SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, SetupDiGetClassDevsFlags.AllClasses);
                }

                if (diSetHandle.IsInvalid)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiGetClassDevs");
                }

                //Get the device information data for each matching device.
                var diData = GetDeviceInfoData(diSetHandle);

                if (!string.IsNullOrEmpty(instanceId))
                {
                    // Find the index of of the instance
                    int index = GetIndexOfInstance(diSetHandle, diData, instanceId);
                    if (index == -1)
                    {
                        throw new IndexOutOfRangeException(string.Format("The device '{0}' could not be found", instanceId));
                    }

                    diData = new SP_DEVINFO_DATA[] { diData[index] };
                }

                for (int i = 0; i < diData.Length; i++)
                {
                    SP_CLASSINSTALL_HEADER installHeader = default(SP_CLASSINSTALL_HEADER);
                    installHeader.InstallFunction = DIF.DIF_PROPERTYCHANGE;
                    installHeader.cbSize          = (uint)Marshal.SizeOf(installHeader);

                    SP_PROPCHANGE_PARAMS propertyChangeParameters = default(SP_PROPCHANGE_PARAMS);
                    propertyChangeParameters.ClassInstallHeader = installHeader;
                    propertyChangeParameters.HwProfile          = 0;
                    propertyChangeParameters.Scope       = DICS.DICS_FLAG_CONFIGSPECIFIC;
                    propertyChangeParameters.StateChange = state;
                    if (NativeMethods.SetupDiSetClassInstallParams(diSetHandle, ref diData[i], ref propertyChangeParameters, (uint)Marshal.SizeOf(propertyChangeParameters)))
                    {
                        if (!NativeMethods.SetupDiCallClassInstaller(DIF.DIF_PROPERTYCHANGE, diSetHandle, ref diData[i]))
                        {
                            throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiCallClassInstaller");
                        }
                    }
                }
            }
            finally
            {
                if (diSetHandle != null)
                {
                    if (diSetHandle.IsClosed == false)
                    {
                        diSetHandle.Close();
                    }
                    diSetHandle.Dispose();
                }
            }
        }
 public static extern bool SetupDiEnumDeviceInfo(SafeDeviceInfoSetHandle deviceInfoSet, int memberIndex,
                                                 ref DeviceInfoData deviceInfoData);
示例#14
0
 public static extern unsafe bool SetupDiEnumDeviceInfo(
     SafeDeviceInfoSetHandle deviceInfoSet,
     int memberIndex,
     SP_DEVINFO_DATA *deviceInfoData);
示例#15
0
 public static extern bool SetupDiEnumDeviceInfo(
     SafeDeviceInfoSetHandle deviceInfoSet,
     int memberIndex,
     SP_DEVINFO_DATA deviceInfoData);
示例#16
0
 public static unsafe extern bool SetupDiBuildDriverInfoList(
     SafeDeviceInfoSetHandle deviceInfoSet,
     [Friendly(FriendlyFlags.In | FriendlyFlags.Optional)] SP_DEVINFO_DATA *deviceInfoData,
     DriverType driverType);
示例#17
0
 public static unsafe extern bool SetupDiSetDeviceInstallParams(
     SafeDeviceInfoSetHandle deviceInfoSet,
     [Friendly(FriendlyFlags.In | FriendlyFlags.Optional)] SP_DEVINFO_DATA *deviceInfoData,
     [Friendly(FriendlyFlags.In)] SP_DEVINSTALL_PARAMS *deviceInstallParams);
示例#18
0
 public static unsafe extern bool SetupDiSetSelectedDevice(
     SafeDeviceInfoSetHandle deviceInfoSet,
     [Friendly(FriendlyFlags.In)] SP_DEVINFO_DATA *deviceInfoData);
示例#19
0
 public static unsafe extern bool SetupDiOpenDeviceInfo(
     SafeDeviceInfoSetHandle deviceInfoSet,
     string deviceInstanceId,
     IntPtr parent,
     SetupDiOpenDeviceInfoFlags openFlags,
     [Friendly(FriendlyFlags.Bidirectional)] SP_DEVINFO_DATA *deviceInfoData);
示例#20
0
 public static extern unsafe bool SetupDiEnumDeviceInfo(
     SafeDeviceInfoSetHandle deviceInfoSet,
     int memberIndex,
     [Friendly(FriendlyFlags.Bidirectional)] SP_DEVINFO_DATA *deviceInfoData);
        public static IEnumerable <Device> GetDevices(string instanceId = "", DeviceClass?deviceClass = null)
        {
            SafeDeviceInfoSetHandle diSetHandle = null;

            try
            {
                GCHandle guid = new GCHandle();

                if (deviceClass.HasValue)
                {
                    var classGuid = Device.DeviceClassGuids[(int)deviceClass];
                    guid        = GCHandle.Alloc(classGuid, GCHandleType.Pinned);
                    diSetHandle = NativeMethods.SetupDiGetClassDevs(guid.AddrOfPinnedObject(), null, IntPtr.Zero, SetupDiGetClassDevsFlags.Null);
                }
                else
                {
                    diSetHandle = NativeMethods.SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, SetupDiGetClassDevsFlags.AllClasses);
                }

                if (diSetHandle.IsInvalid)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiGetClassDevs");
                }

                //Get the device information data for each matching device.
                var diData = GetDeviceInfoData(diSetHandle);

                if (!string.IsNullOrEmpty(instanceId))
                {
                    // Find the index of of the instance
                    int index = GetIndexOfInstance(diSetHandle, diData, instanceId);
                    if (index == -1)
                    {
                        throw new IndexOutOfRangeException(string.Format("The device '{0}' could not be found", instanceId));
                    }

                    diData = new SP_DEVINFO_DATA[] { diData[index] };
                }

                for (int i = 0; i < diData.Length; i++)
                {
                    uint propertyCount = 0;

                    if (!NativeMethods.SetupDiGetDevicePropertyKeys(diSetHandle, ref diData[i], IntPtr.Zero, 0, ref propertyCount, 0))
                    {
                        var error = Marshal.GetLastWin32Error();
                        if (error != 122)
                        {
                            throw new Win32Exception(error, "Error calling SetupDiGetDevicePropertyKeys");
                        }
                    }

                    var device = new Device(diData[i].ClassGuid);

                    DEVPROPKEY[] dev           = new DEVPROPKEY[propertyCount];
                    GCHandle     devPropHandle = GCHandle.Alloc(dev, GCHandleType.Pinned);

                    if (!NativeMethods.SetupDiGetDevicePropertyKeys(diSetHandle, ref diData[i], devPropHandle.AddrOfPinnedObject(), (uint)dev.Length, ref propertyCount, 0))
                    {
                        var error = Marshal.GetLastWin32Error();
                        throw new Win32Exception(error, "Error calling SetupDiGetDevicePropertyKeys");
                    }

                    int devicePropertyIterator = 0;
                    while (devicePropertyIterator < propertyCount)
                    {
                        byte[] propBuffer   = new byte[1];
                        uint   requiredSize = 0;

                        DEVPROPTYPE devicePropertyType;
                        if (!NativeMethods.SetupDiGetDeviceProperty(
                                diSetHandle,
                                ref diData[i],
                                ref dev[devicePropertyIterator],
                                out devicePropertyType, propBuffer,
                                (uint)propBuffer.Length,
                                ref requiredSize,
                                0))
                        {
                            var error = Marshal.GetLastWin32Error();
                            if (error != 122)
                            {
                                throw new Win32Exception(error, "Error calling SetupDiGetDeviceProperty");
                            }
                        }

                        Array.Resize(ref propBuffer, (int)requiredSize);

                        if (NativeMethods.SetupDiGetDeviceProperty(diSetHandle, ref diData[i], ref dev[devicePropertyIterator], out devicePropertyType, propBuffer, (uint)propBuffer.Length, ref requiredSize, 0))
                        {
                            DEVPROPTYPE devPropType = devicePropertyType;
                            switch (devPropType)
                            {
                            case DEVPROPTYPE.DEVPROP_TYPE_UINT16:
                                device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToUInt16(propBuffer, 0));
                                break;

                            case DEVPROPTYPE.DEVPROP_TYPE_INT32:
                                device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToInt32(propBuffer, 0));
                                break;

                            case DEVPROPTYPE.DEVPROP_TYPE_UINT32:
                                device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToUInt32(propBuffer, 0));
                                break;

                            case DEVPROPTYPE.DEVPROP_TYPE_INT64:
                                device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToInt64(propBuffer, 0));
                                break;

                            case DEVPROPTYPE.DEVPROP_TYPE_FLOAT:
                            case DEVPROPTYPE.DEVPROP_TYPE_DOUBLE:
                            case DEVPROPTYPE.DEVPROP_TYPE_DECIMAL:
                            case DEVPROPTYPE.DEVPROP_TYPE_CURRENCY:
                            case DEVPROPTYPE.DEVPROP_TYPE_DATE:
                                throw new Exception(string.Format("No property processing available for device property type '{0}'", devPropType.ToString()));

                            case DEVPROPTYPE.DEVPROP_TYPE_UINT64:
                                device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToUInt64(propBuffer, 0));
                                break;

                            case DEVPROPTYPE.DEVPROP_TYPE_GUID:
                            {
                                byte[] guidBuffer = new byte[16];
                                Array.ConstrainedCopy(propBuffer, 0, guidBuffer, 0, 16);
                                device.Properties.Add(dev[devicePropertyIterator], new Guid(guidBuffer));
                                break;
                            }

                            case DEVPROPTYPE.DEVPROP_TYPE_FILETIME:
                                device.Properties.Add(dev[devicePropertyIterator], DateTime.FromFileTime(BitConverter.ToInt64(propBuffer, 0)));
                                break;

                            case DEVPROPTYPE.DEVPROP_TYPE_BOOLEAN:
                                device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToBoolean(propBuffer, 0));
                                break;

                            case DEVPROPTYPE.DEVPROP_TYPE_STRING:
                                var value = Encoding.Unicode.GetString(propBuffer, 0, (int)requiredSize);
                                if (value[value.Length - 1] == (new char[1])[0])
                                {
                                    value = value.Substring(0, value.Length - 1);
                                }
                                device.Properties.Add(dev[devicePropertyIterator], value);

                                break;

                            default:
                            {
                                Dictionary <DEVPROPKEY, object> properties = device.Properties;
                                DEVPROPKEY prop      = dev[devicePropertyIterator];
                                string     propValue = Encoding.Unicode.GetString(propBuffer, 0, (int)requiredSize);
                                char[]     separator = new char[1];
                                properties.Add(prop, propValue.Split(separator));
                                break;
                            }
                            }

                            devicePropertyIterator++;
                        }
                    }

                    devPropHandle.Free();
                    yield return(device);
                }

                diSetHandle.Close();
            }
            finally
            {
                if (diSetHandle != null)
                {
                    if (diSetHandle.IsClosed == false)
                    {
                        diSetHandle.Close();
                    }
                    diSetHandle.Dispose();
                }
            }
        }
示例#22
0
 public static extern bool SetupDiEnumDeviceInterfaces(
     SafeDeviceInfoSetHandle deviceInfoSet,
     DeviceInfoData deviceInfoData,
     ref Guid interfaceClassGuid,
     uint memberIndex,
     ref DeviceInterfaceData deviceInterfaceData);
示例#23
0
 public static extern unsafe bool SetupDiGetDeviceInterfaceDetail(
     SafeDeviceInfoSetHandle deviceInfoSet,
     ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
     SP_DEVICE_INTERFACE_DETAIL_DATA* deviceInterfaceDetailData,
     int deviceInterfaceDetailDataSize,
     int* requiredSize,
     SP_DEVINFO_DATA deviceInfoData);
示例#24
0
 public static unsafe extern bool SetupDiEnumDriverInfo(
     SafeDeviceInfoSetHandle deviceInfoSet,
     [Friendly(FriendlyFlags.In | FriendlyFlags.Optional)] SP_DEVINFO_DATA *deviceInfoData,
     DriverType driverType,
     uint memberIndex,
     [Friendly(FriendlyFlags.Bidirectional)] SP_DRVINFO_DATA *driverInfoData);
示例#25
0
 public static extern bool SetupDiEnumDeviceInterfaces(
     SafeDeviceInfoSetHandle deviceInfoSet,
     SP_DEVINFO_DATA deviceInfoData,
     ref Guid interfaceClassGuid,
     int memberIndex,
     ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
示例#26
0
 public static unsafe extern bool SetupDiSetSelectedDriver(
     SafeDeviceInfoSetHandle deviceInfoSet,
     [Friendly(FriendlyFlags.In | FriendlyFlags.Optional)] SP_DEVINFO_DATA *deviceInfoData,
     [Friendly(FriendlyFlags.Bidirectional | FriendlyFlags.Optional)] SP_DRVINFO_DATA *driverInfoData);
示例#27
0
 public static extern unsafe bool SetupDiEnumDeviceInterfaces(
     SafeDeviceInfoSetHandle deviceInfoSet,
     [Friendly(FriendlyFlags.In | FriendlyFlags.Optional)] SP_DEVINFO_DATA *deviceInfoData,
     ref Guid interfaceClassGuid,
     int memberIndex,
     ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
示例#28
0
 public static extern bool SetupDiEnumDeviceInfo(SafeDeviceInfoSetHandle DeviceInfoSet, uint MemberIndex, ref SpDevInfoData DeviceInfoData);
 public static extern bool SetupDiCallClassInstaller(DiFunction installFunction,
                                                     SafeDeviceInfoSetHandle deviceInfoSet, [In()] ref DeviceInfoData deviceInfoData);
示例#30
0
 public static extern bool SetupDiGetDeviceProperty(SafeDeviceInfoSetHandle DeviceInfoSet, in SpDevInfoData DeviceInfoData,
 public static extern bool SetupDiSetClassInstallParams(SafeDeviceInfoSetHandle deviceInfoSet,
                                                        [In()] ref DeviceInfoData deviceInfoData, [In()] ref PropertyChangeParameters classInstallParams,
                                                        int classInstallParamsSize);
示例#32
0
		public bool SetupDiGetDeviceInterfaceDetail(
            SafeDeviceInfoSetHandle deviceInfoSet,
            ref DeviceInterfaceData deviceInterfaceData,
            IntPtr deviceInterfaceDetailData,
            uint deviceInterfaceDetailDataSize,
            NullableUInt32 requiredSize,
            DeviceInfoData deviceInfoData)
			=> SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, deviceInterfaceDetailData, deviceInterfaceDetailDataSize, requiredSize, deviceInfoData);
示例#33
0
 public static IList <DeviceNode> GetDevices()
 {
     using (SafeDeviceInfoSetHandle dis = SetupApi.SetupDiGetClassDevsA(IntPtr.Zero, null, IntPtr.Zero, DICFG.PRESENT | DICFG.ALLCLASSES)) {
         return(GetDevicesInSet(dis));
     }
 }
示例#34
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));
     }
 }
示例#35
0
 public static extern bool SetupDiGetDeviceInterfaceDetail(
     SafeDeviceInfoSetHandle deviceInfoSet,
     ref DeviceInterfaceData deviceInterfaceData,
     IntPtr deviceInterfaceDetailData,
     int deviceInterfaceDetailDataSize,
     NullableUInt32 requiredSize,
     DeviceInfoData deviceInfoData);
        public static void RemoveDevice(string instanceId, DeviceClass?deviceClass = null)
        {
            SafeDeviceInfoSetHandle diSetHandle = null;

            try
            {
                GCHandle guid = new GCHandle();

                if (deviceClass.HasValue)
                {
                    var classGuid = Device.DeviceClassGuids[(int)deviceClass];
                    guid        = GCHandle.Alloc(classGuid, GCHandleType.Pinned);
                    diSetHandle = NativeMethods.SetupDiGetClassDevs(guid.AddrOfPinnedObject(), null, IntPtr.Zero, SetupDiGetClassDevsFlags.Null);
                }
                else
                {
                    diSetHandle = NativeMethods.SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, SetupDiGetClassDevsFlags.AllClasses);
                }

                if (diSetHandle.IsInvalid)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiGetClassDevs");
                }

                //Get the device information data for each matching device.
                var diData = GetDeviceInfoData(diSetHandle);

                if (!string.IsNullOrEmpty(instanceId))
                {
                    // Find the index of of the instance
                    int index = GetIndexOfInstance(diSetHandle, diData, instanceId);
                    if (index == -1)
                    {
                        throw new IndexOutOfRangeException(string.Format("The device '{0}' could not be found", instanceId));
                    }

                    diData = new SP_DEVINFO_DATA[] { diData[index] };
                }

                for (int i = 0; i < diData.Length; i++)
                {
                    var needReboot = false;
                    var result     = NativeMethods.DiUninstallDevice(IntPtr.Zero, diSetHandle.DangerousGetHandle(), ref diData[i], 0, out needReboot);

                    if (result == false)
                    {
                        int err = Marshal.GetLastWin32Error();
                        throw new Win32Exception();
                    }
                }
            }
            finally
            {
                if (diSetHandle != null)
                {
                    if (diSetHandle.IsClosed == false)
                    {
                        diSetHandle.Close();
                    }
                    diSetHandle.Dispose();
                }
            }
        }
示例#37
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));
     }
 }
示例#38
0
 public static extern bool SetupDiEnumDeviceInterfaces(
     SafeDeviceInfoSetHandle deviceInfoSet,
     SP_DEVINFO_DATA deviceInfoData,
     ref Guid interfaceClassGuid,
     int memberIndex,
     ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
示例#39
0
		public bool SetupDiEnumDeviceInfo(
            SafeDeviceInfoSetHandle deviceInfoSet,
            uint memberIndex,
            DeviceInfoData deviceInfoData)
			=> SetupDiEnumDeviceInfo(deviceInfoSet, memberIndex, deviceInfoData);
示例#40
0
 public static extern bool SetupDiEnumDeviceInfo(
     SafeDeviceInfoSetHandle deviceInfoSet,
     uint memberIndex,
     DeviceInfoData deviceInfoData);
示例#41
0
		public bool SetupDiEnumDeviceInterfaces(
            SafeDeviceInfoSetHandle deviceInfoSet,
            DeviceInfoData deviceInfoData,
            ref Guid interfaceClassGuid,
            uint memberIndex,
            ref DeviceInterfaceData deviceInterfaceData)
			=> SetupDiEnumDeviceInterfaces(deviceInfoSet, deviceInfoData, ref interfaceClassGuid, memberIndex, ref deviceInterfaceData);
        private static IHidDeviceInformation InfoFromData(SafeDeviceInfoSetHandle infoList, SP_DEVICE_INTERFACE_DATA interfaceData)
        {
            var path = SetupDiGetDeviceInterfaceDetail(infoList, interfaceData, IntPtr.Zero);

            using (var device = Win32HidDevice.TryFromPath(path, 0))
            {
                if (device == null)
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug($"Unable to open device {path}");
                    }
                    return null;
                }

                var information = new Win32HidDeviceInformationStored(device.Information);
                if (log.IsDebugEnabled)
                {
                    log.Debug(
                        $"Found device '{information.Product}' (PID=0x{information.ProductId:X2}) "
                        + $"by '{information.Manufacturer}' (VID=0x{information.VendorId:X2})");
                }
                return information;
            }
        }