Windows 7 64Bit上的.NET COM Interop令我头疼

Twi*_*bit 6 .net com interop windows-7-x64

到目前为止.NET COM互操作总是很好用.自从我升级到Windows 7后,我不再使用我的.NET COM对象了.

我的COM对象很简单:


namespace Crap
{
    [ComVisible(true)]
    [Guid("2134685b-6e22-49ef-a046-74e187ed0d21")]
    [ClassInterface(ClassInterfaceType.None)]
    public class MyClass : IMyClass
    {

        public MyClass()
        {}

        public void Test()
        {
            MessageBox.Show("Finally got in here.");
        }

    }
}



namespace Crap
{
    [Guid("1234685b-6e22-49ef-a046-74e187ed0d21")]
    public interface IMyClass
    {

    }
}


程序集也标记为ComVisible.

我使用注册程序集

regasm /codebase /tlb "path"
Run Code Online (Sandbox Code Playgroud)

注册成功(管理员模式).我试过了regasm 32和64bit.两次我都收到错误

"ActiveX组件无法使用此vbscript创建对象Crap.MyClass":


dim objReg
Set objReg = CreateObject("Crap.MyClass")
MsgBox typename(objReg)

fuslogvw也没有给我任何提示.COM对象在我的Vista 32 Bit机器上完美运行.

我不明白为什么我无法谷歌解决这个问题的解决方案..我真的是唯一遇到过这个问题的人吗?

看OleView我看到我的对象已成功注册.我也能够创建其他COM对象..它只适用于我自己的对象.

谢谢你,凯文

小智 2

我不是 C# 人员,但这里是我从 VB.net 转换的示例。请注意,我必须确保在项目级别有一个命名空间,然后在 VB 项目中拥有此类。我知道这在 C# 项目中是不同的。

[ComClass(MyClass.ClassId, MyClass.InterfaceId, MyClass.EventsId)] 
public class MyClass {

    // These  GUIDs provide the COM identity for this class 
    // and its COM interfaces. If you change them, existing 
    // clients will no longer be able to access the class.
    public const string ClassId = "f58411e1-1689-4bf3-a0e1-b49f479e28ba";
    public const string InterfaceId = "f4a575c6-62d2-44eb-af0f-f5b2bb65ad51";
    public const string EventsId = "ad56e4f9-3512-4233-aae4-7d1c2457c08f";

    // A creatable COM class must have a Public Sub New() 
    // with no parameters, otherwise, the class will not be 
    // registered in the COM registry and cannot be created 
    // via CreateObject.
    public SalePayStatus() : base()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我担心 COM,我总是首先检查注册表以确保已创建适当的条目。我发现版本控制和 MSI 安装会导致问题,特别是卸载(不清理注册表)或重新安装以及使用 .net COM 对象覆盖现有 COM 条目的 MSI 会导致各种麻烦。

我通常发现您必须小心对待 x64 与 x32 构建 .net DLL。例如,您可能必须显式引用 VBS 引擎的 C:\Windows\SysWow64\ 或 C:\Windows\System32\ 版本。

最后,如果您在具有 x32 COM .net 组件的 x64 服务器上的 ASP 网站中使用 VBS,则您需要确保 IIS 7 应用程序池高级选项“Is 32 bit application”(是否 32 位应用程序)正确设置为 True/False。