我在使用某些Windows服务中的事件时遇到问题(我无法访问源代码,我只有文档)。其中一个事件(在文档中)被定义为(C#):
public event BarcodeScanEventHandler OnBarcodeScanned
BarcodeScanEventHandler
:
public delegate void BarcodeScanEventHandler( Object sender, BarcodeScanEventArgs args )
让我告诉你一个最小化的例子:
import win32com.client
cls="FooService"
api = win32com.client.Dispatch(cls)
class ApiEvents:
def OnBarcodeScanned(self, value): #doesnt matter if its OnBarcodeScanned(self) or OnBarcodeScanned(self, value1, value2)
print(" * Barcode scanned!")
api.Initialize()
events = win32com.client.WithEvents(api, ApiEvents)
什么引发错误:
File "minevents", line 14, in <module>
events = win32com.client.WithEvents(api, ApiEvent
File "C:\Python27\lib\site-packages\win32com\client
WithEvents
raise ValueError("This COM object does not suppor
ValueError: This COM object does not support events.
我试图谷歌它,但没有有用的信息。之后我尝试了另一个库comtypes
,其代码如下:
import comtypes.client
api = comtypes.clientCreateObject("FooService")
comtypes.client.ShowEvents(api)
但它会引发错误:
File "<pyshell#5>", line 1, in <module>
comtypes.client.ShowEvents(api)
File "C:\Python27\lib\site-packages\comtypes\client\_events.py", line 218, in ShowEvents
return comtypes.client.GetEvents(source, sink=EventDumper(), interface=interface)
File "C:\Python27\lib\site-packages\comtypes\client\_events.py", line 193, in GetEvents
interface = FindOutgoingInterface(source)
File "C:\Python27\lib\site-packages\comtypes\client\_events.py", line 62, in FindOutgoingInterface
interface = comtypes.com_coclass_registry[clsid]._outgoing_interfaces_[0]
AttributeError: type object 'FooService' has no attribute '_outgoing_interfaces_'
我也试过了help(api)
并且有一些有趣的功能:
| add_OnBarcodeScanned(...)
| remove_OnBarcodeScanned(...)
我试图致电add_OnBarcodeScanned
:
class BarcodeScanned(object):
def QueryInterface(self, value):
help(value)
barcodeScanned = BarcodeScanned()
api.add_OnBarcodeScanned(barcodeScanned)
是什么让我IUnknown
:
class IUnknown(__builtin__.object)
| The most basic COM interface.
|
| Each subclasses of IUnknown must define these class attributes:
|
| _iid_ - a GUID instance defining the identifier of this interface
|
| _methods_ - a list of methods for this interface.
|
| The _methods_ list must in VTable order. Methods are specified
| with STDMETHOD or COMMETHOD calls.
|
| Methods defined here:
|
| AddRef(self)
| Increase the internal refcount by one and return it.
|
| QueryInterface(self, interface, iid=None)
| QueryInterface(interface) -> instance
|
| Release(self)
| Decrease the internal refcount by one and return it.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __metaclass__ = <class 'comtypes._cominterface_meta'>
| Metaclass for COM interfaces. Automatically creates high level
| methods from COMMETHOD lists.
现在我不知道该怎么办。难道我做错了什么?但是您应该知道以下所有代码都适用于InternetExplorer.Application
。这项服务有问题吗?或者您是否知道如果触发事件,如何使用方法add_OnBarcodeScanned
来调用我的python方法/类?
感谢您的时间。