Пример #1
0
        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);
            }
        }