COM - 在 ATL C++ 项目中使用 IEnumerable [英] COM - use IEnumerable in ATL C++ project
问题描述
我在我的 C++ COM 服务器中使用了一个 C# COM DLL,它实现了 IEnumerable 以迭代集合.
I use in my C++ COM server a C# COM DLL that implements IEnumerable for iterating over a collection.
如何在本机代码中指定要从 C# Dll 的实例对象访问 IEnumerable -> GetEnumerator() 方法?我是否必须导入一些 *.tlb 才能在我的 C++ 项目中看到 IEnumerable 接口?我看到的ienumerable接口是由mscorelib.dll定义的
How do I specify in my native code that I want to access IEnumerable -> GetEnumerator() method from the instance object of C# Dll ? Do I have to import some *.tlb in order to see IEnumerable interface in my C++ project ? Ienumerable interface I saw is defined by mscorelib.dll
我能否进一步向我的客户公开一个 IEnumerable 接口(在我的 C+ 项目的 IDL 中定义).举个例子会很有帮助
Can I further expose an IEnumerable interface to my clients (defined in IDL in my C+ project). An example would be helpful
推荐答案
它由类型库导出器自动翻译,System.Collections.IEnumerator 是 [ComVisible] 并被翻译成 IEnumVARIANT.例如:
It is automatically translated by the type library exporter, System.Collections.IEnumerator is [ComVisible] and gets translated to IEnumVARIANT. For example:
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;
}
}
被翻译成这个类型库片段:
Gets translated to this type library fragment:
// 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:windowsmicrosoft.netframeworkv2.0.50727 中,编译器无需帮助即可找到.
Note the importlib directive for mscorlib.tlb, present in c:windowsmicrosoft.netframeworkv2.0.50727 and found by the compiler without help.
这篇关于COM - 在 ATL C++ 项目中使用 IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!