Click here to Skip to main content
15,885,878 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.3K   8.1K   146  
Ever wonder how programs like Nero work? They make their own SCSI libraries... like this!
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
namespace System.Shell
{
	[ComImport, Guid("000214E6-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
	public interface IShellFolder
	{
		void ParseDisplayName([In, Optional] IntPtr hwnd, [In, Optional, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc, [In] string pszDisplayName, [Out] out int pchEaten, [Out, Optional] out ShellItemIdList ppidl, [In, Out] ref SFGAO pdwAttributes);
		/// <param name="grfFlags">SHCONTF</param>
		/// <param name="ppenumIDList">IEnumIDList</param>
		void EnumObjects([In, Optional] IntPtr hWnd, [In] SHCONTF grfFlags, [Out, Optional, MarshalAs(UnmanagedType.Interface)] out IEnumIDList ppenumIDList);
		void BindToObject([In] ShellItemIdList pidl, [In, Optional, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc, [In] ref Guid riid, [Out, Optional, MarshalAs(UnmanagedType.Interface)] out object ppv);
		void BindToStorage([In] ShellItemIdList pidl, [In, Optional, MarshalAs(UnmanagedType.Interface)] IBindCtx pbc, [In] ref Guid riid, [Out, Optional, MarshalAs(UnmanagedType.Interface)] out object ppv);
		[PreserveSig]
		int CompareIDs([In] IntPtr lParam, [In] ShellItemIdList pidl1, [In] ShellItemIdList pidl2);
		void CreateViewObject([In, Optional] IntPtr hWndOwner, [In] ref Guid riid, [Out, Optional, MarshalAs(UnmanagedType.Interface)] out object ppv);
		void GetAttributesOf([In] int cidl, [In, Optional, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] IntPtr[] apidl, [In, Out] ref SFGAO rgfInOut);
		unsafe void GetUIObjectOf([In, Optional] IntPtr hWndOwner, [In] int cidl, [In, Optional, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] IntPtr[] apidl, [In] ref Guid riid, [In, Out] int* rgfReserved, [Out, Optional, MarshalAs(UnmanagedType.Interface)] out object ppv);
		/// <param name="pName">out STRRET</param>
		void GetDisplayNameOf([In] ShellItemIdList pidl, [In] SHGDN uFlags, [Out] out STRRET pName);
		void SetNameOf([In, Optional] IntPtr hWnd, [In] ShellItemIdList pidl, [In] string pszName, [In] SHGDN uFlags, [Out, Optional] out ShellItemIdList ppidlOut);
	}

	public static class ShellFolders
	{
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private static IShellFolder __Desktop;
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		private static IShellFolder __Computer;

		public static IShellFolder Desktop { get { if (__Desktop == null) { Interlocked.CompareExchange(ref __Desktop, ShellUtil.SHGetDesktopFolder(), null); } return __Desktop; } }

		public static IShellFolder Computer
		{
			get
			{
				if (__Computer == null)
				{
					object computerObj;
					var guid = ShellUtil.IID_IShellFolder;
					Desktop.BindToObject(ShellItemIdList.Computer, null, ref guid, out computerObj);
					Interlocked.CompareExchange(ref __Computer, (IShellFolder)computerObj, null);
				}
				return __Computer;
			}
		}

		public static ShellItemIdList[] EnumObjects(IShellFolder folder, IntPtr hWnd, SHCONTF grfFlags)
		{
			IEnumIDList enumerator;
			folder.EnumObjects(hWnd, grfFlags, out enumerator);
			var items = new System.Collections.Generic.List<ShellItemIdList>();
			var buffer = new IntPtr[1024];
			int fetched;
			for (; ; )
			{
				int hr = enumerator.Next(buffer.Length, buffer, out fetched);
				if (hr != 0 && hr != 1) { Marshal.ThrowExceptionForHR(hr); }
				for (int i = 0; i < fetched; i++) { items.Add(new ShellItemIdList(buffer[i], true)); }
				if (hr != 0) { break; }
				if (fetched == buffer.Length) { buffer = new IntPtr[2 * buffer.Length]; }
			}
			return items.ToArray();
		}

		public static int CompareIDs(IShellFolder folder, ShellItemIdList left, ShellItemIdList right, ShellIdentifierComparison comparison, ShellIdentifierComparisonModifier modifier)
		{
			int c = folder.CompareIDs((IntPtr)((int)comparison | (int)modifier), left, right);
			if (c < 0) { Marshal.ThrowExceptionForHR(c); }
			return unchecked((short)(c & 0x0000FFFF));
		}

		internal static string GetPersistFolderPath(IShellFolder folder) { ShellItemIdList id; ((IPersistFolder2)folder).GetCurFolder(out id); using (id) { return id.ToString(); } }
	}
}

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