Click here to Skip to main content
12,748,236 members (39,409 online)
Click here to Skip to main content


78 bookmarked
Posted 18 Sep 2006

Implement an Autoplay Handler

, 18 Sep 2006 CPOL
Implementing an Autoplay handler in C#.
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);

		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

		#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);

		#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));

		#region IDisposableImpl Overrides

		/// <summary>
		/// Override to release the Managed Resources
		/// </summary>
		protected override void DisposeManagedResources() {


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tim Almdal
Canada Canada
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170215.1 | Last Updated 18 Sep 2006
Article Copyright 2006 by Tim Almdal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid