示例#1
0
        protected bool SCSICommand(IntPtr handle, byte cdbLength, byte[] cdb, int dataLength, ref byte[] dataBuffer, byte dataInOut)
        {
            bool   status;
            Int32  nBytesReturned;
            IntPtr result = IntPtr.Zero;
            int    errorMessage;

            SCSI_PASS_THROUGH_WITH_BUFFERS info = new SCSI_PASS_THROUGH_WITH_BUFFERS();

            info.sptd.cdb                = new byte[cdbLength];
            info.sptd.cdb                = cdb;
            info.sptd.Cdblength          = cdbLength;
            info.sense                   = new byte[32];
            info.sptd.Length             = (ushort)Marshal.SizeOf(typeof(SCSI_PASS_THROUGH_DIRECT));
            info.sptd.SenseInfoOffset    = (uint)Marshal.OffsetOf(typeof(SCSI_PASS_THROUGH_WITH_BUFFERS), "sense");
            info.sptd.SenseInfoLength    = 32;
            info.sptd.DataTransferLength = (uint)dataLength;
            info.sptd.DataBufferOffset   = Marshal.AllocHGlobal(dataLength);
            info.sptd.TimeOutValue       = 3;
            info.sptd.DataIn             = dataInOut;

            if (dataInOut == SCSI_IOCTL_DATA_OUT)
            {
                Marshal.Copy(dataBuffer, 0, info.sptd.DataBufferOffset, dataLength);
            }

            result = Marshal.AllocHGlobal(Marshal.SizeOf(info));
            Marshal.StructureToPtr(info, result, false);

            status = DeviceIoControl(handle,
                                     (int)IOCTL_SCSI_PASS_THROUGH_DIRECT,
                                     result,
                                     Marshal.SizeOf(info),
                                     result,
                                     Marshal.SizeOf(info),
                                     out nBytesReturned,
                                     IntPtr.Zero);

            if (status == false)
            {
                errorMessage = Marshal.GetLastWin32Error();
                return(false);
            }

            info = (SCSI_PASS_THROUGH_WITH_BUFFERS)Marshal.PtrToStructure(result, typeof(SCSI_PASS_THROUGH_WITH_BUFFERS));

            Marshal.Copy(info.sptd.DataBufferOffset, dataBuffer, 0, dataLength);

            Marshal.FreeHGlobal(result);
            Marshal.FreeHGlobal(info.sptd.DataBufferOffset);
            return(true);
        }
示例#2
0
        public static bool Cmd(IntPtr handle, byte cdbLength, byte[] cdb, int dataLength, ref byte[] dataBuffer, byte dataInOut)
        {
            bool   status;
            Int32  nBytesReturned;
            IntPtr result = IntPtr.Zero;
            int    errorMessage;

            SCSI_PASS_THROUGH_WITH_BUFFERS info = new SCSI_PASS_THROUGH_WITH_BUFFERS();

            info.sptd.cdb                = new byte[cdbLength];
            info.sptd.cdb                = cdb;
            info.sptd.Cdblength          = cdbLength;
            info.sense                   = new byte[32];
            info.sptd.Length             = (ushort)Marshal.SizeOf(typeof(SCSI_PASS_THROUGH_DIRECT));
            info.sptd.SenseInfoOffset    = (uint)Marshal.OffsetOf(typeof(SCSI_PASS_THROUGH_WITH_BUFFERS), "sense");
            info.sptd.SenseInfoLength    = 32;
            info.sptd.DataTransferLength = (uint)dataLength;
            info.sptd.DataBufferOffset   = Marshal.AllocHGlobal(dataLength);
            info.sptd.TimeOutValue       = 3;
            info.sptd.DataIn             = dataInOut;

            if (dataInOut == SCSI_IOCTL_DATA_OUT)
            {
                Marshal.Copy(dataBuffer, 0, info.sptd.DataBufferOffset, dataLength);
            }

            result = Marshal.AllocHGlobal(Marshal.SizeOf(info));
            Marshal.StructureToPtr(info, result, false);

            status = DeviceIoControl(handle,
                                     (int)IOCTL_SCSI_PASS_THROUGH_DIRECT,
                                     result,
                                     Marshal.SizeOf(info),
                                     result,
                                     Marshal.SizeOf(info),
                                     out nBytesReturned,
                                     IntPtr.Zero);

            if (status == false)
            {
                errorMessage = Marshal.GetLastWin32Error();
                StreamWriter log  = new StreamWriter("log.txt", true);
                DateTime     time = DateTime.Now;
                log.WriteLine(time.Hour.ToString() + ":" + time.Minute.ToString() + ":" + time.Second.ToString() + " Write IO Control Fail, Error Message is:" + errorMessage.ToString());
                log.Close();
            }

            info = (SCSI_PASS_THROUGH_WITH_BUFFERS)Marshal.PtrToStructure(result, typeof(SCSI_PASS_THROUGH_WITH_BUFFERS));

            //if(dataInOut == SCSI_IOCTL_DATA_IN)
            Marshal.Copy(info.sptd.DataBufferOffset, dataBuffer, 0, dataLength);

            Marshal.FreeHGlobal(result);
            Marshal.FreeHGlobal(info.sptd.DataBufferOffset);

            if (status == false)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }