internal Guid GetProperty(Native.SP_DEVINFO_DATA devData, int property, Guid defaultValue)
        {
            if (devData == null)
            {
                throw new ArgumentNullException("devData");
            }

            int propertyRegDataType = 0;
            int requiredSize;
            int propertyBufferSize = Marshal.SizeOf(typeof(Guid));

            IntPtr propertyBuffer = Marshal.AllocHGlobal(propertyBufferSize);

            if (!Native.SetupDiGetDeviceRegistryProperty(_deviceInfoSet,
                                                         devData,
                                                         property,
                                                         out propertyRegDataType,
                                                         propertyBuffer,
                                                         propertyBufferSize,
                                                         out requiredSize))
            {
                Marshal.FreeHGlobal(propertyBuffer);
                int error = Marshal.GetLastWin32Error();
                if (error != Native.ERROR_INVALID_DATA)
                {
                    throw new Win32Exception(error);
                }
                return(defaultValue);
            }

            Guid value = (Guid)Marshal.PtrToStructure(propertyBuffer, typeof(Guid));

            Marshal.FreeHGlobal(propertyBuffer);
            return(value);
        }
Exemple #2
0
        internal Device(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index)
        {
            if (deviceClass == null)
                throw new ArgumentNullException("deviceClass");

            if (deviceInfoData == null)
                throw new ArgumentNullException("deviceInfoData");

            _deviceClass = deviceClass;
            _path = path; // may be null
            _deviceInfoData = deviceInfoData;
            _index = index;
        }
Exemple #3
0
        internal Device(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index)
        {
            if (deviceClass == null)
            {
                throw new ArgumentNullException("deviceClass");
            }

            if (deviceInfoData == null)
            {
                throw new ArgumentNullException("deviceInfoData");
            }

            _deviceClass    = deviceClass;
            _path           = path;   // may be null
            _deviceInfoData = deviceInfoData;
            _index          = index;
        }
        internal Native.SP_DEVINFO_DATA GetInfo(int dnDevInst)
        {
            StringBuilder sb = new StringBuilder(1024);
            int           hr = Native.CM_Get_Device_ID(dnDevInst, sb, sb.Capacity, 0);

            if (hr != 0)
            {
                throw new Win32Exception(hr);
            }

            Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA();
            devData.cbSize = (uint)Marshal.SizeOf(devData);
            if (!Native.SetupDiOpenDeviceInfo(_deviceInfoSet, sb.ToString(), IntPtr.Zero, 0, devData))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return(devData);
        }
Exemple #5
0
 internal Volume(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index)
     : base(deviceClass, deviceInfoData, path, index)
 {
 }
 internal override Device CreateDevice(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index, int disknum = -1)
 {
     return(new Volume(deviceClass, deviceInfoData, path, index));
 }
        /// <summary>
        /// Gets the list of devices of this device class.
        /// </summary>
        /// <value>
        /// The devices.
        /// </value>
        /// <exception cref="System.ComponentModel.Win32Exception">
        /// </exception>
        public List <Device> GetDevices()
        {
            try
            {
                if (_devices == null || _devices.Count == 0)
                {
                    _devices = new List <Device>();
                    int index = 0;
                    while (true)
                    {
                        Native.SP_DEVICE_INTERFACE_DATA interfaceData = new Native.SP_DEVICE_INTERFACE_DATA();
                        interfaceData.cbSize = (uint)Marshal.SizeOf(interfaceData);
                        if (!Native.SetupDiEnumDeviceInterfaces(_deviceInfoSet, null, ref _classGuid, index, interfaceData))
                        {
                            int error = Marshal.GetLastWin32Error();
                            if (error != Native.ERROR_NO_MORE_ITEMS)
                            {
                                throw new Win32Exception(error);
                            }
                            break;
                        }

                        Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA();
                        devData.cbSize = (uint)Marshal.SizeOf(devData);
                        int size = 0;
                        if (!Native.SetupDiGetDeviceInterfaceDetail(_deviceInfoSet, interfaceData, IntPtr.Zero, 0, ref size, devData))
                        {
                            int error = Marshal.GetLastWin32Error();
                            if (error != Native.ERROR_INSUFFICIENT_BUFFER)
                            {
                                throw new Win32Exception(error);
                            }
                        }

                        IntPtr buffer = Marshal.AllocHGlobal(size);
                        Native.SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new Native.SP_DEVICE_INTERFACE_DETAIL_DATA();
                        detailData.cbSize = Marshal.SizeOf(detailData);

                        Marshal.WriteInt32(buffer, IntPtr.Size);
                        Marshal.StructureToPtr(detailData, buffer, false);

                        if (!Native.SetupDiGetDeviceInterfaceDetail(_deviceInfoSet, interfaceData, buffer, size, ref size, devData))
                        {
                            int            error = Marshal.GetLastWin32Error();
                            Win32Exception exc   = new Win32Exception(error);
                            Marshal.FreeHGlobal(buffer);
                            throw exc;
                        }

                        var    strPtr     = new IntPtr(buffer.ToInt64() + 4);
                        string devicePath = Marshal.PtrToStringAuto(strPtr);
                        //IntPtr pDevicePath = (IntPtr)((int)buffer + Marshal.SizeOf(typeof(int)));
                        //string devicePath = Marshal.PtrToStringAuto(pDevicePath);
                        Marshal.FreeHGlobal(buffer);

                        if (_classGuid.Equals(new Guid(Native.GUID_DEVINTERFACE_DISK)))
                        {
                            // Find disks
                            IntPtr hFile = Native.CreateFile(devicePath, 0, Native.FILE_SHARE_READ | Native.FILE_SHARE_WRITE, IntPtr.Zero, Native.OPEN_EXISTING, 0, IntPtr.Zero);
                            if (hFile.ToInt32() == Native.INVALID_HANDLE_VALUE)
                            {
                                throw new Win32Exception(Marshal.GetLastWin32Error());
                            }

                            int    bytesReturned = 0;
                            int    numBufSize    = 0x400; // some big size
                            IntPtr numBuffer     = Marshal.AllocHGlobal(numBufSize);
                            Native.STORAGE_DEVICE_NUMBER disknum;

                            try
                            {
                                if (!Native.DeviceIoControl(hFile, Native.IOCTL_STORAGE_GET_DEVICE_NUMBER, IntPtr.Zero, 0, numBuffer, numBufSize, out bytesReturned, IntPtr.Zero))
                                {
                                    Console.WriteLine("IOCTL failed.");
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("Exception calling ioctl: " + ex);
                            }
                            finally
                            {
                                Native.CloseHandle(hFile);
                            }

                            if (bytesReturned > 0)
                            {
                                disknum = (Native.STORAGE_DEVICE_NUMBER)Marshal.PtrToStructure(numBuffer, typeof(Native.STORAGE_DEVICE_NUMBER));
                            }
                            else
                            {
                                disknum = new Native.STORAGE_DEVICE_NUMBER()
                                {
                                    DeviceNumber = -1, DeviceType = -1, PartitionNumber = -1
                                }
                            };

                            Device device = CreateDevice(this, devData, devicePath, index, disknum.DeviceNumber);
                            _devices.Add(device);

                            Marshal.FreeHGlobal(hFile);
                        }
                        else
                        {
                            Device device = CreateDevice(this, devData, devicePath, index);
                            _devices.Add(device);
                        }

                        index++;
                    }
                    _devices.Sort();
                }
                return(_devices);
            }
            catch (Exception err)
            {
                Debug.WriteLine("DeviceClass.GetDevices() - ERROR: " + err.ToString());
                return(new List <Device>());
            }
        }
 internal virtual Device CreateDevice(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index, int disknum = -1)
 {
     return(new Device(deviceClass, deviceInfoData, path, index, disknum));
 }
Exemple #9
0
        internal Native.SP_DEVINFO_DATA GetInfo(int dnDevInst)
        {
            StringBuilder sb = new StringBuilder(1024);
            int hr = Native.CM_Get_Device_ID(dnDevInst, sb, sb.Capacity, 0);
            if (hr != 0)
                throw new Win32Exception(hr);

            Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA();
            devData.cbSize = Marshal.SizeOf(typeof(Native.SP_DEVINFO_DATA));
            if (!Native.SetupDiOpenDeviceInfo(_deviceInfoSet, sb.ToString(), IntPtr.Zero, 0, devData))
                throw new Win32Exception(Marshal.GetLastWin32Error());

            return devData;
        }