English 中文(简体)
How to return an array of .NET objects via a COM method
原标题:

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.

最佳回答

You need to implement IDispatch and the Enumerator method

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

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签