简体   繁体   中英

How to expose a C# interface's method arguments as specific COM/ODL types?

I'm defining an interface in C# which will be implemented in C#, but called from an unmanaged C++ module as a COM object.

I know what I want/need the C++ API to look like and how I'd define it via ODL:

//args is an array of BSTR e.g VT_ARRAY|VT_BSTR
HRESULT DoMethod(/*[in]*/BSTR name, /*[in]*/VARIANT args);

I'm not sure how to set this up in C# to cause the TLB definition to match this, in regards the VARIANT . Could it be something as simple as the following?

void DoMethod(string name, string args[])

I've been looking around COM/.NET interop documentation but either I've missed the section on this, or simply don't understand what's being described!

As an aside, how can I see what COM definition is being emitted for a given C# interface? Is the DLL/TLB easily inspected?

If you want a variant on the C++ side (why?) then you need to declare it:

using System.Runtime.InteropServices;

namespace LibraryName {
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IFoo {
        void DoMethod(string name, object args);
    }
}

Register the C# assembly with Regasm.exe /codebase /tlb. The /tlb option generates the type library, you can use it in your C++ code with the #import directive. Which is enough to have a look-see, the LibraryName.tlh file it generates has the declarations. Or you can run Oleview.exe from the Visual Studio Command Prompt and use File > View Typelib to look at it.

Your original instinctive choice is better, string[] shows up as SAFEARRAY* on the C++ side. Less accidents that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM