在python中实现COM接口类型库

编程入门 行业动态 更新时间:2024-10-27 08:37:06
本文介绍了在python中实现COM接口类型库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个插件,我试图创建一个应用程序的样本从我工作的公司。我试图用Python编写这个插件。

插件架构的工作方式是插件需要实现在提供的COM类型库中定义的接口。所以它是一个COM客户端到那个类型库,最终获得注册为COM服务器到注册表和应用程序通过给它的应用程序的后期绑定COM的ClassID。

我使用pythoncom和win32com并使用makepy.py从类型libarary生成所需的python代码,但我似乎找不到一种方法来创建一个类,实现从这个类型库的接口

感谢您。

$ b

b $ b

当我尝试运行Dispatch来获得COM对象时,我得到以下exeption:

> interface $ win32com.client.Dispatch('{68AC7909-804F-4D6D-861C-8382DAA7B029}')回溯(最近一次调用):文件< stdin>,第1行< module> 文件C:\Python26\lib\site-packages\win32com\client\__init __。py,第95行,在Dispatch dispatch中,userName = dynamic._GetGoodDispatchAndUserName(dispatch ,userName,clsctx)文件C:\Python26\Lib\site-packages\win32com\client\dynamic.py,第108行,在_GetGoodDispatchAndUserName 中return(_GetGoodDispatch IDispatch,clsctx),userName)文件C:\Python26\Lib\site-packages\win32com\client\dynamic.py,第85行,位于_GetGoodDispatch IDispatch = pythoncom.CoCreateInstance(IDispatch,None,clsctx,pythoncom.IID_IDispatch) pywintypes_error:(-2147221164,'Class not registered',None,None)

解决方案

您可以使用 win32com.client.Dispatch():

object = win32com.client.Dispatch(Class.Name)

这是来自ActiveState快速入门指南的示例:

>> import win32com.client >>>> w = win32com.client.Dispatch(Word.Application)>>> w.Visible = 1 >>>> w < win32com.gen_py.Microsoft Word 8.0 Object Library.Application>

如果不起作用,可以使用 win32com.client.gencache .EnsureModule()以确保您已生成正确的缓存模块。

from win32com.client import从win32com.client导入调度 import gencache #这来自运行:makepy.py -iMicrosoft Excel 14.0对象库 gencache.EnsureModule ('{00020813-0000-0000-C000-000000000046}',0,1,7) obj = Dispatch(Excel.Application.14) #给出< win32com .gen_py.Microsoft Excel 14.0 Object Library._Application instance ...> print repr(obj)

与comtypes同样的东西

来自comtypes.client import CreateObject obj = CreateObject(Excel.Application)

I have a plugin that I'm trying to create as a sample for an application from the company I work for. I'm trying to write this plugin in Python.

The way the plugin architecture works is that the plugin needs to implement an interface defined in a provided COM type library. So it is a COM client to that type library and in the end gets registered as a COM Server to the registry and the application by giving it the ClassID for late-bound COM by the application.

I'm using pythoncom and win32com and have used makepy.py to generate the needed python code from the type libarary but I can't seem to find a way to create a class that implements the interface from this type library.

Any pointers on this would be greatly appreciated.

Thanks

When I try to run Dispatch to get the COM object I get the following exeption:

>>> interface = win32com.client.Dispatch('{68AC7909-804F-4D6D-861C-8382DAA7B029}') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) File "C:\Python26\Lib\site-packages\win32com\client\dynamic.py", line 108, in _GetGoodDispatchAndUserName return (_GetGoodDispatch(IDispatch, clsctx), userName) File "C:\Python26\Lib\site-packages\win32com\client\dynamic.py", line 85, in _GetGoodDispatch IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes_error: (-2147221164, 'Class not registered', None, None)

解决方案

You can simply use win32com.client.Dispatch():

object = win32com.client.Dispatch("Class.Name")

This is the example from ActiveState quick start guide:

>>> import win32com.client >>> w=win32com.client.Dispatch("Word.Application") >>> w.Visible=1 >>> w <win32com.gen_py.Microsoft Word 8.0 Object Library._Application>

If it doesn't work, you can use win32com.client.gencache.EnsureModule() to make sure you've generated the right cache module.

from win32com.client import Dispatch from win32com.client import gencache # This comes from running: makepy.py -i "Microsoft Excel 14.0 Object Library" gencache.EnsureModule('{00020813-0000-0000-C000-000000000046}', 0, 1, 7) obj = Dispatch("Excel.Application.14") # gives <win32com.gen_py.Microsoft Excel 14.0 Object Library._Application instance ...> print repr(obj)

The same thing with comtypes (simpler and supports custom interfaces)

from comtypes.client import CreateObject obj = CreateObject("Excel.Application")

更多推荐

在python中实现COM接口类型库

本文发布于:2023-11-16 13:31:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1604732.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口类型   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!