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); }
internal Device(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index) { if (deviceClass == null) { throw new ArgumentNullException(nameof(deviceClass)); } if (deviceInfoData == null) { throw new ArgumentNullException(nameof(deviceInfoData)); } DeviceClass = deviceClass; DeviceInfoData = deviceInfoData; Path = path; // may be null Index = index; _class = new Lazy <string>(GetClass); _classGuid = new Lazy <string>(GetClassGuid); _description = new Lazy <string>(GetDescription); _friendlyName = new Lazy <string>(GetFriendlyName); _capabilities = new Lazy <DeviceCapabilities>(GetCapabilities); _isUsb = new Lazy <bool>(GetIsUsb); _parent = new Lazy <Device>(GetParent); _removableDevices = new Lazy <DeviceCollection>(GetRemovableDevices); }
internal Volume(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index, ILogger logger) : base(deviceClass, deviceInfoData, path, index, logger) { _volumeName = new Lazy <string>(GetVolumeName); _logicalDrive = new Lazy <string>(GetLogicalDrive); _disks = new Lazy <DiskCollection>(GetDisks); _diskNumbers = new Lazy <int[]>(GetDiskNumbers); }
private Device GetParent() { uint parentDevInst = 0; int hr = Native.CM_Get_Parent(ref parentDevInst, DeviceInfoData.devInst, 0); if (hr == 0) { Native.SP_DEVINFO_DATA info = DeviceClass.GetInfo(parentDevInst); return(new Device(DeviceClass, info, null, -1)); } return(null); }
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; }
private Native.SP_DEVINFO_DATA GetDeviceData(Native.SP_DEVICE_INTERFACE_DATA interfaceData, out int size) { Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA(); size = 0; if (!Native.SetupDiGetDeviceInterfaceDetail(_deviceInfoSet, interfaceData, IntPtr.Zero, 0, ref size, devData)) { int error = Marshal.GetLastWin32Error(); if (error != Native.ERROR_INSUFFICIENT_BUFFER) { Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); Logger.Log(LogLevel.Error, ex); throw ex; } } return(devData); }
private DeviceCollection GetDevices() { List <Device> devices = new List <Device>(); Native.SP_DEVICE_INTERFACE_DATA interfaceData; for (int index = 0; (interfaceData = GetInterfaceData(index)) != null; index++) { int size; Native.SP_DEVINFO_DATA devData = GetDeviceData(interfaceData, out size); string devicePath = GetDevicePath(interfaceData, devData, size); Device device = CreateDevice(devData, devicePath, index); devices.Add(device); } devices.Sort(); return(devices); }
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); }
internal int GetProperty(Native.SP_DEVINFO_DATA devData, int property, int defaultValue) { if (devData == null) { throw new ArgumentNullException(nameof(devData)); } int propertyRegDataType = 0; int requiredSize; int propertyBufferSize = Marshal.SizeOf(typeof(int)); IntPtr propertyBuffer = Marshal.AllocHGlobal(propertyBufferSize); try { if (!Native.SetupDiGetDeviceRegistryProperty(_deviceInfoSet, devData, property, out propertyRegDataType, propertyBuffer, propertyBufferSize, out requiredSize)) { int error = Marshal.GetLastWin32Error(); if (error != Native.ERROR_INVALID_DATA) { Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); Logger.Log(LogLevel.Error, ex); throw ex; } return(defaultValue); } return((int)Marshal.PtrToStructure(propertyBuffer, typeof(int))); } finally { Marshal.FreeHGlobal(propertyBuffer); } }
internal string GetProperty(Native.SP_DEVINFO_DATA devData, int property, string defaultValue) { if (devData == null) { throw new ArgumentNullException(nameof(devData)); } int propertyRegDataType = 0; int requiredSize; int propertyBufferSize = Native.PROPERTY_BUFFER_SIZE; IntPtr propertyBuffer = Marshal.AllocHGlobal(propertyBufferSize); try { if (!Native.SetupDiGetDeviceRegistryProperty(_deviceInfoSet, devData, property, out propertyRegDataType, propertyBuffer, propertyBufferSize, out requiredSize)) { int error = Marshal.GetLastWin32Error(); if (error != Native.ERROR_INVALID_DATA) { Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); throw ex; } return(defaultValue); } return(Marshal.PtrToStringAuto(propertyBuffer)); } finally { Marshal.FreeHGlobal(propertyBuffer); } }
internal Native.SP_DEVINFO_DATA GetInfo(uint dnDevInst) { StringBuilder sb = new StringBuilder(Native.CM_BUFFER_SIZE); int hr = Native.CM_Get_Device_ID(dnDevInst, sb, sb.Capacity, 0); if (hr != 0) { Exception ex = Marshal.GetExceptionForHR(hr); Logger.Log(LogLevel.Error, ex); throw ex; } Native.SP_DEVINFO_DATA devData = new Native.SP_DEVINFO_DATA(); if (!Native.SetupDiOpenDeviceInfo(_deviceInfoSet, sb.ToString(), IntPtr.Zero, 0, devData)) { Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); Logger.Log(LogLevel.Error, ex); throw ex; } return(devData); }
private string GetDevicePath(Native.SP_DEVICE_INTERFACE_DATA interfaceData, Native.SP_DEVINFO_DATA devData, int size) { IntPtr buffer = Marshal.AllocHGlobal(size); try { Native.SP_DEVICE_INTERFACE_DETAIL_DATA detailData = new Native.SP_DEVICE_INTERFACE_DETAIL_DATA(); Marshal.StructureToPtr(detailData, buffer, false); if (!Native.SetupDiGetDeviceInterfaceDetail(_deviceInfoSet, interfaceData, buffer, size, ref size, devData)) { Exception ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); Logger.Log(LogLevel.Error, ex); throw ex; } IntPtr pDevicePath = new IntPtr(buffer.ToInt64() + Marshal.SizeOf(typeof(int))); return(Marshal.PtrToStringAuto(pDevicePath)); } finally { Marshal.FreeHGlobal(buffer); } }
internal override Device CreateDevice(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index) { return(new Volume(deviceClass, deviceInfoData, path, index)); }
internal abstract Device CreateDevice(Native.SP_DEVINFO_DATA deviceInfoData, string path, int index);
internal override Device CreateDevice(Native.SP_DEVINFO_DATA deviceInfoData, string path, int index) { return(new Disk(this, deviceInfoData, path, index)); }
internal Volume(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index) : base(deviceClass, deviceInfoData, path, index) { }
internal Disk(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index, ILogger logger) : base(deviceClass, deviceInfoData, path, index, logger) { _diskNumber = new Lazy <int>(GetDiskNumber); }
internal virtual Device CreateDevice(DeviceClass deviceClass, Native.SP_DEVINFO_DATA deviceInfoData, string path, int index) { return(new Device(deviceClass, deviceInfoData, path, index)); }