CustomActionProxy.cs :  » Installers-Generators » WiX » Microsoft » Deployment » WindowsInstaller » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Installers Generators » WiX 
WiX » Microsoft » Deployment » WindowsInstaller » CustomActionProxy.cs
//---------------------------------------------------------------------
// <copyright file="CustomActionProxy.cs" company="Microsoft">
//    Copyright (c) Microsoft Corporation.  All rights reserved.
//    
//    The use and distribution terms for this software are covered by the
//    Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
//    which can be found in the file CPL.TXT at the root of this distribution.
//    By using this software in any fashion, you are agreeing to be bound by
//    the terms of this license.
//    
//    You must not remove this notice, or any other, from this software.
// </copyright>
// <summary>
// Part of the Deployment Tools Foundation project.
// </summary>
//---------------------------------------------------------------------

namespace Microsoft.Deployment.WindowsInstaller{
    using System;
    using System.IO;
    using System.Text;
    using System.Security;
    using System.Reflection;
    using System.Collections;
    using System.Configuration;
    using System.Runtime.InteropServices;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// Managed-code portion of the custom action proxy.
    /// </summary>
    internal static class CustomActionProxy
    {
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public static int InvokeCustomAction32(int sessionHandle, string entryPoint,
            int remotingDelegatePtr)
        {
            return CustomActionProxy.InvokeCustomAction(sessionHandle, entryPoint, new IntPtr(remotingDelegatePtr));
        }

        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public static int InvokeCustomAction64(int sessionHandle, string entryPoint,
            long remotingDelegatePtr)
        {
            return CustomActionProxy.InvokeCustomAction(sessionHandle, entryPoint, new IntPtr(remotingDelegatePtr));
        }

        /// <summary>
        /// Invokes a managed custom action method.
        /// </summary>
        /// <param name="sessionHandle">Integer handle to the installer session.</param>
        /// <param name="entryPoint">Name of the custom action entrypoint. This must
        /// either map to an entrypoint definition in the <c>customActions</c>
        /// config section, or be an explicit entrypoint of the form:
        /// &quot;AssemblyName!Namespace.Class.Method&quot;</param>
        /// <param name="remotingDelegatePtr">Pointer to a delegate used to
        /// make remote API calls, if this custom action is running out-of-proc.</param>
        /// <returns>The value returned by the custom action method,
        /// or ERROR_INSTALL_FAILURE if the custom action could not be invoked.</returns>
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public static int InvokeCustomAction(int sessionHandle, string entryPoint,
            IntPtr remotingDelegatePtr)
        {
            Session session = null;
            string assemblyName, className, methodName;
            MethodInfo method;

            try
            {
                MsiRemoteInvoke remotingDelegate = (MsiRemoteInvoke)
                    Marshal.GetDelegateForFunctionPointer(
                        remotingDelegatePtr, typeof(MsiRemoteInvoke));
                RemotableNativeMethods.RemotingDelegate = remotingDelegate;

                sessionHandle = RemotableNativeMethods.MakeRemoteHandle(sessionHandle);
                session = new Session((IntPtr) sessionHandle, false);
                if (String.IsNullOrEmpty(entryPoint))
                {
                    throw new ArgumentNullException("entryPoint");
                }

                if (!CustomActionProxy.FindEntryPoint(
                    session,
                    entryPoint,
                    out assemblyName,
                    out className,
                    out methodName))
                {
                    return (int) ActionResult.Failure;
                }
                session.Log("Calling custom action {0}!{1}.{2}", assemblyName, className, methodName);

                method = CustomActionProxy.GetCustomActionMethod(
                    session,
                    assemblyName,
                    className,
                    methodName);
                if (method == null)
                {
                    return (int) ActionResult.Failure;
                }
            }
            catch (Exception ex)
            {
                if (session != null)
                {
                    try
                    {
                        session.Log("Exception while loading custom action:");
                        session.Log(ex.ToString());
                    }
                    catch (Exception) { }
                }
                return (int) ActionResult.Failure;
            }

            try
            {
                // Set the current directory to the location of the extracted files.
                Environment.CurrentDirectory =
                    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                object[] args = new object[] { session };
                if (DebugBreakEnabled(new string[] { entryPoint, methodName }))
                {
                    System.Diagnostics.Debugger.Launch();
                }

                ActionResult result = (ActionResult) method.Invoke(null, args);
                session.Close();
                return (int) result;
            }
            catch (InstallCanceledException)
            {
                return (int) ActionResult.UserExit;
            }
            catch (Exception ex)
            {
                session.Log("Exception thrown by custom action:");
                session.Log(ex.ToString());
                return (int) ActionResult.Failure;
            }
        }

        /// <summary>
        /// Checks the "MMsiBreak" environment variable for any matching custom action names.
        /// </summary>
        /// <param name="names">List of names to search for in the environment
        /// variable string.</param>
        /// <returns>True if a match was found, else false.</returns>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        private static bool DebugBreakEnabled(string[] names)
        {
            string mmsibreak = Environment.GetEnvironmentVariable("MMsiBreak");
            if (mmsibreak != null)
            {
                foreach (string breakName in mmsibreak.Split(',', ';'))
                {
                    foreach (string name in names)
                    {
                        if (breakName == name)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Locates and parses an entrypoint mapping in CustomAction.config.
        /// </summary>
        /// <param name="session">Installer session handle, just used for logging.</param>
        /// <param name="entryPoint">Custom action entrypoint name: the key value
        /// in an item in the <c>customActions</c> section of the config file.</param>
        /// <param name="assemblyName">Returned display name of the assembly from
        /// the entrypoint mapping.</param>
        /// <param name="className">Returned class name of the entrypoint mapping.</param>
        /// <param name="methodName">Returned method name of the entrypoint mapping.</param>
        /// <returns>True if the entrypoint was found, false if not or if some error
        /// occurred.</returns>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        private static bool FindEntryPoint(
            Session session,
            string entryPoint,
            out string assemblyName,
            out string className,
            out string methodName)
        {
            assemblyName = null;
            className = null;
            methodName = null;

            string fullEntryPoint;
            if (entryPoint.IndexOf('!') > 0)
            {
                fullEntryPoint = entryPoint;
            }
            else
            {
                IDictionary config;
                try
                {
                    config = (IDictionary) ConfigurationManager.GetSection("customActions");
                }
                catch (ConfigurationException cex)
                {
                    session.Log("Error: missing or invalid customActions config section.");
                    session.Log(cex.ToString());
                    return false;
                }
                fullEntryPoint = (string) config[entryPoint];
                if (fullEntryPoint == null)
                {
                    session.Log(
                        "Error: custom action entry point '{0}' not found " +
                        "in customActions config section.",
                        entryPoint);
                    return false;
                }
            }

            int assemblySplit = fullEntryPoint.IndexOf('!');
            int methodSplit = fullEntryPoint.LastIndexOf('.');
            if (assemblySplit < 0 || methodSplit < 0 || methodSplit < assemblySplit)
            {
                session.Log("Error: invalid custom action entry point:" + entryPoint);
                return false;
            }

            assemblyName = fullEntryPoint.Substring(0, assemblySplit);
            className = fullEntryPoint.Substring(assemblySplit + 1, methodSplit - assemblySplit - 1);
            methodName = fullEntryPoint.Substring(methodSplit + 1);
            return true;
        }

        /// <summary>
        /// Uses reflection to load the assembly and class and find the method.
        /// </summary>
        /// <param name="session">Installer session handle, just used for logging.</param>
        /// <param name="assemblyName">Display name of the assembly containing the
        /// custom action method.</param>
        /// <param name="className">Fully-qualified name of the class containing the
        /// custom action method.</param>
        /// <param name="methodName">Name of the custom action method.</param>
        /// <returns>The method, or null if not found.</returns>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        private static MethodInfo GetCustomActionMethod(
            Session session,
            string assemblyName,
            string className,
            string methodName)
        {
            Assembly customActionAssembly;
            Type customActionClass = null;
            Exception caughtEx = null;
            try
            {
                customActionAssembly = AppDomain.CurrentDomain.Load(assemblyName);
                customActionClass = customActionAssembly.GetType(className, true, true);
            }
            catch (IOException ex) { caughtEx = ex; }
            catch (BadImageFormatException ex) { caughtEx = ex; }
            catch (TypeLoadException ex) { caughtEx = ex; }
            catch (ReflectionTypeLoadException ex) { caughtEx = ex; }
            catch (SecurityException ex) { caughtEx = ex; }
            if (caughtEx != null)
            {
                session.Log("Error: could not load custom action class " + className + " from assembly: " + assemblyName);
                session.Log(caughtEx.ToString());
                return null;
            }

            MethodInfo[] methods = customActionClass.GetMethods(
                BindingFlags.Public | BindingFlags.Static);
            foreach (MethodInfo method in methods)
            {
                if (method.Name == methodName &&
                    CustomActionProxy.MethodHasCustomActionSignature(method))
                {
                    return method;
                }
            }
            session.Log("Error: custom action method \"" + methodName +
                "\" is missing or has the wrong signature.");
            return null;
        }

        /// <summary>
        /// Checks if a method has the right return and paramater types
        /// for a custom action, and that it is marked by a CustomActionAttribute.
        /// </summary>
        /// <param name="method">Method to be checked.</param>
        /// <returns>True if the method is a valid custom action, else false.</returns>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        private static bool MethodHasCustomActionSignature(MethodInfo method)
        {
            if (method.ReturnType == typeof(ActionResult) &&
                method.GetParameters().Length == 1 &&
                method.GetParameters()[0].ParameterType == typeof(Session))
            {
                object[] methodAttribs = method.GetCustomAttributes(false);
                foreach (object attrib in methodAttribs)
                {
                    if (attrib is CustomActionAttribute)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.