Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#

SCSI Library in C# - Burn CDs and DVDs, Access Hard Disks, etc.

Rate me:
Please Sign up or sign in to vote.
4.77/5 (48 votes)
19 Jun 2017Ms-PL6 min read 145.1K   8.1K   146  
Ever wonder how programs like Nero work? They make their own SCSI libraries... like this!
using System.Runtime.InteropServices;
using System.Diagnostics;
using System;
using Helper;
namespace Scsi
{
	[StructLayout(LayoutKind.Sequential, Pack = 1)]
	public class BufferCombinedHeaderAndData : IMarshalable
	{
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private static readonly IntPtr DATA_OFFSET = Marshal.OffsetOf(typeof(BufferCombinedHeaderAndData), "_Data");
		public BufferCombinedHeaderAndData() { }

		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private byte byte0;
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private byte _byte1; //MSB
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private byte _byte2;
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private byte _byte3; //LSB
		public uint BufferCapacity
		{
			get { unchecked { return (uint)this._byte3 | ((uint)this._byte2 << 8) | ((uint)this._byte1 << 16); } }
			set { unchecked { this._byte3 = (byte)(value >> 0); this._byte2 = (byte)(value >> 8); this._byte1 = (byte)(value >> 16); } }
		}
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
		private byte[] _Data;
		public byte[] Data { get { return this._Data; } set { this._Data = value; this.BufferCapacity = value == null ? 0 : (uint)value.Length; } }

		internal static uint ReadBufferCapacity(IntPtr address) { unsafe { byte* ptr = (byte*)address; unchecked { return (uint)ptr[3] | ((uint)ptr[2] << 8) | ((uint)ptr[1] << 16); } } }

		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		protected virtual int MarshaledSize { get { return Marshal.SizeOf(this) + this.Data.Length - 1; } }

		protected virtual void MarshalFrom(BufferWithSize buffer)
		{
			Marshaler.DefaultPtrToStructure(buffer, this);
			this.Data = new byte[this.BufferCapacity];
			buffer.CopyTo(DATA_OFFSET, this.Data, IntPtr.Zero, (IntPtr)this.Data.Length);
		}

		protected virtual void MarshalTo(BufferWithSize buffer)
		{
			Marshaler.DefaultStructureToPtr((object)this, buffer);
			buffer.CopyFrom(DATA_OFFSET, this.Data, IntPtr.Zero, (IntPtr)this.Data.Length);
		}


		void IMarshalable.MarshalFrom(BufferWithSize buffer) { this.MarshalFrom(buffer); }
		void IMarshalable.MarshalTo(BufferWithSize buffer) { this.MarshalTo(buffer); }
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		int IMarshalable.MarshaledSize { get { return this.MarshaledSize; } }
	}

	[StructLayout(LayoutKind.Sequential, Pack = 1)]
	public struct ReadBufferDescriptor
	{
		public byte OffsetBoundary;
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private byte _byte1; //MSB
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private byte _byte2;
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private byte _byte3; //LSB
		public uint BufferCapacity
		{
			get { unchecked { return (uint)this._byte3 | ((uint)this._byte2 << 8) | ((uint)this._byte1 << 16); } }
			set { unchecked { this._byte3 = (byte)(value >> 0); this._byte2 = (byte)(value >> 8); this._byte1 = (byte)(value >> 16); } }
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions