如何通过 COM 方法返回 .NET 对象数组

发布于 2024-08-11 00:17:49 字数 1529 浏览 11 评论 0原文

我有一个 .NET 程序集。它恰好是用 C++/CLI 编写的。我通过 COM 公开了一些对象。一切都工作正常,但我一生都无法弄清楚如何从方法返回我自己的对象的数组。每次这样做时,我都会在运行时收到类型不匹配错误。我可以很好地返回一个整数或字符串数​​组。

这是我的主类

[Guid("7E7E69DD-blahblah")]
[ClassInterface(ClassInterfaceType::None)]
[ComVisible(true)]
public ref class Foo sealed : IFoo
{
public:
    virtual array<IBar^>^ GetStuff();
}

[Guid("21EC1AAA-blahblah")]
[InterfaceType(ComInterfaceType::InterfaceIsIDispatch)]
[ComVisible(true)]  
public interface class IFoo
{
public:
    virtual array<IBar^>^ GetStuff()
    {
        // For simplicity, return an empty array for now.
        return gcnew array<IBar^>(0);
    }
};

这是我要返回的类

[Guid("43A37BD4-blahblah")]
[InterfaceType(ComInterfaceType::InterfaceIsIDispatch)]
[ComVisible(true)]  
public interface class IBar
{
    // Completely empty class, just for testing.  
    //In real life, I would like to return two strings and an int.
};

[Guid("634708AF-blahblah")]
[ClassInterface(ClassInterfaceType::None)]
[ComVisible(true)]
[Serializable]
public ref class Bar : IBar
{
};

这是调用它的(本机)C++ 代码:

MyNamespace::IFooPtr session(__uuidof(MyNamespace::Foo));
// For simplicity, don't even check the return.
session->GetStuff();

调用 GetStuff() 会出现 _com_error 0x80020005 (DISP_E_TYPEMISMATCH)。我可以告诉我的方法被正确调用,只是当 .NET/COM 去编组返回时,它会窒息。正如我所说,它适用于整数或字符串数​​组。我必须对我的类做什么才能让它以数组形式返回?

碰巧的是,我的类将只包含几个字符串和一个 int (没有方法),如果这样更容易的话。显然我已经尝试返回一个非空数组和实际上包含一些数据的类,这只是说明问题的最简单的情况。

I have a .NET assembly. It happens to be written in C++/CLI. I am exposing a few objects via COM. Everything is working fine, but I cannot for the life of me figure out how to return an array of my own objects from a method. Every time I do, I get a type mismatch error at runtime. I can return an array of ints or strings just fine.

Here is my main class

[Guid("7E7E69DD-blahblah")]
[ClassInterface(ClassInterfaceType::None)]
[ComVisible(true)]
public ref class Foo sealed : IFoo
{
public:
    virtual array<IBar^>^ GetStuff();
}

[Guid("21EC1AAA-blahblah")]
[InterfaceType(ComInterfaceType::InterfaceIsIDispatch)]
[ComVisible(true)]  
public interface class IFoo
{
public:
    virtual array<IBar^>^ GetStuff()
    {
        // For simplicity, return an empty array for now.
        return gcnew array<IBar^>(0);
    }
};

Here is the class I am returning

[Guid("43A37BD4-blahblah")]
[InterfaceType(ComInterfaceType::InterfaceIsIDispatch)]
[ComVisible(true)]  
public interface class IBar
{
    // Completely empty class, just for testing.  
    //In real life, I would like to return two strings and an int.
};

[Guid("634708AF-blahblah")]
[ClassInterface(ClassInterfaceType::None)]
[ComVisible(true)]
[Serializable]
public ref class Bar : IBar
{
};

This is my (native) C++ code that calls it:

MyNamespace::IFooPtr session(__uuidof(MyNamespace::Foo));
// For simplicity, don't even check the return.
session->GetStuff();

Calling GetStuff() gets me a _com_error 0x80020005 (DISP_E_TYPEMISMATCH). I can tell my method is being called correctly, it's just that when .NET/COM goes to marshall the return, it chokes. As I said, it works fine with arrays of ints or strings. What do I have to do to my class to allow it to be returned in an array?

As it happens, my class will only contain a couple of strings and an int (no methods), if that makes it any easier. Obviously I've tried returning a non-empty array and classes that actually contain some data, this is just the simplest case that illustrates the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红ご颜醉 2024-08-18 00:17:49

您需要实现 IDispatchEnumerator 方法

public ref class FooCollection{
[DispId(-4)]
public IEnumerator^ GetEnumerator()
{
//...
}
}

You need to implement IDispatch and the Enumerator method

public ref class FooCollection{
[DispId(-4)]
public IEnumerator^ GetEnumerator()
{
//...
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文