x64 COM加载项注册仅适用于x86 Excel

时间:2020-03-02 16:09:15

标签: .net dll com office-addins dllregistration

我正在努力注册要在Excel 64位中使用的C#类库dll,到目前为止,我已经检查了互联网上谈论此问题的每个地方。 我的类库很简单,有一个方法(现在可以测试)用作Excel插件。

1)当我的类库编译到AnyCPU时,我的确在Windows注册表中看到了我的Addin,它可见且可以正常工作,但仅适用于Excel 32位版本,而不适用于Excel 64位版本。 这是注册表生成文件的屏幕截图:

Registry screenshot when my library is compiled to AnyCPU

2)当我的类库被编译为x64(不对代码进行任何更改)时,我在Windows注册表中看不到我的Addin,这就像注册失败或发生了什么。 这是编译为x64时在寄存器中生成的屏幕截图:

Registry screenshot when my library is compiled to x64

所以在这里您可以看到我的Class根本没有像编译到AnyCPU时那样注册。

这是我正在使用的简单类,您可以在其中检查注册代码是否正在使用:

interface IAutomationAddin
{
    string GetTest();
}

[Guid("249618F9-A4A0-4999-9712-280D2A1493FB")]
[ClassInterface(ClassInterfaceType.AutoDual), ComVisible(true)]
public class AutomationAddin : IAutomationAddin
{
    #region Constructors

    public AutomationAddin()
    {

    }

    #endregion Constructors

    #region Initialization (Register/Unregister functions)

    /// <summary>
    /// Specifies the method to call when you register an assembly for use from COM; 
    /// this allows for the execution of user-written code during the registration process.
    /// </summary>
    /// <param name="type"></param>
    [ComRegisterFunction]
    public static void RegisterFunction(Type type)
    {
        string s = @"Wow6432Node\\CLSID\{" + type.GUID.ToString().ToUpper() + @"}\Programmable";

        Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("Wow6432Node\\CLSID\\{" +
                                                                                            type.GUID.ToString().ToUpper() + "}\\InprocServer32");

        if (key != null)
        {
            key.SetValue("", System.Environment.GetFolderPath(System.Environment.SpecialFolder.System) +
                             @"\mscoree.dll");
        }

        Registry.ClassesRoot.CreateSubKey(s);
    }

    ///// <summary>
    ///// Specifies the method to call when you unregister an assembly for use from COM; 
    ///// this allows for the execution of user-written code during the unregistration process.
    ///// </summary>
    ///// <param name="type"></param>
    [ComUnregisterFunction]
    public static void UnregisterFunction(Type type)
    {
        string s = @"Wow6432Node\\CLSID\{" + type.GUID.ToString().ToUpper() + @"}\Programmable";

        Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("Wow6432Node\\CLSID\\{" +
                                                                                            type.GUID.ToString().ToUpper() + "}\\InprocServer32");

        if (key != null)
        {
            key.SetValue("", System.Environment.GetFolderPath(System.Environment.SpecialFolder.System) +
                             @"\mscoree.dll");
        }

        Registry.ClassesRoot.DeleteSubKey(s, false);
    }

    #endregion Initialization (Register/Unregister functions)

    public string GetTest()
    {
        return "x64 Simple Test";
    }
}

我需要提及的是,在我的项目属性中已选中“注册COM互操作”(这很明显,因为它适用于32位)。

有人知道为什么不注册x64吗?或为什么只为32位Excel注册AnyCPU?

0 个答案:

没有答案
相关问题