Ejemplo n.º 1
0
        /// <summary>
        /// Enable or disable a device.
        /// </summary>
        /// <param name="classGuid">The class guid of the device. Available in the device manager.</param>
        /// <param name="instanceId">The device instance id of the device. Available in the device manager.</param>
        /// <param name="enable">True to enable, False to disable.</param>
        /// <remarks>Will throw an exception if the device is not Disableable.</remarks>
        public static void SetDeviceEnabled(Guid classGuid, string instanceId, bool enable)
        {
            SafeDeviceInfoSetHandle diSetHandle = null;

            try
            {
                // Get the handle to a device information set for all devices matching classGuid that are present on the
                // system.
                diSetHandle = NativeMethods.SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, SetupDiGetClassDevsFlags.Present);
                // Get the device information data for each matching device.
                DeviceInfoData[] diData = GetDeviceInfoData(diSetHandle);
                // Find the index of our instance.
                int index = GetIndexOfInstance(diSetHandle, diData, instanceId);
                // Enable/Disable.
                EnableDevice(diSetHandle, diData[index], enable);
                //   EnableDevice(diSetHandle, diData[1], enable);
            }
            finally
            {
                if (diSetHandle != null)
                {
                    if (diSetHandle.IsClosed == false)
                    {
                        diSetHandle.Close();
                    }
                    diSetHandle.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tries to reset (disable/enable) the port with the given instance Id.
        /// </summary>
        /// <param name="instanceId">The instance Id of the port to reset.</param>
        /// <returns>True, if the port was successfully resetted, otherwise false.</returns>
        public static bool TryResetPortByInstanceId(string instanceId)
        {
            SafeDeviceInfoSetHandle diSetHandle = null;

            if (!String.IsNullOrEmpty(instanceId))
            {
                try
                {
                    Guid[] guidArray = GetGuidFromName("Ports");

                    //Get the handle to a device information set for all devices matching classGuid that are present on the
                    //system.
                    diSetHandle = NativeMethods.SetupDiGetClassDevs(
                        ref guidArray[0], null, IntPtr.Zero, SetupDiGetClassDevsFlags.DeviceInterface);

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

                    //Try to find the object with the same instance Id.
                    foreach (var infoData in diData)
                    {
                        var instanceIds = GetInstanceIdsFromClassGuid(infoData.ClassGuid);
                        foreach (var id in instanceIds)
                        {
                            if (id.Equals(instanceId))
                            {
                                //disable port
                                SetDeviceEnabled(infoData.ClassGuid, id, false);
                                //wait a moment
                                Thread.Sleep(5000);
                                //enable port
                                SetDeviceEnabled(infoData.ClassGuid, id, true);
                                return(true);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    return(false);
                }
                finally
                {
                    if (diSetHandle != null)
                    {
                        if (diSetHandle.IsClosed == false)
                        {
                            diSetHandle.Close();
                        }
                        diSetHandle.Dispose();
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        private static List <string> GetInstanceIdsFromClassGuid(Guid classGuid)
        {
            SafeDeviceInfoSetHandle diSetHandle = null;
            List <string>           resultList  = new List <string>();

            try
            {
                // Get the handle to a device information set for all devices matching classGuid that are present on the
                // system.
                diSetHandle = NativeMethods.SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, SetupDiGetClassDevsFlags.Present);
                // Get the device information data for each matching device.
                DeviceInfoData[] diData = GetDeviceInfoData(diSetHandle);

                const int ERROR_INSUFFICIENT_BUFFER = 122;
                for (int index = 0; index <= diData.Length - 1; index++)
                {
                    StringBuilder sb           = new StringBuilder(1);
                    int           requiredSize = 0;
                    bool          result       = NativeMethods.SetupDiGetDeviceInstanceId(diSetHandle.DangerousGetHandle(), ref diData[index], sb, sb.Capacity, out requiredSize);
                    if (result == false && Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
                    {
                        sb.Capacity = requiredSize;
                        result      = NativeMethods.SetupDiGetDeviceInstanceId(diSetHandle.DangerousGetHandle(), ref diData[index], sb, sb.Capacity, out requiredSize);

                        resultList.Add(sb.ToString());
                    }
                }
            }
            finally
            {
                if (diSetHandle != null)
                {
                    if (diSetHandle.IsClosed == false)
                    {
                        diSetHandle.Close();
                    }
                    diSetHandle.Dispose();
                }
            }
            return(resultList);
        }