Exemple #1
0
        public bool ReadSerialNumber(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_GetSerialNumberString(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);
        }
Exemple #2
0
        public string readSerial()
        {
            if (serial != null)
            {
                return(serial);
            }

            if (Capabilities.InputReportByteLength == 64)
            {
                byte[] buffer = new byte[16];
                buffer[0] = 18;
                readFeatureData(buffer);
                serial = String.Format("{0:X02}:{1:X02}:{2:X02}:{3:X02}:{4:X02}:{5:X02}", buffer[6], buffer[5], buffer[4], buffer[3], buffer[2], buffer[1]);
                return(serial);
            }
            else
            {
                byte[] buffer = new byte[126];
                NativeMethods.HidD_GetSerialNumberString(safeReadHandle.DangerousGetHandle(), buffer, (ulong)buffer.Length);
                string MACAddr = System.Text.Encoding.Unicode.GetString(buffer).Replace("\0", string.Empty).ToUpper();
                MACAddr = String.Format("{0}{1}:{2}{3}:{4}{5}:{6}{7}:{8}{9}:{10}{11}",
                                        MACAddr[0], MACAddr[1], MACAddr[2], MACAddr[3], MACAddr[4],
                                        MACAddr[5], MACAddr[6], MACAddr[7], MACAddr[8],
                                        MACAddr[9], MACAddr[10], MACAddr[11]);
                serial = MACAddr;
                return(serial);
            }
        }
Exemple #3
0
        public bool ReadSerialNumber(out byte[] data)
        {
            data = new byte[64];
            IntPtr intPtr = IntPtr.Zero;
            bool   result = false;

            try
            {
                intPtr = OpenDeviceIO(_devicePath, 0u);
                result = NativeMethods.HidD_GetSerialNumberString(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);
        }