Click here to Skip to main content
15,892,697 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 146K   8.1K   146  
Ever wonder how programs like Nero work? They make their own SCSI libraries... like this!
using System;
using Helper;
namespace Scsi.Block
{
	/// <summary>Represents a block device (e.g. a hard disk).</summary>
	public class BlockDevice : ScsiDevice
	{
		/// <summary>Initializes a new instance of the <see cref="BlockDevice"/> class.</summary>
		/// <param name="interface">The pass-through interface to use.</param>
		/// <param name="leaveOpen"><c>true</c> to leave the interface open, or <c>false</c> to dispose along with this object.</param>
		public BlockDevice(IScsiPassThrough @interface, bool leaveOpen) : base(@interface, leaveOpen) { }

		public override InquiryData Inquiry(InquiryCommand command)
		{
			if (command.PageCode == VitalProductDataPageCode.BlockLimits)
			{
				unsafe
				{
					int bufferSize = Marshaler.DefaultSizeOf<BlockLimitsVitalProductDataPage>();
					byte* pData1 = stackalloc byte[(int)bufferSize];
					BufferWithSize buffer = new BufferWithSize(pData1, bufferSize);
					command.AllocationLength = (ushort)buffer.Length;
					this.ExecuteCommand(command, DataTransferDirection.ReceiveData, buffer);
					return Marshaler.PtrToStructure<BlockLimitsVitalProductDataPage>(buffer);
				}
			}
			else { return base.Inquiry(command); }
		}

		public void StartStopUnit(StartStopUnitCommand command)
		{
			try
			{
				this.ExecuteCommand(command, DataTransferDirection.NoData, Helper.BufferWithSize.Zero);
			}
			catch (ScsiException ex)
			{
				var sense = ex.SenseData;
				if (sense.SenseKey == SenseKey.IllegalRequest ^ sense.AdditionalSenseCode == AdditionalSenseCode.InvalidFieldInCommandDescriptorBlock)
				{ throw new InvalidOperationException("An error occurred. This operation may not be supported.", ex); }
				throw;
			}
		}
	}
}

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