【问题标题】:COM - use IEnumerable in ATL C++ projectCOM - 在 ATL C++ 项目中使用 IEnumerable
【发布时间】:2017-04-10 17:51:53
【问题描述】:

我在我的 C++ COM 服务器中使用了一个 C# COM DLL,它实现了 IEnumerable 以迭代集合。

  1. 如何在本机代码中指定要从 C# Dll 的实例对象访问 IEnumerable -> GetEnumerator() 方法?我是否必须导入一些 *.tlb 才能在我的 C++ 项目中看到 IEnumerable 接口?我看到的ienumerable接口是由mscorelib.dll定义的

  2. 我能否进一步向我的客户公开一个 IEnumerable 接口(在我的 C+ 项目的 IDL 中定义)。举个例子会很有帮助

【问题讨论】:

    标签: com


    【解决方案1】:

    它由类型库导出器自动翻译,System.Collections.IEnumerator 是 [ComVisible] 并被翻译为 IEnumVARIANT。例如:

    using System;
    using System.Collections;
    using System.Runtime.InteropServices;
    
    [ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IExample {
        IEnumerator GetEnumerator();
    
    }
    [ComVisible(true), ClassInterface(ClassInterfaceType.None)]
    public class Example : IExample {
        public IEnumerator GetEnumerator() {
            yield return 42;
        }
    }
    

    被翻译成这个类型库片段:

    // TLib :     // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
    importlib("mscorlib.tlb");
    // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("stdole2.tlb");
    
    // Forward declare all types defined in this typelib
    interface IExample;
    
    [
      odl,
      uuid(9B046FDE-9234-3DE7-B055-DADE8F7B4A99),
      version(1.0),
      dual,
      oleautomation,
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "IExample")    
    
    ]
    interface IExample : IDispatch {
        [id(0xfffffffc)]
        HRESULT GetEnumerator([out, retval] IEnumVARIANT** pRetVal);
    };
    

    注意 mscorlib.tlb 的 importlib 指令,该指令位于 c:\windows\microsoft.net\framework\v2.0.50727 中,编译器无需帮助即可找到。

    【讨论】:

    • 你说得对,我想了一段时间,我可以说我的 COM 对象也实现了 IEnumerable(所以他们将实现功能接口和 IEnumerable)
    猜你喜欢
    • 1970-01-01
    • 2017-06-24
    • 2013-06-21
    • 2016-01-17
    • 2014-08-05
    • 2011-09-26
    • 2011-06-10
    • 2017-05-04
    • 2012-02-04
    相关资源
    最近更新 更多