Example #1
0
        internal 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 }))
                {
                    string message = String.Format(
                        "To debug your custom action, attach to process ID {0} (0x{0:x}) and click OK; otherwise, click Cancel to fail the custom action.",
                        System.Diagnostics.Process.GetCurrentProcess().Id
                        );

                    MessageResult button = NativeMethods.MessageBox(
                        IntPtr.Zero,
                        message,
                        "Custom Action Breakpoint",
                        (int)MessageButtons.OKCancel | (int)MessageIcon.Asterisk | (int)(MessageBoxStyles.TopMost | MessageBoxStyles.ServiceNotification)
                        );

                    if (MessageResult.Cancel == button)
                    {
                        return (int)ActionResult.UserExit;
                    }
                }

                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;
            }
        }
        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 =
                    AppDomain.CurrentDomain.BaseDirectory;

                object[] args = new object[] { session };
                if (DebugBreakEnabled(new string[] { entryPoint, methodName }))
                {
                    string message = String.Format(
                        "To debug your custom action, attach to process ID {0} (0x{0:x}) and click OK; otherwise, click Cancel to fail the custom action.",
                        System.Diagnostics.Process.GetCurrentProcess().Id
                        );

                    MessageResult button = NativeMethods.MessageBox(
                        IntPtr.Zero,
                        message,
                        "Custom Action Breakpoint",
                        (int)MessageButtons.OKCancel | (int)MessageIcon.Asterisk | (int)(MessageBoxStyles.TopMost | MessageBoxStyles.ServiceNotification)
                        );

                    if (MessageResult.Cancel == button)
                    {
                        return((int)ActionResult.UserExit);
                    }
                }

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