如何在python中创建IPortableDeviceManager的实例?

时间:2014-01-14 19:40:14

标签: python com

我正在尝试使用通过COM公开的Windows Portable Device API从便携式设备(iphone,Android)导入照片。在添加了PortableDeviceApiLib.dll的引用后,我看到了一些使用c#的代码,如下所示:

PortableDeviceApiLib.PortableDeviceManagerClass devMgr = new PortableDeviceApiLib.PortableDeviceManagerClass();

在python中做同样的事情是什么?

1 个答案:

答案 0 :(得分:1)

您应该能够执行以下操作:

from comtypes.client import CreateObject
CreateObject("{0af10cec-2ecd-4b92-9581-34f6ae0637f3}")

但是,您应该将它(QueryInterface)强制转换为有用的东西:

import os
from comtypes.client import GetModule
GetModule(os.getenv("WINDIR") + "\\system32\\PortableDeviceApi.dll")
from comtypes.gen.PortableDeviceApiLib import *
CreateObject(PortableManager)

CreateObject将检查第一个参数,看它是否是具有_reg_clsid_属性,comtypes.GUID实例,CLSID字符串的对象(即以{开头)最后尝试查找CLSID,假设它是一个ProgID。

它还将检查_com_interfaces_属性的第一个参数,该属性应该是接口列表,如果{QueryInterface,则将第一个参数作为interface的默认接口。没有提供1}}参数。