예제 #1
0
        static public bool RemoveDevices(uint Instance)
        {
            int  DN_REMOVABLE = 0x00004000;
            int  CR_SUCCESS   = 0;
            uint status       = 0;
            uint problem      = 0;
            bool success      = false;

            if (CR_SUCCESS == SetupAPI.CM_Get_DevNode_Status(out status, out problem, Instance, 0) && (DN_REMOVABLE & status) > 0)
            {
                SetupAPI.PNP_VETO_TYPE    pnp_veto_type;
                System.Text.StringBuilder sb = new System.Text.StringBuilder(255);

                success = (CR_SUCCESS == SetupAPI.CM_Request_Device_Eject(Instance, out pnp_veto_type, sb, sb.Capacity, 0));
            }

            return(success);
        }
예제 #2
0
        /// <summary>
        /// Get a list of all known devices of a given class, optionally only selecting devices
        /// that are physically present at the time of the call.
        ///
        /// Throws a System.ComponentModel.Win32Exception on any Windows API error
        /// </summary>
        /// <param name="DeviceClassGUID">GUID of device class to enumerate.  May be a value from
        /// GUID_DEVINTERFACE, any other known device class GUID, or Guid.Empty to find all USB devices.</param>
        /// <param name="Present">TRUE if only present (conencted) devices should be returned</param>
        /// <returns>List of all found devices</returns>
        static public List <DeviceInfo> GetDevices(Guid DeviceClassGUID, Boolean Present = true)
        {
            List <DeviceInfo> Devices = new List <DeviceInfo>();

            if (DeviceClassGUID == Guid.Empty)
            {
                DeviceClassGUID = GUID_DEVINTERFACE.GUID_DEVINTERFACE_USB_DEVICE;
            }

            IntPtr deviceList = IntPtr.Zero;
            int    Flags      = (Present ? (int)SetupAPI.DiGetClassFlags.DIGCF_PRESENT : 0);

            Flags      = Flags | (int)SetupAPI.DiGetClassFlags.DIGCF_DEVICEINTERFACE;
            deviceList = SetupAPI.SetupDiGetClassDevs(ref DeviceClassGUID, IntPtr.Zero, IntPtr.Zero, Flags);

            if (deviceList.ToInt64() == SetupAPI.INVALID_HANDLE_VALUE)
            {
                throw new Win32Exception();
            }

            SetupAPI.SP_DEVINFO_DATA DevInfo = new SetupAPI.SP_DEVINFO_DATA();
            DevInfo.cbSize = (uint)Marshal.SizeOf(DevInfo);

            uint DeviceIndex = 0;

            while (SetupAPI.SetupDiEnumDeviceInfo(deviceList, DeviceIndex, ref DevInfo))
            {
                String DevID = SetupAPI.GetDeviceID(DevInfo);
                if (String.IsNullOrEmpty(DevID))
                {
                    throw new Win32Exception();
                }

                SetupAPI.SP_DEVINFO_DATA IntInfo = new SetupAPI.SP_DEVINFO_DATA();
                IntInfo.cbSize = (uint)Marshal.SizeOf(IntInfo);

                uint InterfaceIndex = 0;
                while (SetupAPI.SetupDiEnumDeviceInterfaces(deviceList, ref DevInfo, ref DeviceClassGUID, InterfaceIndex, ref IntInfo))
                {
                    DeviceInfo Info = new DeviceInfo(DevID);

                    Info.DevicePath = SetupAPI.GetDeviceInterfacePath(deviceList, ref DevInfo, ref IntInfo);
                    if (String.IsNullOrEmpty(Info.DevicePath))
                    {
                        throw new Win32Exception();
                    }

                    Info.InterfaceGUID = IntInfo.classGuid;
                    Info.Instance      = DevInfo.devInst;

                    Info.Class        = SetupAPI.GetDevicePropertyString(deviceList, DevInfo, (uint)SPDRP.SPDRP_CLASS);
                    Info.FriendlyName = SetupAPI.GetDevicePropertyString(deviceList, DevInfo, (uint)SPDRP.SPDRP_FRIENDLYNAME);
                    Info.Description  = SetupAPI.GetDevicePropertyString(deviceList, DevInfo, (uint)SPDRP.SPDRP_DEVICEDESC);

                    Info.Enumerator = SetupAPI.GetDevicePropertyString(deviceList, DevInfo, (uint)SPDRP.SPDRP_ENUMERATOR_NAME);
                    Info.Location   = SetupAPI.GetDevicePropertyString(deviceList, DevInfo, (uint)SPDRP.SPDRP_LOCATION_INFORMATION);

                    Info.Manufacturer = SetupAPI.GetDevicePropertyString(deviceList, DevInfo, (uint)SPDRP.SPDRP_MFG);
                    Info.ServiceName  = SetupAPI.GetDevicePropertyString(deviceList, DevInfo, (uint)SPDRP.SPDRP_SERVICE);
                    Info.ClassGUID    = System.Guid.Parse(SetupAPI.GetDevicePropertyString(deviceList, DevInfo, (uint)SPDRP.SPDRP_CLASSGUID));
                    Devices.Add(Info);
                    InterfaceIndex++;

                    IntInfo        = new SetupAPI.SP_DEVINFO_DATA();
                    IntInfo.cbSize = (uint)Marshal.SizeOf(IntInfo);
                }

                DeviceIndex++;
            }

            SetupAPI.SetupDiDestroyDeviceInfoList(deviceList);

            return(Devices);
        }