public UsbDevice[] GetDevices() { UInt32 index = 0; List <UsbDevice> devices = new List <UsbDevice>(); while (true) { UsbDeviceWinApi.SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new UsbDeviceWinApi.SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.Size = (UInt32)Marshal.SizeOf(deviceInterfaceData); Boolean success = UsbDeviceWinApi.SetupDiEnumDeviceInterfaces(this.handle, IntPtr.Zero, ref this.classGuid, index, ref deviceInterfaceData); if (!success) { if (UsbDeviceWinApi.ERROR_NO_MORE_ITEMS == Marshal.GetLastWin32Error()) { break; } this.TraceError("SetupDiEnumDeviceInterfaces"); } index++; devices.Add(UsbDeviceInterfaceData.GetUsbDevice(this.handle, deviceInterfaceData)); } return(devices.ToArray()); }
protected virtual void Dispose(bool disposing) { if (!IsInvalid()) { UsbDeviceWinApi.SetupDiDestroyDeviceInfoList(this.handle); } }
public UsbDevices(Guid classGuid) { this.classGuid = classGuid; this.handle = UsbDeviceWinApi.SetupDiGetClassDevs(ref classGuid, IntPtr.Zero, IntPtr.Zero, UsbDeviceWinApi.SetupDiGetClassDevsFlags.DIGCF_PRESENT | UsbDeviceWinApi.SetupDiGetClassDevsFlags.DIGCF_DEVICEINTERFACE); if (this.IsInvalid()) { this.TraceError("SetupDiGetClassDevs"); } }
public UsbDeviceProperty GetProperty(UsbDeviceWinApi.DEVPROPKEY devPropKey) { for (Int32 i = 0; i < this.Properties.Length; i++) { if (this.Properties[i].HasSameKey(devPropKey)) { return this.Properties[i]; } } return null; }
private String GetDeviceId() { String deviceId = null; Int32 bufferSize = 512; IntPtr buffer = Marshal.AllocHGlobal(bufferSize); Int32 errorCode = UsbDeviceWinApi.CM_Get_Device_ID(this.devInfoData.DevInst, buffer, bufferSize, 0); if (UsbDeviceWinApi.ERROR_SUCCESS == errorCode) { deviceId = Marshal.PtrToStringAuto(buffer); int slash = deviceId.LastIndexOf('\\'); if ((slash > 0) && (deviceId.LastIndexOf('-') > slash)) { UInt32 devInstParent; errorCode = UsbDeviceWinApi.CM_Get_Parent(out devInstParent, this.devInfoData.DevInst, 0); if (UsbDeviceWinApi.ERROR_SUCCESS == errorCode) { errorCode = UsbDeviceWinApi.CM_Get_Device_ID(devInstParent, buffer, bufferSize, 0); if (UsbDeviceWinApi.ERROR_SUCCESS == errorCode) { deviceId = Marshal.PtrToStringAuto(buffer); } else { this.TraceError("CM_Get_Device_ID", errorCode); } } else { this.TraceError("CM_Get_Parent", errorCode); } } } else { this.TraceError("CM_Get_Device_ID", errorCode); } Marshal.FreeHGlobal(buffer); return(deviceId); }
private UsbDeviceRegistryProperty[] GetRegistryProperties() { List <UsbDeviceRegistryProperty> registryProperties = new List <UsbDeviceRegistryProperty>(); for (UInt32 property = 0; property < UsbDeviceWinApi.DeviceRegistryPropertyKeys.SPDRP_MAXIMUM_PROPERTY; property++) { UInt32 propertyType; UInt32 requiredSize; Boolean success = UsbDeviceWinApi.SetupDiGetDeviceRegistryProperty(this.handle, ref this.devInfoData, property, out propertyType, IntPtr.Zero, 0, out requiredSize); if (success || (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INSUFFICIENT_BUFFER)) { if (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INVALID_DATA) { this.TraceError("SetupDiGetDeviceRegistryProperty"); } } else { IntPtr buffer = Marshal.AllocHGlobal((Int32)requiredSize); success = UsbDeviceWinApi.SetupDiGetDeviceRegistryProperty(this.handle, ref this.devInfoData, property, out propertyType, buffer, requiredSize, out requiredSize); if (success) { Object value = this.MarshalDeviceRegistryProperty(buffer, (Int32)requiredSize, propertyType); registryProperties.Add(new UsbDeviceRegistryProperty(property, value, propertyType)); } else { this.TraceError("SetupDiGetDeviceRegistryProperty"); registryProperties.Add(new UsbDeviceRegistryProperty(property, null, UsbDeviceWinApi.DeviceRegistryPropertyTypes.REG_NONE)); } Marshal.FreeHGlobal(buffer); } } return(registryProperties.ToArray()); }
private UsbDeviceInterface[] GetInterfaces(UInt32 devInst, String deviceId) { List <UsbDeviceInterface> ids = new List <UsbDeviceInterface>(); UInt32 devInstChild; Int32 errorCode = UsbDeviceWinApi.CM_Get_Child(out devInstChild, devInst, 0); if (UsbDeviceWinApi.CR_SUCCESS != errorCode) { this.TraceError("CM_Get_Child", errorCode); ids.Add(new UsbDeviceInterface(deviceId)); return(ids.ToArray()); } String interfaceId = this.GetDeviceId(devInstChild); if (!String.IsNullOrEmpty(interfaceId)) { ids.Add(new UsbDeviceInterface(interfaceId)); UInt32 devInstSibling = devInstChild; while (true) { errorCode = UsbDeviceWinApi.CM_Get_Sibling(out devInstSibling, devInstSibling, 0); if (UsbDeviceWinApi.CR_SUCCESS != errorCode) { this.TraceError("CM_Get_Sibling", errorCode); break; } interfaceId = this.GetDeviceId(devInstSibling); if (!String.IsNullOrEmpty(interfaceId)) { ids.Add(new UsbDeviceInterface(interfaceId)); } } } return(ids.ToArray()); }
private String GetDeviceInterfaceDetail() { UInt32 requiredSize; this.devInfoData = new UsbDeviceWinApi.SP_DEVINFO_DATA(); this.devInfoData.Size = (UInt32)Marshal.SizeOf(devInfoData); Boolean success = UsbDeviceWinApi.SetupDiGetDeviceInterfaceDetail(this.handle, ref this.deviceInterfaceData, IntPtr.Zero, 0, out requiredSize, ref this.devInfoData); if (success || (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INSUFFICIENT_BUFFER)) { this.TraceError("SetupDiGetDeviceInterfaceDetail"); return(null); } IntPtr buffer = Marshal.AllocHGlobal((Int32)requiredSize); Marshal.WriteInt32(buffer, 8 == IntPtr.Size ? 8 : 6); success = UsbDeviceWinApi.SetupDiGetDeviceInterfaceDetail(this.handle, ref this.deviceInterfaceData, buffer, requiredSize, out requiredSize, ref this.devInfoData); String devicePath = null; if (success) { IntPtr stringBuffer = new IntPtr(buffer.ToInt64() + 4); devicePath = Marshal.PtrToStringAuto(stringBuffer); } else { this.TraceError("SetupDiGetDeviceInterfaceDetail"); } Marshal.FreeHGlobal(buffer); return(devicePath); }
private String GetDeviceId(UInt32 devInst) { Int32 bufferSize = 1024; IntPtr buffer = Marshal.AllocHGlobal(bufferSize); String deviceId = null; Int32 errorCode = UsbDeviceWinApi.CM_Get_Device_ID(devInst, buffer, bufferSize, 0); if (UsbDeviceWinApi.CR_SUCCESS == errorCode) { deviceId = Marshal.PtrToStringAuto(buffer); } else { this.TraceError("CM_Get_Device_ID", errorCode); } Marshal.FreeHGlobal(buffer); return(deviceId); }
public Boolean HasSameKey(UsbDeviceWinApi.DEVPROPKEY devPropKey) { return (this.Key.Fmtid == devPropKey.Fmtid) && (this.Key.Pid == devPropKey.Pid); }
internal UsbDeviceProperty(UsbDeviceWinApi.DEVPROPKEY key, Object value, UInt32 type) { this.Key = key; this.Value = value; this.Type = type; }
public static UsbDevice GetUsbDevice(IntPtr handle, UsbDeviceWinApi.SP_DEVICE_INTERFACE_DATA deviceInterfaceData) { UsbDeviceInterfaceData usbDeviceInterface = new UsbDeviceInterfaceData(handle, deviceInterfaceData); return usbDeviceInterface.GetDevice(); }
private UsbDeviceInterfaceData(IntPtr handle, UsbDeviceWinApi.SP_DEVICE_INTERFACE_DATA deviceInterfaceData) { this.handle = handle; this.deviceInterfaceData = deviceInterfaceData; }
public Object GetPropertyValue(UsbDeviceWinApi.DEVPROPKEY devPropKey) { UsbDeviceProperty usbDeviceProperty = this.GetProperty(devPropKey); return null == usbDeviceProperty ? null : usbDeviceProperty.Value; }
private UsbDeviceProperty[] GetProperties() { UInt32 propertyKeyCount; Boolean success = UsbDeviceWinApi.SetupDiGetDevicePropertyKeys(this.handle, ref this.devInfoData, IntPtr.Zero, 0, out propertyKeyCount, 0); if (success || (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INSUFFICIENT_BUFFER)) { this.TraceError("SetupDiGetDevicePropertyKeys"); return(new UsbDeviceProperty[0]); } if (0 == propertyKeyCount) { return(new UsbDeviceProperty[0]); } List <UsbDeviceProperty> properties = new List <UsbDeviceProperty>(); UsbDeviceWinApi.DEVPROPKEY[] propertyKeyArray = new UsbDeviceWinApi.DEVPROPKEY[propertyKeyCount]; GCHandle propertyKeyArrayPinned = GCHandle.Alloc(propertyKeyArray, GCHandleType.Pinned); IntPtr buffer = propertyKeyArrayPinned.AddrOfPinnedObject(); success = UsbDeviceWinApi.SetupDiGetDevicePropertyKeys(this.handle, ref this.devInfoData, buffer, propertyKeyCount, out propertyKeyCount, 0); if (success) { for (UInt32 propertyKeyIndex = 0; propertyKeyIndex < propertyKeyCount; propertyKeyIndex++) { UInt32 propertyType; UInt32 requiredSize; success = UsbDeviceWinApi.SetupDiGetDevicePropertyW(this.handle, ref this.devInfoData, ref propertyKeyArray[propertyKeyIndex], out propertyType, IntPtr.Zero, 0, out requiredSize, 0); if (success || (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INSUFFICIENT_BUFFER)) { this.TraceError("SetupDiGetDeviceProperty"); success = false; } else { buffer = Marshal.AllocHGlobal((Int32)requiredSize); success = UsbDeviceWinApi.SetupDiGetDevicePropertyW(this.handle, ref this.devInfoData, ref propertyKeyArray[propertyKeyIndex], out propertyType, buffer, requiredSize, out requiredSize, 0); if (success) { Object value = this.MarshalDeviceProperty(buffer, (Int32)requiredSize, propertyType); properties.Add(new UsbDeviceProperty(propertyKeyArray[propertyKeyIndex], value, propertyType)); } else { this.TraceError("SetupDiGetDevicePropertyW"); } Marshal.FreeHGlobal(buffer); } if (!success) // don't combine with previous "if", covers 2 cases { properties.Add(new UsbDeviceProperty(propertyKeyArray[propertyKeyIndex], null, UsbDeviceWinApi.DevicePropertyTypes.DEVPROP_TYPE_EMPTY)); } } } else { this.TraceError("SetupDiGetDevicePropertyKeys"); } propertyKeyArrayPinned.Free(); return(properties.ToArray()); }