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

.NET Shell Extensions - Shell Drop Handlers

, 19 Jan 2013
Rapidly create Shell Drop Handler Extensions using .NET
SharpShell_Core_Library.zip
SharpShell_Source_Code.zip
SharpShell Source Code
Dependencies
Apex
Apex.WinForms.dll
SharpShell
packages
Apex.1.5.0
Apex.1.5.0.nupkg
lib
net35
Apex.dll
net40
Apex.dll
sl30
Apex.Silverlight.dll
sl3-wp
Apex.WP7.dll
sl40
Apex.Silverlight4.dll
sl4-wp71
Apex.WP7.dll
sl50
Apex.Silverlight.dll
NUnit.2.6.2
lib
nunit.framework.dll
NUnit.2.6.2.nupkg
Samples
ContextMenu
CountLinesExtension
bin
CountLinesExtension.csproj.vspscc
Key.snk
Properties
Resources
CountLines.bmp
DropHandler
XsdDropHandler
bin
ExampleFiles
Key.snk
Properties
XsdDropHandler.csproj.vspscc
IconHandler
DllIconHandler
bin
DllIconHandler.csproj.vspscc
Key.snk
Properties
Resources
ManagedDll.ico
NativeDll.ico
IconOverlayHandler
ReadOnlyFileIconOverlayHandler
bin
Key.snk
Properties
ReadOnlyFileIconOverlayHandler.csproj.vspscc
Resources
ReadOnly.ico
InfoTipHandler
FolderInfoTipHandler
FolderInfoTipHandler.csproj.vspscc
Key.snk
Properties
PropertySheet
DrivePropertySheet
DrivePropertySheet.csproj.vspscc
Key.snk
Properties
TextFilePropertySheet
Key.snk
Properties
TextFilePropertySheet.csproj.vspscc
SharpShell
Attributes
Diagnostics
Extensions
Interop
Key.snk
NativeBridge
SharpShellNativeBridge32.dll
SharpShellNativeBridge64.dll
Properties
ServerRegistration
SharpContextMenu
SharpDropHandler
SharpIconHandler
SharpIconOverlayHandler
SharpInfoTipHandler
SharpPropertySheet
SharpShell.csproj.vspscc
SharpShellNativeBridge
SharpShellNativeBridge.vcxproj.filters
SharpShellNativeBridge.vcxproj.user
SharpShellNativeBridge.vcxproj.vspscc
Tools
ServerInspector
Properties
Settings.settings
ServerInspector.csproj.vspscc
Servers
ServerManager
About
LogView
Properties
Settings.settings
Resources
ChangeYourView.png
ContextMenu.png
Icon.png
openHS.png
PlayHS.png
PropertySheet.png
View_Details.png
View_LargeIcons.png
View_List.png
View_SmallIcons.png
View_Tiles.png
ServerDetails
ServerManager.csproj.vspscc
SharpShell.ico
TestShell
ShellExtensionManager
Properties
Settings.settings
ShellExtensionManager.csproj.vspscc
SharpShell_Tools.zip
SharpShell Tools
Apex.dll
Apex.WinForms.dll
ServerInspector.exe
ServerManager.exe
SharpShell.dll
System.Windows.Interactivity.dll
using System;
using System.ComponentModel.Composition;
using System.Runtime.InteropServices;
using SharpShell.Attributes;
using SharpShell.Diagnostics;
using SharpShell.ServerRegistration;

namespace SharpShell
{
    /// <summary>
    /// The SharpShellServer class is the base class for all SharpShellServers.
    /// It provides the core standard functionality - registration, unregistration,
    /// identity information (as required by ISharpShellServer), MEF contract inheritance
    /// and definitions of virtual functions that can be overriden by advanced users
    /// to hook into key points in Server Lifecycle.
    /// 
    /// Note that ALL derived classes will Export ISharpShellServer - this is a useful
    /// feature as it means that the ServerManager tool (and other tools) can interrogate
    /// assemblies via MEF to get information on servers they contain.
    /// </summary>
    [InheritedExport(typeof(ISharpShellServer))]
    public abstract class SharpShellServer : ISharpShellServer
    {
        /// <summary>
        /// The COM Register function. Called by regasm to register a COM server
        /// in the system. This function will register the server associations defined
        /// by the type's COMServerAssociation attributes.
        /// </summary>
        /// <param name="type">The type.</param>
        [ComRegisterFunction]
        internal static void Register(Type type)
        {
            //  Register the type, use the operating system architecture to determine
            //  what registration type to perform.
            DoRegister(type, Environment.Is64BitOperatingSystem ? RegistrationType.OS64Bit : RegistrationType.OS32Bit);
        }

        /// <summary>
        /// The COM Unregister function. Called by regasm to unregister a COM server
        /// in the system. This function will unregister the server associations defined
        /// by the type's COMServerAssociation attributes.
        /// </summary>
        /// <param name="type">The type.</param>
        [ComUnregisterFunction]
        internal static void Unregister(Type type)
        {
            //  Unregister the type, use the operating system architecture to determine
            //  what registration type to unregister.
            DoUnregister(type, Environment.Is64BitOperatingSystem ? RegistrationType.OS64Bit : RegistrationType.OS32Bit);
        }

        /// <summary>
        /// Actually performs registration. The ComRegisterFunction decorated method will call this function
        /// internally with the flag appropriate for the operating system processor architecture.
        /// However, this function can also be called manually if needed.
        /// </summary>
        /// <param name="type">The type of object to register, this must be a SharpShellServer derived class.</param>
        /// <param name="registrationType">Type of the registration.</param>
        internal static void DoRegister(Type type, RegistrationType registrationType)
        {
            //  Get the assoication data.
            var associationType = COMServerAssociationAttribute.GetAssociationType(type);
            var associations = COMServerAssociationAttribute.GetAssociations(type);

            //  Get the server type.
            var serverType = ServerTypeAttribute.GetServerType(type);

            //  Register the server associations, if there are any.
            if (associationType != AssociationType.None)
            {
                ServerRegistrationManager.RegisterServerAssociations(
                    type.GUID, serverType, type.Name, associationType, associations, registrationType);
            }

            //  Execute the custom register function, if there is one.
            CustomRegisterFunctionAttribute.ExecuteIfExists(type, registrationType);
        }

        /// <summary>
        /// Actually performs unregistration. The ComUnregisterFunction decorated method will call this function
        /// internally with the flag appropriate for the operating system processor architecture.
        /// However, this function can also be called manually if needed.
        /// </summary>
        /// <param name="type">The type of object to unregister, this must be a SharpShellServer derived class.</param>
        /// <param name="registrationType">Type of the registration to unregister.</param>
        internal static void DoUnregister(Type type, RegistrationType registrationType)
        {
            //  Get the assoication data.
            var associationType = COMServerAssociationAttribute.GetAssociationType(type);
            var associations = COMServerAssociationAttribute.GetAssociations(type);

            //  Get the server type.
            var serverType = ServerTypeAttribute.GetServerType(type);
            
            //  Unregister the server associations, if there are any.
            if (associationType != AssociationType.None)
            {
                ServerRegistrationManager.UnregisterServerAssociations(
                    type.GUID, serverType, type.Name, associationType, associations, registrationType);
            }

            //  Execute the custom unregister function, if there is one.
            CustomUnregisterFunctionAttribute.ExecuteIfExists(type, registrationType);
        }

        /// <summary>
        /// Logs the specified message to the SharpShell log, with the name of the type.
        /// </summary>
        /// <param name="message">The message.</param>
        protected virtual void Log(string message)
        {
            //  Log the message, but put our type name first.
            Logging.Log(GetType().Name + ": " + message);
        }

        /// <summary>
        /// Logs the specified message to the SharpShell log as an error, with the name of the type.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="exception">The exception.</param>
        protected virtual void LogError(string message, Exception exception = null)
        {
            //  Log the error, but put our type name first.
            Logging.Error(GetType().Name + ": " + message, exception);
        }

        /// <summary>
        /// Gets the type of the server.
        /// </summary>
        /// <value>
        /// The type of the server.
        /// </value>
        public ServerType ServerType
        {
            get
            {
                //  Just return the ServerTypeAttribute on the class (we must always have one somewhere in the hierarchy).
                return ServerTypeAttribute.GetServerType(GetType());
            }
        }

        /// <summary>
        /// Gets the server CLSID.
        /// </summary>
        public Guid ServerClsid
        {
            get { return GetType().GUID; }
        }
    }
}

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

Dave Kerr
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.
Follow on   Twitter

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 19 Jan 2013
Article Copyright 2013 by Dave Kerr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid