Exemple #1
0
        public static void RegisterFunction(Type type)
        {
            if (null == type)
            {
                throw new ArgumentNullException("type");
            }
            if (null != type.GetCustomAttribute <DontRegisterAddinAttribute>())
            {
                return;
            }

            RegisterHandleCodebase(type, InstallScope.System);
            RegisterHandleProgrammable(type, InstallScope.System);

            MethodInfo registerMethod = null;
            RegisterFunctionAttribute registerAttribute = null;
            bool registerMethodPresent = AttributeReflector.GetRegisterAttribute(type, ref registerMethod, ref registerAttribute);

            if (null != registerAttribute && true == registerMethodPresent)
            {
                if (!TryCallDerivedRegisterMethod(registerMethod, type, InstallScope.System))
                {
                    if (!RegisterErrorHandler.RaiseStaticErrorHandlerMethod(type,
                                                                            RegisterErrorMethodKind.Register,
                                                                            new RegisterException(0)))
                    {
                        return;
                    }
                }
            }
        }
 /// <summary>
 /// Derived Register Call Helper
 /// </summary>
 /// <param name="type">type for derived class</param>
 /// <param name="registerMethod">the method to call</param>
 /// <param name="registerAttribute">arguments</param>
 private static void CallDerivedRegisterMethod(Type type, MethodInfo registerMethod, RegisterFunctionAttribute registerAttribute)
 {
     if (registerAttribute.Value == RegisterMode.Replace)
     {
         registerMethod.Invoke(null, new object[] { type, RegisterCall.Replace });
     }
     else if (registerAttribute.Value == RegisterMode.CallBeforeAndAfter || registerAttribute.Value == RegisterMode.CallBefore)
     {
         registerMethod.Invoke(null, new object[] { type, RegisterCall.CallBefore });
     }
 }
        public static void RegisterFunction(Type type)
        {
            try
            {
                MethodInfo registerMethod = null;
                RegisterFunctionAttribute registerAttribute = null;
                bool registerMethodPresent = AttributeHelper.GetRegisterAttribute(type, ref registerMethod, ref registerAttribute);
                if (registerMethodPresent)
                {
                    CallDerivedRegisterMethod(type, registerMethod, registerAttribute);
                    if (registerAttribute.Value == RegisterMode.Replace)
                    {
                        return;
                    }
                }

                GuidAttribute             guid     = AttributeHelper.GetGuidAttribute(type);
                ProgIdAttribute           progId   = AttributeHelper.GetProgIDAttribute(type);
                RegistryLocationAttribute location = AttributeHelper.GetRegistryLocationAttribute(type);
                COMAddinAttribute         addin    = AttributeHelper.GetCOMAddinAttribute(type);

                Assembly    thisAssembly = Assembly.GetAssembly(type);
                RegistryKey key          = Registry.ClassesRoot.CreateSubKey("CLSID\\{" + type.GUID.ToString().ToUpper() + "}\\InprocServer32\\" + GetAssemblyVersionString(type.Assembly));
                key.SetValue("CodeBase", thisAssembly.CodeBase);
                key.Close();

                // add bypass key
                // http://support.microsoft.com/kb/948461
                key = Registry.ClassesRoot.CreateSubKey("Interface\\{000C0601-0000-0000-C000-000000000046}");
                string defaultValue = key.GetValue("") as string;
                if (null == defaultValue)
                {
                    key.SetValue("", "Office .NET Framework Lockback Bypass Key");
                }
                key.Close();

                // register addin in Word
                Registry.CurrentUser.CreateSubKey(_addinOfficeRegistryKey + progId.Value);
                RegistryKey regKeyWord = null;

                if (location.Value == RegistrySaveLocation.LocalMachine)
                {
                    regKeyWord = Registry.LocalMachine.OpenSubKey(_addinOfficeRegistryKey + progId.Value, true);
                }
                else
                {
                    regKeyWord = Registry.CurrentUser.OpenSubKey(_addinOfficeRegistryKey + progId.Value, true);
                }

                regKeyWord.SetValue("LoadBehavior", addin.LoadBehavior);
                regKeyWord.SetValue("FriendlyName", addin.Name);
                regKeyWord.SetValue("Description", addin.Description);
                if (-1 != addin.CommandLineSafe)
                {
                    regKeyWord.SetValue("CommandLineSafe", addin.CommandLineSafe);
                }

                regKeyWord.Close();

                if ((registerMethodPresent) && (registerAttribute.Value == RegisterMode.CallBeforeAndAfter || registerAttribute.Value == RegisterMode.CallAfter))
                {
                    registerMethod.Invoke(null, new object[] { type, RegisterCall.CallAfter });
                }
            }
            catch (NetRunTimeSystem.Exception exception)
            {
                NetOffice.DebugConsole.WriteException(exception);
                RaiseStaticErrorHandlerMethod(type, RegisterErrorMethodKind.Register, exception);
            }
        }