通过Python只能部分访问COM类型库 [英] Only partial access to a COM Type Library through Python

查看:424
本文介绍了通过Python只能部分访问COM类型库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一些脚本来使用一个应用程序附带的COM类型库来控制它外部。

I am attempting to write some scripts to employ a COM type library that came with an application to control it externally.

但是,我被困在我的使用COM的Python库(pythoncom和win32com):

However, I am stuck in my use of the Python libraries for COM (pythoncom and win32com):

使用COM浏览器,我可以导航找到我想要使用的类型库的GUID。

Using a COM Browser, I am able to navigate to find the GUID for the Type Library I am interested in using.

使用pythoncom,我可以轻松浏览此类型库,查看可用的对象。

Using pythoncom, I am able to easily navigate this type library to see what objects are available available.

>>> import win32com.client
>>> import pythoncom
>>> type_lib_iid = '{12345678-ABCD-EFGH-1234-12341234}'
>>> lib = pythoncom.LoadTypeLib(type_lib_iid, 1, 0)
>>> for index in range(0, lib.GetTypeInfoCount()):
        print lib.GetDocumentation(index)[0]
        print lib.GetTypeInfo(index).GetTypeAttr().iid

这会返回可用COM对象的所有GUID(对不起,如果我正在删除这个词汇表)。

This returns all the GUID's of the available COM objects (sorry if I am butchering this vocabulary).

奇怪的是,当我浏览类型库时,这个调用返回的所有对象都显示在COM浏览器中,只有81个可用的对象可以调用Dispatch。剩余返回:

Strangely, while all the objects that are returned by this call appear in the COM browser when I browse the type library, only 4 of the 81 that are available can I call Dispatch on. The rest return:

'Class not registered.'

我的问题分为两部分:

1)如何通过COM浏览器和通过上面的调用和不可调用使用win32com.client.Dispatch? [注意:有问题的应用程序正在运行。]

1) How is it possible that objects can be found both through a COM browser and through the above call and not be callable using win32com.client.Dispatch? [Note: the application in question IS running.]

2)我使用错误的函数? Dispatch是否不正确?我自由地承认,我发现Python COM模块的文档混淆,但我没有发现任何现有的COM对象没有调度,所以我不知道从哪里去。此外,可以发送的4个对象似乎完全按照文档中的说明工作。

2) Am I using the wrong function? Is Dispatch incorrect? I freely admit that I have found the documentation for the Python COM modules confusing, but I have not found any examples where existing COM objects were not Dispatched upon, so I do not know where to go from here. Furthermore, the 4 objects that CAN be dispatched upon seem to work exactly as explained in the documentation.

[最后一个注意:如果我尝试pythoncom.CoCreateInstance(TypeLibCLSID,None ,1,GUIDofInterfaceObject)我得到'接口不支持。我需要使用的关键接口是我在这里的GUID。]

[One last note: if I attempt pythoncom.CoCreateInstance(TypeLibCLSID, None, 1, GUIDofInterfaceObject) I get 'Interface not supported." The critical interface I need to use is the GUID I am trying here.]

推荐答案

只有CoClass接口可以直接使用Dispatch来创建,其他接口通常是通过深入了解顶级对象的属性和方法来创建的,如果你在typelib上运行makepy,生成的包装文件将具有CoClass标有评论。

Only CoClass interfaces can be created directly using Dispatch. The other interfaces are usually created by drilling down through the properties and methods of the top-level objects. If you run makepy on your typelib, the generated wrapper file will have the CoClass's marked with a comment.

这篇关于通过Python只能部分访问COM类型库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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