Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Implement an Autoplay Handler

, 18 Sep 2006
Implementing an Autoplay handler in C#.
autoplaydemo_demo.zip
autoplaydemo_src.zip
AutoPlayDemo
Autoplay.ico
AutoPlayDemo.csproj.user
bin
Properties
Settings.settings
Resources
AutoPlayListener
Autoplay.ico
AutoPlayListener.snk
bin
Properties
Resources
Autoplay.ico
RegistryScript
Almdal.RegistryScript.snk
bin
Properties
SharedPipies
Almdal.SharedPipes.snk
bin
Properties
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

using Microsoft.Win32.SafeHandles;

/*******************************************************************
 *	Author: Tim Almdal
 *	Date: Sept, 2006
 *			tnalmdal (at) shaw-dot-net
 * 
 *	SharedPipe class. You can use this class in any way you want
 *	as long as this header remains in this file and you don't blame
 *	when it doesn't work :-)
 * 
 *******************************************************************/
namespace Almdal.SharedPipes {

	public class SharedPipe : IDisposableImpl {

		#region Windows Entry Points
		[DllImport("kernel32.dll", SetLastError = true)]
		static extern bool CreatePipe(out SafeFileHandle hReadPipe, out SafeFileHandle hWritePipe,
									  //ref SECURITY_ATTRIBUTES lpPipeAttributes, uint nSize);
									  IntPtr lpPipeAttributes, uint nSize);

		[DllImport("kernel32.dll", SetLastError = true)]
		static extern bool DuplicateHandle(IntPtr hSourceProcessHandle,
											SafeFileHandle hSourceHandle, IntPtr hTargetProcessHandle, out IntPtr lpTargetHandle,
											uint dwDesiredAccess, bool bInheritHandle, uint dwOptions);
		#endregion

		public enum Direction {Read, Write};

		#region Member Variables
		private SafeFileHandle _hReadPipe;			//	Handle to read end of the pipe.
		private SafeFileHandle _hWritePipe;			//	Handle to the write end of the pipe.
		private Direction _direction;				//	Direction that this instance is working with
		#endregion

		#region Constructors
		/// <summary>
		/// This constructor creates the Write End of an anonymous pipe.  Typically used by
		/// the server side of the pipe.
		/// </summary>
		public SharedPipe() {
			if (CreatePipe(out _hReadPipe, out _hWritePipe, IntPtr.Zero, 0) == false) {
				throw new Win32Exception(Marshal.GetLastWin32Error());
			}

			_direction = Direction.Write;
		}

		/// <summary>
		/// This constructor creates the Client or Read end of the pipe.
		/// </summary>
		/// <param name="hReadPipe">Handle to the read end of the pipe</param>
		public SharedPipe(IntPtr hReadPipe) {
			_hReadPipe = new SafeFileHandle(hReadPipe, true);
			_direction = Direction.Read;
			_hWritePipe = new SafeFileHandle(IntPtr.Zero, true);
		}
		#endregion

		#region Pipe Access Methods
		/// <summary>
		/// This method returns a duplicate handle, in the target process, to the read end of the pipe.  It is only
		/// available when the instance of the pipe is in read mode.
		/// </summary>
		/// <param name="targetProcess">This is process that the handle needs to be mapped to</param>
		/// <returns>The mapped Read handle.</returns>
		public IntPtr ReadHandle(IntPtr targetProcess) {
			Debug.Assert(_direction == Direction.Write, "The Shared Pipe is in Read mode, cannot reuse the read handle.");

			IntPtr targetHandle;
			DuplicateHandle(Process.GetCurrentProcess().Handle, _hReadPipe, targetProcess, out targetHandle, 0, false, 3);
			return targetHandle;
		}

		/// <summary>
		/// Read only property to expose a Stream Writer on the write end of the pipe.
		/// </summary>
		public StreamWriter Writer {
			get {
				Debug.Assert(_direction == Direction.Write, "The Shared Pipe is in Read mode, cannot get Writer.");
				return new StreamWriter(new FileStream(_hWritePipe, FileAccess.Write));
			}
		}

		/// <summary>
		/// Read only property to expose a Stream Reader on the read end of the pipe.
		/// </summary>
		public StreamReader Reader {
			get {
				Debug.Assert(_direction == Direction.Read, "The Shared Pipe is in Write mode, cannot get Reader.");
				return new StreamReader(new FileStream(_hReadPipe, FileAccess.Read));
			}
		}
		#endregion

		#region IDisposableImpl Overrides

		/// <summary>
		/// Override to release the Managed Resources
		/// </summary>
		protected override void DisposeManagedResources() {
			_hReadPipe.Dispose();
			_hWritePipe.Dispose();
		}

		#endregion
	}
}

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 Code Project Open License (CPOL)

Share

About the Author

Tim Almdal

Canada Canada
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 18 Sep 2006
Article Copyright 2006 by Tim Almdal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid