64位类型库和32位类型库不同步

时间:2013-03-05 12:16:05

标签: c# .net com regasm

这个问题让我头疼了几天,我找不到理由。我很确定这是我的机器特有的环境问题,但它仍然会导致测试问题。

我正在使用Visual Studio 2010 Professional在C#中创建一个DLL。第一版是下面的;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TestCOM
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.ProgId("TestCOM.Class1")]
    [System.Runtime.InteropServices.Guid("803A1B2F-1CDA-4571-9084-87500388693B")]
    public class Class1
    {
        public void showMessage()
        {
            MessageBox.Show("Hello from TextCom");
        }

    }
}

这个程序集很好,一切都很好。我运行以下脚本将其注册为COM对象(首先是32位,然后是64位);

C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe TestCOM.dll /codebase /nologo /tlb:TestCOM32.tlb
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe TestCOM.dll /codebase /nologo /tlb:TestCOM64.tlb

然后使用以下脚本进行测试;

dim tc 
set tc = CreateObject("TestCOM.Class1")
tc.showMessage()

我使用csript来测试脚本,所以我可以控制它使用的位深度 - 我用32位测试一次,用64位测试一次。到目前为止一切都很好。

现在,当我修改原始程序集以添加函数时,如下所示

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace TestCOM
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.ProgId("TestCOM.Class1")]
    [System.Runtime.InteropServices.Guid("803A1B2F-1CDA-4571-9084-87500388693B")]
    public class Class1
    {
        public void showMessage()
        {
            MessageBox.Show("Hello from TextCom");
        }

        public void HelloWorld()
        {
            MessageBox.Show("Hello World!!");
        }

    }
}

在修改之前,我使用“regasm / unregister”取消注册库,并报告所有类型未成功注册。

当我注册库时,现在有了更改,原始测试脚本可以正常工作。如果我扩展测试脚本以调用新的HelloWorld函数;

在32位脚本中,它完美运行。 在64位脚本中,它抱怨TestCOM.Class1对象

不存在这样的函数

我已经尝试了每一种方式,但我无法确定为什么新功能可用于32位呼叫者,而不是64位呼叫。

我做错了什么?对于我不知道的64位内容,或者需要更改的注册表设置,是否存在某个缓存?

要清楚;  1.构建组件  2.使用regasm注册,一次为32,一次为64  3.使用脚本测试 - 一切正常  4.取消注册库  5.进行修改,重建  6.按步骤2注册  7.测试工作在32位,但不是64. Wtf?

1 个答案:

答案 0 :(得分:2)

很明显,你正在遭受DLL Hell,总是在COM附近,它正在加载旧版本的DLL。您的GAC可能已被早期的实验污染,它将始终首先找到GACed版本。你通过指定[Guid]使情况更糟,使你的新类看起来与旧类相同,即使它不相同。防止COM告诉您它无法找到该类的新版本。

通过使用SysInterals的ProcMon实用程序,查看DLL来自哪里的最可靠但有噪音的方法。您将看到它读取注册表项并加载DLL。你可以看到它来自哪个目录。确保它不是GAC,如果是这种情况,请使用gacutil / u删除它,并确保通过检查文件上的时间戳来重建它。