Example #1
0
        /// <summary>
        /// Analyze a COMAddin for the TweakAttribute and try to set given arguments(registry) if exists
        /// </summary>
        /// <param name="factory">current used factory or null for default</param>
        /// <param name="addinInstance">COMAddin instance</param>
        /// <param name="addinType">Type info from COMAddin instance</param>
        /// <param name="registryEndPoint">specific office registry key endpoint</param>
        /// <param name="useSystemRegistryKey">Try read in HKEY_LOCAL_Machine otherwise HKEY_CURRENT_USER</param>
        public static void ApplyTweaks(Core factory, object addinInstance, Type addinType, string registryEndPoint, IsLoadedFromSystemKeyDelegate useSystemRegistryKey)
        {
            try
            {
                if (null == addinInstance)
                {
                    return;
                }
                if (null == factory)
                {
                    factory = Core.Default;
                }

                TweakAttribute tweakAttribute = AttributeReflector.GetTweakAttribute(addinType);
                if (null == tweakAttribute || false == tweakAttribute.Enabled)
                {
                    return;
                }

                ProgIdAttribute progIDAttribute = AttributeReflector.GetProgIDAttribute(addinType, false);
                if (null == progIDAttribute)
                {
                    return;
                }

                bool?systemKey = useSystemRegistryKey();
                if (null == systemKey)
                {
                    return;
                }

                RegistryKey hiveKey = systemKey == true ? Registry.LocalMachine : Registry.CurrentUser;

                RegistryKey key = hiveKey.OpenSubKey("Software\\Microsoft\\Office\\" + registryEndPoint + "\\Addins\\" + progIDAttribute.Value);
                if (null != key)
                {
                    TweakConsoleMode(factory, addinInstance, addinType, key);
                    TweakSharedOutput(factory, addinInstance, addinType, key);
                    TweakAddHocLoading(factory, addinInstance, addinType, key);
                    TweakDeepLoading(factory, addinInstance, addinType, key);
                    TweakDebugOutput(factory, addinInstance, addinType, key);
                    TweakExceptionHandling(factory, addinInstance, addinType, key);
                    TweakExceptionMessage(factory, addinInstance, addinType, key);
                    TweakThreadCulture(factory, addinInstance, addinType, key);
                    TweakMessageFilter(factory, addinInstance, addinType, key);
                    TweakSafeMode(factory, addinInstance, addinType, key);
                    TweakEventOutput(factory, addinInstance, addinType, key);
                    Dictionary <string, string> customTweaks = ApplyCustomTweaks(factory, addinInstance, addinType, key);
                    AddCustomAppliedTweaks(addinInstance.GetHashCode(), customTweaks);
                    key.Close();
                }
                hiveKey.Close();
            }
            catch (Exception exception)
            {
                factory.Console.WriteException(exception);
            }
        }
Example #2
0
        /// <summary>
        /// Checks for a static method, signed with the ErrorHandlerAttribute and call them if its available
        /// </summary>
        /// <param name="type">type information for the class with static method </param>
        /// <param name="methodKind">origin method where the error comes from</param>
        /// <param name="exception">occured exception</param>
        /// <returns>true if error is handled by derived method and we can proceed</returns>
        public static bool RaiseStaticErrorHandlerMethod(Type type, RegisterErrorMethodKind methodKind, System.Exception exception)
        {
            MethodInfo errorMethod = AttributeReflector.GetRegisterErrorMethod(type);

            if (null != errorMethod)
            {
                try
                {
                    object          result         = null;
                    ParameterInfo[] arguments      = errorMethod.GetParameters();
                    int             argumentsCount = arguments.Length;
                    switch (argumentsCount)
                    {
                    case 0:
                        result = errorMethod.Invoke(null, new object[0]);
                        break;

                    case 1:
                        if (arguments[0].ParameterType.GUID == typeof(RegisterErrorMethodKind).GUID)
                        {
                            result = errorMethod.Invoke(null, new object[] { methodKind });
                        }
                        else
                        {
                            result = errorMethod.Invoke(null, new object[] { exception });
                        }
                        break;

                    case 2:
                        result = errorMethod.Invoke(null, new object[] { methodKind, exception });
                        break;

                    case 3:
                        result = errorMethod.Invoke(null, new object[] { type, methodKind, exception });
                        break;

                    default:
                        break;
                    }

                    if (result is bool)
                    {
                        return((bool)result);
                    }
                }
                catch (Exception throwedException)
                {
                    Console.WriteLine("Unable to call addin register error method. {0}", throwedException.Message);
                }
            }
            return(false);
        }
Example #3
0
        /// <summary>
        /// Do register export process
        /// </summary>
        /// <param name="type">addin type</param>
        /// <param name="addinOfficeRegistryKey">office application registry path</param>
        /// <param name="scope">the current installation scope</param>
        /// <param name="keyState">the office registry key need to create</param>
        public static RegExport Proceed(Type type, string[] addinOfficeRegistryKey, InstallScope scope, OfficeRegisterKeyState keyState)
        {
            try
            {
                object     result       = null;
                MethodInfo exportMethod = null;
                RegExportFunctionAttribute registerAttribute = null;
                bool registerMethodPresent = AttributeReflector.GetRegExportAttribute(type, ref exportMethod, ref registerAttribute);
                if (registerMethodPresent)
                {
                    ParameterInfo[] arguments     = exportMethod.GetParameters();
                    int             argumentCount = arguments.Length;
                    switch (argumentCount)
                    {
                    case 0:
                        result = exportMethod.Invoke(null, new object[0]);
                        break;

                    case 1:
                        result = exportMethod.Invoke(null, new object[] { scope });
                        break;

                    case 2:
                        result = exportMethod.Invoke(null, new object[] { scope, keyState });
                        break;

                    case 3:
                        exportMethod.Invoke(null, new object[] { type, scope, keyState });
                        break;

                    default:
                        break;
                    }

                    return(result as RegExport);
                }
                else
                {
                    return(null);
                }
            }
            catch (System.Exception exception)
            {
                NetOffice.DebugConsole.Default.WriteException(exception);
                RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.Export, exception);
                return(null);
            }
        }
        /// <summary>
        /// Do register process
        /// </summary>
        /// <param name="type">addin type</param>
        /// <param name="addinOfficeRegistryKey">office application registry path</param>
        /// <param name="scope">the current installation scope</param>
        /// <param name="keyState">the office registry key need to create</param>
        public static void Proceed(Type type, string[] addinOfficeRegistryKey, InstallScope scope, OfficeRegisterKeyState keyState)
        {
            if (null == type)
            {
                throw new ArgumentNullException("type");
            }
            if (null == addinOfficeRegistryKey)
            {
                throw new ArgumentNullException("addinOfficeRegistryKey");
            }

            int errorBlock = -1;

            try
            {
                GuidAttribute             guid         = AttributeReflector.GetGuidAttribute(type);
                ProgIdAttribute           progId       = AttributeReflector.GetProgIDAttribute(type);
                RegistryLocationAttribute location     = AttributeReflector.GetRegistryLocationAttribute(type);
                COMAddinAttribute         addin        = AttributeReflector.GetCOMAddinAttribute(type, progId.Value);
                CodebaseAttribute         codebase     = AttributeReflector.GetCodebaseAttribute(type);
                LockbackAttribute         lockBack     = AttributeReflector.GetLockbackAttribute(type);
                ProgrammableAttribute     programmable = AttributeReflector.GetProgrammableAttribute(type);
                TimestampAttribute        timestamp    = AttributeReflector.GetTimestampAttribute(type);
                bool isSystemComponent = location.IsMachineComponentTarget(scope);
                bool isSystemAddin     = location.IsMachineAddinTarget(scope);

                MethodInfo registerMethod = null;
                RegisterFunctionAttribute registerAttribute = null;
                bool registerMethodPresent = false;

                errorBlock = 0;

                try
                {
                    registerMethodPresent = AttributeReflector.GetRegisterAttribute(type, ref registerMethod, ref registerAttribute);
                    if (null != registerAttribute && true == registerMethodPresent && (registerAttribute.Value == RegisterMode.CallBefore || registerAttribute.Value == RegisterMode.CallBeforeAndAfter))
                    {
                        if (!CallDerivedRegisterMethod(registerMethod, type, registerAttribute.Value == RegisterMode.Replace ? RegisterCall.Replace : RegisterCall.CallBefore, scope, keyState))
                        {
                            if (!RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type,
                                                                                    RegisterErrorMethodKind.Register,
                                                                                    new RegisterException(errorBlock)))
                            {
                                return;
                            }
                        }

                        if (registerAttribute.Value == RegisterMode.Replace)
                        {
                            return;
                        }
                    }
                }
                catch (Exception)
                {
                    errorBlock = 1;
                    throw;
                }

                if (null != programmable)
                {
                    try
                    {
                        ProgrammableAttribute.CreateKeys(type.GUID, isSystemComponent);
                    }
                    catch (Exception)
                    {
                        errorBlock = 2;
                        throw;
                    }
                }

                if (null != codebase && codebase.Value)
                {
                    try
                    {
                        Assembly thisAssembly    = Assembly.GetAssembly(type);
                        string   assemblyVersion = thisAssembly.GetName().Version.ToString();
                        CodebaseAttribute.CreateValue(type.GUID, isSystemComponent, assemblyVersion, thisAssembly.CodeBase);
                    }
                    catch (Exception)
                    {
                        errorBlock = 3;
                        throw;
                    }
                }

                if (null != lockBack)
                {
                    if (!LockbackAttribute.CreateKey(isSystemComponent))
                    {
                        NetOffice.DebugConsole.Default.WriteLine("Unable to create lockback bypass.");
                    }
                }

                if (keyState == OfficeRegisterKeyState.NeedToCreate)
                {
                    try
                    {
                        foreach (string item in addinOfficeRegistryKey)
                        {
                            RegistryLocationAttribute.CreateApplicationKey(isSystemAddin, item, progId.Value,
                                                                           addin.LoadBehavior, addin.Name, addin.Description, addin.CommandLineSafe, null != timestamp);
                        }
                    }
                    catch (Exception)
                    {
                        errorBlock = 5;
                        throw;
                    }
                }

                if ((null != registerAttribute && true == registerMethodPresent) && (registerAttribute.Value == RegisterMode.CallAfter || registerAttribute.Value == RegisterMode.CallBeforeAndAfter))
                {
                    if (!CallDerivedRegisterMethod(registerMethod, type, RegisterCall.CallAfter, scope, keyState))
                    {
                        RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.Register, new RegisterException(errorBlock));
                    }
                }
            }
            catch (System.Exception exception)
            {
                NetOffice.DebugConsole.Default.WriteLine("RegisterHandler Exception.Block:{0}", errorBlock);
                NetOffice.DebugConsole.Default.WriteException(exception);
                if (!RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.Register, exception))
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// Do unregister process
        /// </summary>
        /// <param name="type">addin type</param>
        /// <param name="addinOfficeRegistryKey">office application registry path</param>
        /// <param name="scope">the current installation scope</param>
        /// <param name="keyState">the office registry key need to delete</param>
        public static void Proceed(Type type, string[] addinOfficeRegistryKey, InstallScope scope, OfficeUnRegisterKeyState keyState)
        {
            try
            {
                MethodInfo registerMethod = null;
                UnRegisterFunctionAttribute registerAttribute = null;
                bool registerMethodPresent = AttributeReflector.GetUnRegisterAttribute(type, ref registerMethod, ref registerAttribute);

                if ((null != registerAttribute &&
                     true == registerMethodPresent) &&
                    (registerAttribute.Value == RegisterMode.CallBefore || registerAttribute.Value == RegisterMode.CallBeforeAndAfter || registerAttribute.Value == RegisterMode.Replace))
                {
                    if (!CallDerivedUnRegisterMethod(registerMethod, type, registerAttribute.Value == RegisterMode.Replace ? RegisterCall.Replace : RegisterCall.CallBefore, scope, keyState))
                    {
                        if (!RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.UnRegister, new UnregisterException()))
                        {
                            return;
                        }
                    }
                    if (registerAttribute.Value == RegisterMode.Replace)
                    {
                        return;
                    }
                }

                ProgIdAttribute           progId       = AttributeReflector.GetProgIDAttribute(type);
                RegistryLocationAttribute location     = AttributeReflector.GetRegistryLocationAttribute(type);
                CodebaseAttribute         codebase     = AttributeReflector.GetCodebaseAttribute(type);
                ProgrammableAttribute     programmable = AttributeReflector.GetProgrammableAttribute(type);
                bool isSystemComponent = location.IsMachineComponentTarget(scope);
                bool isSystemAddin     = location.IsMachineAddinTarget(scope);

                if (null != programmable)
                {
                    ProgrammableAttribute.DeleteKeys(type.GUID, isSystemComponent, false);
                }

                if (null != codebase && codebase.Value == true)
                {
                    Assembly thisAssembly    = Assembly.GetAssembly(type);
                    string   assemblyVersion = thisAssembly.GetName().Version.ToString();
                    CodebaseAttribute.DeleteValue(type.GUID, isSystemComponent, assemblyVersion, false);
                }

                if (keyState == OfficeUnRegisterKeyState.NeedToDelete)
                {
                    foreach (string item in addinOfficeRegistryKey)
                    {
                        RegistryLocationAttribute.TryDeleteApplicationKey(isSystemAddin, item, progId.Value);
                    }
                }

                if ((null != registerAttribute && true == registerMethodPresent) && (registerAttribute.Value == RegisterMode.CallAfter || registerAttribute.Value == RegisterMode.CallBeforeAndAfter))
                {
                    if (!CallDerivedUnRegisterMethod(registerMethod, type, RegisterCall.CallAfter, scope, keyState))
                    {
                        RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.UnRegister, new UnregisterException());
                    }
                }
            }
            catch (System.Exception exception)
            {
                NetOffice.DebugConsole.Default.WriteException(exception);
                if (!RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.UnRegister, exception))
                {
                    throw;
                }
            }
        }
Example #6
0
        /// <summary>
        /// Do register process
        /// </summary>
        /// <param name="type">addin type</param>
        /// <param name="addinOfficeRegistryKey">office application registry path</param>
        /// <param name="scope">the current installation scope</param>
        /// <param name="keyState">the office registry key need to create</param>
        public static void Proceed(Type type, string[] addinOfficeRegistryKey, InstallScope scope, OfficeRegisterKeyState keyState)
        {
            try
            {
                GuidAttribute             guid         = AttributeReflector.GetGuidAttribute(type);
                ProgIdAttribute           progId       = AttributeReflector.GetProgIDAttribute(type);
                RegistryLocationAttribute location     = AttributeReflector.GetRegistryLocationAttribute(type);
                COMAddinAttribute         addin        = AttributeReflector.GetCOMAddinAttribute(type);
                CodebaseAttribute         codebase     = AttributeReflector.GetCodebaseAttribute(type);
                LockbackAttribute         lockBack     = AttributeReflector.GetLockbackAttribute(type);
                ProgrammableAttribute     programmable = AttributeReflector.GetProgrammableAttribute(type);
                bool isSystem = location.IsMachineTarget(scope);

                MethodInfo registerMethod = null;
                RegisterFunctionAttribute registerAttribute = null;
                bool registerMethodPresent = AttributeReflector.GetRegisterAttribute(type, ref registerMethod, ref registerAttribute);
                if (null != registerAttribute && true == registerMethodPresent &&
                    registerAttribute.Value == RegisterMode.CallBefore || registerAttribute.Value == RegisterMode.CallBeforeAndAfter)
                {
                    if (!CallDerivedRegisterMethod(registerMethod, type, registerAttribute.Value == RegisterMode.Replace ? RegisterCall.Replace : RegisterCall.CallBefore, scope, keyState))
                    {
                        if (!RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.Register, new NetOfficeException(ExceptionMessage)))
                        {
                            return;
                        }
                    }
                    if (registerAttribute.Value == RegisterMode.Replace)
                    {
                        return;
                    }
                }

                if (null != programmable)
                {
                    ProgrammableAttribute.CreateKeys(type.GUID, isSystem);
                }

                if (null != codebase && codebase.Value)
                {
                    Assembly thisAssembly    = Assembly.GetAssembly(type);
                    string   assemblyVersion = thisAssembly.GetName().Version.ToString();
                    CodebaseAttribute.CreateValue(type.GUID, location.IsMachineTarget(scope), assemblyVersion, thisAssembly.CodeBase);
                }

                if (null != lockBack)
                {
                    if (!LockbackAttribute.CreateKey(isSystem))
                    {
                        NetOffice.DebugConsole.Default.WriteLine("Unable to create lockback bypass.");
                    }
                }

                if (keyState == OfficeRegisterKeyState.NeedToCreate)
                {
                    foreach (string item in addinOfficeRegistryKey)
                    {
                        RegistryLocationAttribute.CreateApplicationKey(location.IsMachineTarget(scope), item, progId.Value,
                                                                       addin.LoadBehavior, addin.Name, addin.Description, addin.CommandLineSafe);
                    }
                }

                if (null != registerAttribute && true == registerMethodPresent &&
                    registerAttribute.Value == RegisterMode.CallAfter || registerAttribute.Value == RegisterMode.CallBeforeAndAfter)
                {
                    if (!CallDerivedRegisterMethod(registerMethod, type, RegisterCall.CallAfter, scope, keyState))
                    {
                        RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.Register, new NetOfficeException(ExceptionMessage));
                    }
                }
            }
            catch (System.Exception exception)
            {
                NetOffice.DebugConsole.Default.WriteException(exception);
                RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.Register, exception);
            }
        }