Click here to Skip to main content
15,894,907 members
Articles / Programming Languages / C#

Implement an Autoplay Handler

Rate me:
Please Sign up or sign in to vote.
5.00/5 (22 votes)
18 Sep 2006CPOL8 min read 118.7K   1.7K   78  
Implementing an Autoplay handler in C#.
using System;
using System.Runtime.InteropServices;
using System.Text;

/*****************************************************************************************
 *  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.AutoPlayListener {
	/// <summary>
	/// Pointer to a Shell ITEMIDLIST structure, which s used to identify objects in the Shell namespace
	/// </summary>
    public class PIdl : IDisposable {
        public IntPtr _pidl;

        public PIdl() { _pidl = IntPtr.Zero; }
        public PIdl(IntPtr pidl) { _pidl = pidl; }

        public void Dispose() {
            if (!IntPtr.Zero.Equals(_pidl)) {       //  only release the storage if the pointer is valid
                IMalloc malloc = null;
                WinApi.SHGetMalloc(out malloc);
                try {
                    malloc.Free((IntPtr)_pidl);
                } finally {
                    Marshal.ReleaseComObject(malloc);
                }
            }
        }

        public static IntPtr NextPIDL(IntPtr ptr) {
            if (!IntPtr.Zero.Equals(ptr)) {
                short cb = Marshal.ReadInt16(ptr);

                return new IntPtr((int)ptr + cb);
                // must have 'int' here
                // it changes the IntPtr myIntPtr
                // to 'int', a memory address
            }
            else {
                return ptr;
            }
        }

        public int Count {
            get {
                int Result = 0;
                IntPtr IDList = _pidl;
                if (IDList != IntPtr.Zero) {
                    short cb = Marshal.ReadInt16(IDList);
                    while (cb != 0) {
                        Result++;
                        IDList = NextPIDL(IDList);
                        cb = Marshal.ReadInt16(IDList);
                    }
                }
                return Result;
            }
        }

        public bool isNull() { return IntPtr.Zero.Equals(_pidl); }

        public static explicit operator IntPtr(PIdl p) { return p._pidl; }
        public static explicit operator PIdl(IntPtr p) { return new PIdl(p); }

        public IntPtr Detach() {
            IntPtr pidl = _pidl;
            _pidl = IntPtr.Zero;
            return pidl;
        }
    }

}

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)


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions