Ejemplo n.º 1
0
        public bool ReadProduct(out byte[] data)
        {
            data = new byte[254];
            IntPtr hidHandle = IntPtr.Zero;
            bool   success   = false;

            try
            {
                if (IsOpen)
                {
                    hidHandle = Handle;
                }
                else
                {
                    hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);
                }

                success = NativeMethods.HidD_GetProductString(hidHandle, ref data[0], data.Length);
            }
            catch (Exception exception)
            {
                throw new Exception(string.Format("Error accessing HID device '{0}'.", _devicePath), exception);
            }
            finally
            {
                if (hidHandle != IntPtr.Zero && hidHandle != Handle)
                {
                    CloseDeviceIO(hidHandle);
                }
            }

            return(success);
        }
Ejemplo n.º 2
0
        private string ReadProductString()
        {
            byte[] data      = new byte[254];
            IntPtr hidHandle = IntPtr.Zero;
            bool   success   = false;

            try
            {
                if (IsOpen && IsConnected)
                {
                    hidHandle = ReadHandle;
                }
                else
                {
                    hidHandle = OpenDeviceIO(_devicePath, NativeMethods.ACCESS_NONE);
                }

                success = NativeMethods.HidD_GetProductString(hidHandle, ref data[0], data.Length);
            }
            catch (Exception exception)
            {
                throw new Exception(string.Format("Error accessing HID device '{0}'.", _devicePath), exception);
            }
            finally
            {
                if (hidHandle != IntPtr.Zero && hidHandle != ReadHandle)
                {
                    CloseDeviceIO(hidHandle);
                }
            }

            return(Encoding.Unicode.GetString(data).TrimEnd('\0'));
        }
Ejemplo n.º 3
0
        public bool ReadProduct(out byte[] data)
        {
            data = new byte[64];
            IntPtr intPtr = IntPtr.Zero;
            bool   result = false;

            try
            {
                intPtr = OpenDeviceIO(_devicePath, 0u);
                result = NativeMethods.HidD_GetProductString(intPtr, ref data[0], data.Length);
            }
            catch (Exception innerException)
            {
                throw new Exception($"Error accessing HID device '{_devicePath}'.", innerException);
            }
            finally
            {
                if (intPtr != IntPtr.Zero)
                {
                    CloseDeviceIO(intPtr);
                }
            }
            return(result);
        }