Click here to Skip to main content
15,883,901 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.2K   8.1K   146  
Ever wonder how programs like Nero work? They make their own SCSI libraries... like this!
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Helper;

namespace Scsi.Multimedia
{
	/// <summary>This feature identifies a drive that has a unique serial number. The vendor ID, model ID, and serial number is able to uniquely identify a drive that has this feature.</summary>
	[Description("This feature identifies a drive that has a unique serial number.\r\nThe vendor ID, model ID, and serial number is able to uniquely identify a drive that has this feature.")]
	[StructLayout(LayoutKind.Sequential, Pack = 1)]
	public class DriveSerialNumberFeature : MultimediaFeature
	{
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private static readonly IntPtr SERIAL_NUMBER_OFFSET = Marshal.OffsetOf(typeof(DriveSerialNumberFeature), "_SerialNumber");
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private static readonly ScsiCommandCode[] __SupportedOperations = Sort(new ScsiCommandCode[] { });

		public override FeatureSupportKind GetSupport(ScsiCommand command)
		{
			return Array.BinarySearch<ScsiCommandCode>(__SupportedOperations, command.OpCode) >= 0 ? FeatureSupportKind.Mandatory : FeatureSupportKind.None;
		}

		public DriveSerialNumberFeature() : base(FeatureCode.DriveSerialNumber) { }

		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
		private byte[] _SerialNumber;
		[Browsable(false)]
		public byte[] SerialNumber
		{
			get { return this._SerialNumber; }
			set
			{
				this._SerialNumber = value;
				this.AdditionalLength = (byte)(1 * (value == null ? 0 : value.Length));
			}
		}
		[DisplayName("Drive Serial Number")]
		public string StringSerialNumber { get { return System.BitConverter.ToString(this._SerialNumber); } }

		protected override void MarshalFrom(BufferWithSize buffer)
		{
			base.MarshalFrom(buffer);
			this._SerialNumber = new byte[this.AdditionalLength / 1];
			buffer.CopyTo(SERIAL_NUMBER_OFFSET, this._SerialNumber, IntPtr.Zero, (IntPtr)this._SerialNumber.Length);
		}

		protected override void MarshalTo(BufferWithSize buffer)
		{
			base.MarshalTo(buffer);
			buffer.CopyFrom(SERIAL_NUMBER_OFFSET, this._SerialNumber, IntPtr.Zero, (IntPtr)this._SerialNumber.Length);
		}
	}
}

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