python写dll接口_如何从python中的dll加载具有自定义接口的COM对象?

本文档描述了尝试从Python加载DLL并使用DirectShow接口的挑战,特别是当COM对象需要自定义接口时。作者展示了如何使用comtypes和ctypes库,但遇到了因Pythoncom对DirectShow接口不识别而导致的错误。示例代码中尝试通过IClassFactory创建COM对象,但QueryInterface调用失败,因为没有注册支持特定IID的接口对象。
摘要由CSDN通过智能技术生成

大家好。

任务是使用从dll加载的COM对象(禁止使用regsvr32)。该对象还公开了我将来需要的DirectShow接口。在

然后我试图使用this link中的示例获取一个模块,但遇到了一个问题:pythoncom对DirectShow接口(例如IBaseFilter)一无所知。而且from this post我觉得pythoncom不支持定制的COM接口,但那是在2008年,也许事情已经改变了?在

代码是# -*- coding: utf-8 -*-

import ctypes, inspect, os, pythoncom, sys

from comtypes import client

from ctypes import OleDLL, c_long, byref

from uuid import UUID

#dshow is a module with DirectShow constants, etc

cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"path_to_dshow_module")))

if cmd_subfolder not in sys.path:

sys.path.insert(0, cmd_subfolder)

import dshow

#that way comtypes gets to know about DirectShow interfaces

qedit = client.GetModule("qedit.dll")

dll_path = os.path.join(cmd_subfolder, "../my_path/my_dshow_filter.ax") #specifying path to dll

iid_interface = dshow.CLSID_IUnknown

iid_ibasefilter = dshow.CLSID_IBaseFilter

clsid_class = UUID(dshow.CLSID_my_filter).bytes_le

iclassfactory = UUID(str(pythoncom.IID_IClassFactory)).bytes_le

com_classfactory = c_long(0)

my_dll = ctypes.oledll.LoadLibrary(dll_path)

#getting com_classfactory pointer to an adress of IClassFactory within loaded dll

hr = my_dll.DllGetClassObject(clsid_class, iclassfactory, byref(com_classfactory))

#creating class factory from adress using pythoncom

MyFactory = pythoncom.ObjectFromAddress(com_classfactory.value, pythoncom.IID_IClassFactory)

#creating COM object using IClassFactory::CreateInstance, using IUnknown as a default interface

dmx_interface = MyFactory.CreateInstance(None, iid_interface)

# I could've tried to use IBaseFilter directly,

# but pythoncom knows nothing about DirectShow interfaces!

# dmx = dmx_interface.QueryInterface(str(qedit.IBaseFilter._iid_)) #that yields an error

dmx = dmx_interface.QueryInterface(iid_ibasefilter) #that yields the same error

我得到的错误是TypeError: There is no interface object registered that supports this IID,这是可以理解的。在

所以,通信类型知道这些接口!但不幸的是,我找不到使用comtypes或甚至ctypes从dll加载COM对象的方法。在

我已经处理这个问题好几天了,如果你能给我一些建议,我将不胜感激。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值