简体   繁体   English

如何将C#接口的方法参数公开为特定的COM / ODL类型?

[英]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. 我在C#中定义一个接口,该接口将在C#中实现,但从非托管C ++模块作为COM对象调用。

I know what I want/need the C++ API to look like and how I'd define it via ODL: 我知道我想要/需要C ++ API的外观,以及如何通过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 . 关于VARIANT ,我不确定如何在C#中进行设置以使TLB定义与此匹配。 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! 我一直在寻找COM / .NET互操作文档,但是我错过了有关此部分的内容,或者根本不了解所描述的内容!

As an aside, how can I see what COM definition is being emitted for a given C# interface? 顺便说一句,如何查看给定C#接口正在发出的COM定义? Is the DLL/TLB easily inspected? DLL / TLB是否易于检查?

If you want a variant on the C++ side (why?) then you need to declare it: 如果您想要C ++方面的变体(为什么?),则需要声明它:

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. 用Regasm.exe / codebase / tlb注册C#程序集。 The /tlb option generates the type library, you can use it in your C++ code with the #import directive. / tlb选项生成类型库,您可以在C ++代码中使用#import指令使用它。 Which is enough to have a look-see, the LibraryName.tlh file it generates has the declarations. 足以看一看,它生成的LibraryName.tlh文件具有声明。 Or you can run Oleview.exe from the Visual Studio Command Prompt and use File > View Typelib to look at it. 或者,您可以从Visual Studio命令提示符运行Oleview.exe,然后使用“文件”>“查看Typelib”进行查看。

Your original instinctive choice is better, string[] shows up as SAFEARRAY* on the C++ side. 您最初的本能选择更好,string []在C ++方面显示为SAFEARRAY *。 Less accidents that way. 这样可以减少事故发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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