DFr*_*end 7 .net c# 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个程序集很好,一切都很好.我运行以下脚本将其注册为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
Run Code Online (Sandbox Code Playgroud)
然后使用以下脚本进行测试;
dim tc
set tc = CreateObject("TestCOM.Class1")
tc.showMessage()
Run Code Online (Sandbox Code Playgroud)
我使用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!!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在修改之前,我使用"regasm/unregister"取消注册了库,并且报告了所有未注册的类型.
当我注册库时,现在有了更改,原始测试脚本可以正常工作.如果我扩展测试脚本来调用新的HelloWorld函数;
在32位脚本中,它完美地运行.在64位脚本中,它抱怨TestCOM.Class1对象不存在这样的函数
我已经尝试了每一种方式,但我无法确定为什么新功能可用于32位调用者,而不是64位调用.
我究竟做错了什么 ?对于我不知道的64位内容,或者需要更改的注册表设置,是否存在某个缓存?
要清楚; 1.构建程序集2.使用regasm注册,一次为32,一次为64 3.使用脚本进行测试 - 一切正常4.取消注册库5.进行修改,重建6.按步骤2注册7.测试工作在32位,但不是64. Wtf?
显然,您正在遭受 DLL Hell 的困扰,它总是与 COM 在一起,它正在加载旧版本的 DLL。您的 GAC 可能已被早期实验污染,它总是会首先找到 GACed 版本。你通过指定 [Guid] 使情况变得更糟,使你的新类看起来与旧类相同,即使它并不相同。防止 COM 告诉您它找不到该类的新版本。
查看 DLL 来源的最可靠(尽管有噪音)的方法是使用 SysInterals 的 ProcMon 实用程序。您将看到它读取注册表项并加载 DLL。你可以看到它来自哪个目录。确保它不是 GAC,如果是这种情况,请使用 gacutil /u 将其删除,并通过检查文件上的时间戳确保已重建它。