Implement com interface


A

alex

Hi,
I'm scratching my head trying to implement the AutoTextEntries interface (below)
The roadblock is implementing the indexer. Certainly, C# doesn't like the following piece throwing the compiler error
ref and out are not valid in this context (CS0631). Any suggestions how to overcome it?

public Word.AutoTextEntry this[ref object Index]
{
get
{
throw new NotImplementedException();
}
}

This is the interface:

using System;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Word
{
[Guid("00020937-0000-0000-C000-000000000046"), TypeLibType(4288)]
[ComImport]
public interface AutoTextEntries : IEnumerable
{
[DispId(1000)]
Application Application
{
[DispId(1000)]
[MethodImpl(MethodImplOptions.InternalCall)]
[return: MarshalAs(UnmanagedType.Interface)]
get;
}
[DispId(1001)]
int Creator
{
[DispId(1001)]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
[DispId(1002)]
object Parent
{
[DispId(1002)]
[MethodImpl(MethodImplOptions.InternalCall)]
[return: MarshalAs(UnmanagedType.IDispatch)]
get;
}
[DispId(1)]
int Count
{
[DispId(1)]
[MethodImpl(MethodImplOptions.InternalCall)]
get;
}
[DispId(0)]
AutoTextEntry this[[MarshalAs(UnmanagedType.Struct)] [In] ref object Index]
{
[DispId(0)]
[MethodImpl(MethodImplOptions.InternalCall)]
[return: MarshalAs(UnmanagedType.Interface)]
get;
}
[DispId(-4), TypeLibFunc(1024)]
[MethodImpl(MethodImplOptions.InternalCall)]
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalType = "System.Runtime.InteropServices.CustomMarshalers.EnumeratorToEnumVariantMarshaler")]
IEnumerator GetEnumerator();
[DispId(101)]
[MethodImpl(MethodImplOptions.InternalCall)]
[return: MarshalAs(UnmanagedType.Interface)]
AutoTextEntry Add([MarshalAs(UnmanagedType.BStr)] [In] string Name, [MarshalAs(UnmanagedType.Interface)] [In] Range Range);
[DispId(102)]
[MethodImpl(MethodImplOptions.InternalCall)]
[return: MarshalAs(UnmanagedType.Interface)]
AutoTextEntry AppendToSpike([MarshalAs(UnmanagedType.Interface)] [In] Range Range);
}
}
 
Ad

Advertisements

A

alex

What is the COM interface declaration? I.e. what's the IDL? Alternatively,
do you have a C++ declaration of the implementation of the interface?

Off the top of my head, I'm not sure I see any reason to think that a C#
indexer is going to automatically map to a COM indexer. It might be more
fruitful to explicitly name the individual C# methods that implement the
necessary methods in the COM interface.

Admittedly, it's been a couple of year since I did any serious .NET/COM
interop work, so I might be overlooking some useful feature. But as you've
noted, C# doesn't support by-ref arguments in the indexer for a type. If
your COM interface requires by-ref for those arguments, that's just not
going to work.

Pete

Well, looks like the auto-implement interface feature misbehaved after all.
And ILSpy lied to me too, producing an invalid C# code for the interface.
Manually changing the indexer signature like this allowed the compilation.

public Word.AutoTextEntry get_Item(ref object idx)
{
throw new NotImplementedException();
}

Alex.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top