Click here to Skip to main content
11,480,626 members (61,969 online)
Click here to Skip to main content
Add your own
alternative version

Implement an Autoplay Handler

, 18 Sep 2006 CPOL 73.4K 1.4K 77
Implementing an Autoplay handler in C#.
autoplaydemo_demo.zip
Almdal.RegistryScript.dll
Almdal.SharedPipes.dll
AutoPlayDemo.exe
AutoPlayListener.dll
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.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using Microsoft.Win32;

using Almdal.SharedPipes;

/*****************************************************************************************
 *  Copyright (c) 2006 T. ALmdal
 *
 *  Permission is hereby granted, free of charge, to any person obtaining 
 *  a copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation 
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 *  and/or sell copies of the Software, and to permit persons to whom the 
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included 
 *  in all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
 *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
/*******************************************************************************************/
namespace Almdal.AutoPlayDemo {
	public partial class Form1 : Form {
        protected delegate void AppendText(String text);
        protected AppendText appendText;

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern uint RegisterWindowMessage(string lpString);

        /// <summary>
        /// This message is sent to the main window, when new files are detected on removable media
        /// </summary>
        public static int START_LOAD = (int)RegisterWindowMessage("3B36DF78-BBA4-4d02-B50D-AF0FFB8DEF34");

        public Form1() {
			InitializeComponent();
            appendText = new AppendText(processList.AppendText);
        }

        /// <summary>
        /// Override the Window proce so we can notice when our registered message arrives
        /// </summary>
        /// <param name="m">The message to process</param>
		protected override void WndProc(ref Message m) {
			if (m.Msg == START_LOAD) {
				StringBuilder sb = new StringBuilder(processList.Text, 1024);
				sb.AppendLine("Start Picture Load Message received");
				processList.Text = sb.ToString();

                Thread readThread = new Thread(new ParameterizedThreadStart(RecieveData));
                readThread.Start(m.WParam);
			}
			else {
				base.WndProc(ref m);
			}
		}

        /// <summary>
        /// This is the delegate implementation that allows for the file names to be appended
        /// to the process list from the background processing thread.
        /// </summary>
        /// <param name="text">Text String to append</param>
        protected void AppendTextMethod(String text) {
            processList.AppendText(text);
        }

        /// <summary>
        /// Reads the paths from the pipe.
        /// </summary>
        /// <param name="handle">Handle to the read end of the anonymous pipe</param>
        public void RecieveData(object handle) {
            SharedPipe pipe = new SharedPipe((IntPtr)handle);
            StreamReader reader = pipe.Reader;
            while (!reader.EndOfStream) {
                String line = reader.ReadLine() + "\r\n";
                processList.Invoke(appendText, new object[] { line });
            }

            reader.Dispose();
        }
    }
}

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 | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 18 Sep 2006
Article Copyright 2006 by Tim Almdal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid