【问题标题】:passing an array of COM pointers from Python to C++将一组 COM 指针从 Python 传递到 C++
【发布时间】:2016-10-20 04:37:25
【问题描述】:

我已经阅读了很多文档、示例和 StackOverflow 主题,但它仍然不起作用!我正在为我的 C++ COM 对象编写一个 Python 接口。这不是我第一次这样做。过去我已经成功地使用 comtypes 来获取单独的接口指针并将它们传递给我的 COM 类,但这次我需要传递一个指向接口指针数组的指针。

我需要调用的COM接口:

STDMETHOD(ExportGeopackage)([in] IMap* pMap,
                            [in] imageFormatType imageFormat,
                            [in] long imageQuality,
                            [in] long zoomMin,
                            [in] long zoomMax,
                            [in] IFeatureLayer** attributeExportLayers,
                            [in] BSTR title,
                            [in] BSTR description,
                            [in] BSTR saveToPath,
                            [in] ITrackCancel* pTrackCancel);

attributeExportLayers 参数应为指向以空结尾的 IFeatureLayer 指针的 C 数组的指针。 ExportGeopackage() 已经用 C++ 客户端进行了测试。我正在编写第一个 Python 客户端。

在 Python 中:

# append a null pointer to the list of comtypes IFeatureLayer pointers
exportLayers.append(comtypes.cast(0, comtypes.POINTER(esriCarto.IFeatureLayer)))
# create ctypes array and populate
PointerArray = ctypes.c_void_p * len(exportLayers)
pointers = PointerArray()
for i in range(len(exportLayers)):
    pointers[i] = exportLayers[i]

# export is comtypes interface pointer acquired earlier
export.ExportGeopackage(map, format, quality, min, max,
                        ctypes.cast(pointers, ctypes.POINTER(esriCarto.IFeatureLayer)),
                        title, desc, geopackage_path, 0)

比较 exportLayer 和指针变量内容的 Python 转储显示指针值已成功从前者传输到后者。这些指针的 Python 测试是成功的。但是,当我调试到 ExportGeopackage() 时,attributeExportLayers 指向的内存与预期的 IFeatureLayer 指针数组没有相似之处。它看起来像一个单个指针(指向错误的位置),后面跟着一长串空指针。考虑到 Python 指针变量可能已经被垃圾回收,我在调用 ExportGeopackage() 之后添加了对指针的引用。这没什么区别。

我是否以某种方式插入了额外的间接级别,或者没有足够的间接?我很困惑。

TIA 寻求任何帮助(或猜测)。 艾伦

【问题讨论】:

    标签: python windows types comtypes


    【解决方案1】:

    再次回答我自己的问题。除了所需的之外,还引入了额外的间接级别。显然,与 C 中的强制转换不同,ctypes.cast 实际上采用第一个参数的地址。

    变化:

    PointerArray = ctypes.c_void_p * len(exportLayers)
    

    收件人:

    PointerArray = comtypes.POINTER(esriCarto.IFeatureLayer) * len(exportLayers)
    

    并消除在调用 ExportGeopackage() 时使用 ctypes.cast:

    export.ExportGeopackage(map, format, quality, min, max,
                            pointers,
                            title, desc, geopackage_path, 0)
    

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 2018-12-20
      相关资源
      最近更新 更多