public int GetReportLength(ReportType type, byte reportId) { int bitLength = 0; List <ValueCaps> caps = null; switch (type) { case ReportType.Input: caps = inputValueCaps; break; case ReportType.Output: caps = outputValueCaps; break; case ReportType.Feature: caps = featureValueCaps; break; } if (caps == null) { return(0); } for (int i = 0; i < caps.Count; i++) { ValueCaps c = caps[i]; if (c.ReportID == reportId) { bitLength += c.ReportCount * c.BitSize; } } return(bitLength / 8); }
public HidDevice(string path) { //device mutex lockObject = new object(); //static buffers strBuffer = new byte[256]; //open device reference = Kernel32.File.Create(path, Kernel32.FileAccess.GenericWrite, Kernel32.FileShare.Read | Kernel32.FileShare.Write, IntPtr.Zero, CreationDisposition.OpenExisting, Kernel32.FileAttributes.Overlapped, IntPtr.Zero); if (reference.IsInvalid) { return; } //overlapped fileEvent = new ManualResetEvent(false); fileOverlapped = new SafeOverlapped(fileEvent); //get preparsed data IntPtr preparsed; if (!NativeMethods.HidD_GetPreparsedData(reference, out preparsed)) { return; } preparsedData = new PreparsedData(preparsed); //transfer buffers Caps capabilities = preparsedData.Capabilities; inBuffer = new byte[capabilities.InputReportByteLength]; outBuffer = new byte[capabilities.OutputReportByteLength]; featureBuffer = new byte[capabilities.FeatureReportByteLength]; //dynamic caps inputValueCaps = new List <ValueCaps>(); outputValueCaps = new List <ValueCaps>(); featureValueCaps = new List <ValueCaps>(); ushort numInputCaps = capabilities.NumberInputValueCaps; ushort numOutputCaps = capabilities.NumberOutputValueCaps; ushort numFeatureCaps = capabilities.NumberFeatureValueCaps; ushort maxValueCaps = Math.Max(numInputCaps, numOutputCaps); maxValueCaps = Math.Max(maxValueCaps, numFeatureCaps); int valueCapSize = Marshal.SizeOf <ValueCaps>(); IntPtr valueCapPtr = Marshal.AllocHGlobal(maxValueCaps * valueCapSize); HidPStatus stat = NativeMethods.HidP_GetValueCaps(ReportType.Input, valueCapPtr, ref numInputCaps, preparsed); for (int i = 0; i < numInputCaps; i++) { ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + (i * valueCapSize))); inputValueCaps.Add(val); } stat = NativeMethods.HidP_GetValueCaps(ReportType.Output, valueCapPtr, ref numOutputCaps, preparsed); for (int i = 0; i < numOutputCaps; i++) { ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + i * valueCapSize)); outputValueCaps.Add(val); } stat = NativeMethods.HidP_GetValueCaps(ReportType.Feature, valueCapPtr, ref numFeatureCaps, preparsed); for (int i = 0; i < numFeatureCaps; i++) { ValueCaps val = Marshal.PtrToStructure <ValueCaps>(new IntPtr(valueCapPtr.ToInt64() + i * valueCapSize)); featureValueCaps.Add(val); } Marshal.FreeHGlobal(valueCapPtr); }