使用COM对象的Python ctypes和DLL [英] Python ctypes and DLL that uses a COM object

查看:102
本文介绍了使用COM对象的Python ctypes和DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows下,我试图使用使用 ctypes从Python 2.7用C ++编程的第三方DLL( SomeLib.dll
对于其某些功能,该库使用另一个COM DLL( SomeCOMlib.dll ),它本身使用其他DLL( LibA.dll )。

Under Windows, I'm trying to use a 3rd party DLL (SomeLib.dll) programmed in C++ from Python 2.7 using ctypes. For some of its features, this library uses another COM DLL (SomeCOMlib.dll), which itself uses other DLL (LibA.dll).

请注意,这与直接从Python使用COM DLL无关,而是与使用

为了简化与Python的集成,我将要使用的调用分组到了新DLL中自己的函数中。 ( MyLib.dll ),也用C ++编程,只是为了使从 ctypes 进行的调用更加方便(使用外部 C ,并针对特定情况定制功能)。
本质上,我的库公开了2个函数: doSomethingSimple() doSomethingWithCOMobj()(全部返回 void ,无参数)。

To make the integration with Python easier, I've grouped the calls I want to use into my own functions in a new DLL (MyLib.dll), also programmed in C++, simply to make the calls from ctypes more convenient (using extern "C" and tailoring the functions for specific scenarios). Essentially, my library exposes 2 functions: doSomethingSimple(), doSomethingWithCOMobj() (all returning void, no parameters).

有效依赖项层次结构如下:

The "effective" dependency hierarchy is as follows:

MyLib.dll
  SomeLib.dll
    SomeCOMlib.dll
      LibA.dll

我能够编写一个使用 MyLib的简单C ++控制台应用程序(Visual C ++) .dll 并连续进行两次连续调用。

I'm able to write a simple C++ console application (Visual C++) that uses MyLib.dll and makes those 2 consecutive calls without problem.

使用Python / ctypes,第一个调用可以正常工作,但可以使用的COM引发 WindowsError:[错误-529697949] Windows错误0xE06D7363
从其余的库行为来看,我可以看到问题恰好发生在进行COM调用的地方。

Using Python/ctypes, the first call works fine, but the call that makes use of COM throws a WindowsError: [Error -529697949] Windows Error 0xE06D7363. From the rest of the library behaviour, I can see that the problem comes exactly where that COM call is made.

(简单的测试C ++应用程序也失败了如果缺少 LibA.dll ,则在同一位置或多或少,但是我不确定是否相关。)

(The simple test C++ application also fails more or less at the same place if LibA.dll is missing, but I'm not sure if it's related.)

我已经使用 Dependency Walker 研究了依赖关系层次结构。尽管显然需要
SomeCOMlib.dll 作为 SomeLib.dll 的依赖项,和 LibA.dll 未被列为 SomeCOMlib.dll 的依赖项,尽管在运行时也明确要求

I've looked at the dependency hierarchy using Dependency Walker. SomeCOMlib.dll isn't listed as a dependency of SomeLib.dll, although it's clearly required, and LibA.dll isn't listed as a dependency of SomeCOMlib.dll, although it's also clearly required at run time.

我从命令行运行这些DLL所在目录中的所有内容(并且C ++示例可执行文件运行良好)。
我尝试强制PATH包含该目录,并且我还尝试将DLL复制到我猜可能会被拾取的各个位置( C:\Windows \System32 C:\Python27\DLLs ),但未成功。
SomeCOMlib.dll 也已在 regasm.exe 中注册。

I'm running everything from the command line, from within the directory where these DLLs are (and the C++ sample executable works fine). I've tried to force the PATH to include that directory, and I've also tried to copy the DLLs to various places where I guessed they might be picked up (C:\Windows\System32 and C:\Python27\DLLs), without success. SomeCOMlib.dll was also registered with regasm.exe.

在使用COM自身使用COM时,是从普通C ++应用程序使用此DLL还是从Python的 ctypes 中导致此差异的原因是什么?机制(以及随后可能在那里加载其他DLL)?

What could cause this difference between using this DLL from a plain C++ application and from Python's ctypes when it comes to its own usage of the COM mechanism (and possibly the subsequent loading of other DLLs there)?

以下哪个步骤至少会提供比<$ c $更多的信息c>来自Python的Windows错误0xE06D7363 ,以便能够进一步调查问题?

Which steps would give at least a bit more information than Windows Error 0xE06D7363 from Python, to be able to investigate the problem further?

Python代码如下所示:

The Python code looks like this:

import ctypes
myDll = ctypes.WinDLL("MyLib.dll")
myDll.doSomethingSimple()
myDll.doSomethingWithCOMobj() # This statement throws the Windows Error

(可以正常工作的测试C ++独立应用程序,链接到 MyLib.dll ,可以在 main 中进行完全相同的调用)

(The test C++ standalone application that works, linked to MyLib.dll makes exactly the same calls within main.)

推荐答案

您可以使用诸如processexplorer之类的东西来查找是否所有必需的库都已加载到正在运行的进程中。

You could use something like processexplorer to find out if all the required libraries are loaded into the running process.


  1. 您是否在Python代码中的某处初始化了COM运行时?在您的C ++代码中?
    C ++代码和Python / ctypes之间的区别是,在C ++代码中,加载dll时会调用dll的DllMain()函数,由于完全动态加载dll,Python / ctypes不会这样做

这篇关于使用COM对象的Python ctypes和DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆