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

时间:2010-05-01 10:51:25

标签: .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"

成功注册(管理员模式)。 我试过了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 个答案:

答案 0 :(得分:2)

我不是C#人,但这是我从VB.net转换的示例。注意,我必须确保在项目级别有一个命名空间,然后是VB项目中的这个类。我理解在C#Projects中有所不同。

[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()
    {
    }
}

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

我通常发现你必须小心x64 vs x32 build .net DLLs。例如,您可能必须显式引用VBS引擎的C:\ Windows \ SysWow64 \或C:\ Windows \ System32 \ editions。

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

答案 1 :(得分:1)

谢谢!不知道有2个注册表我要照顾..是时候切换到Win7 64位我想:)

谢谢。

对于遇到同样问题的其他人: wscript(通常执行vbs文件的客户端)以64位模式执行=>将使用RegAsm 64位

其他常见客户端(如Excel)以32位模式执行=>将使用RegAsm 32位。

Visual Studio以32位执行=>注册COM interop仅在32位注册表中注册COM对象。

我现在唯一需要弄清楚的是如何确保VS Setup注册两个版本