Exemplo n.º 1
0
        private unsafe int ReadUnsafe(byte[] buffer, int offset, int count, int timeout)
        {
            var  length     = Convert.ToUInt32(count);
            uint bytes_read = 0;

            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            if (offset + count > buffer.Length)
            {
                throw new ArgumentException("The sum of offset and count is larger than the buffer length");
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset));
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));

                fixed(byte *p = &buffer[offset])
                {
                    IntPtr ptr = (IntPtr)p;
                    var    ret = NativeApi.DataRead(_handle, ptr, length, timeout, out bytes_read);

                    if (ret < NativeApi.InspectionError.None)
                    {
                        throw ExceptionFactory.CreateException(ret);
                    }
                }

                return((int)bytes_read);
        }