C# 无法通过python使用COM服务事件
我在使用某些windows服务中的事件时遇到问题(我没有访问源代码的权限,我只有文档)。其中一个事件(在文档中)定义为(C#): 其中C# 无法通过python使用COM服务事件,c#,python,events,com,win32com,C#,Python,Events,Com,Win32com,我在使用某些windows服务中的事件时遇到问题(我没有访问源代码的权限,我只有文档)。其中一个事件(在文档中)定义为(C#): 其中BarcodeScanEventHandler: public delegate void BarcodeScanEventHandler( Object sender, BarcodeScanEventArgs args ) 让我向您展示一个最小化的示例: import win32com.client cls="FooService" api = win32
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.
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_'
但这会带来错误:
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.
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_'
我试图调用添加条形码扫描:
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.
类IUnknown(\uuuu内置对象)
|最基本的COM接口。
|
|IUnknown的每个子类必须定义以下类属性:
|
|_iid_u-定义此接口标识符的GUID实例
|
|_methods_u-此接口的方法列表。
|
|“方法”列表必须按VTable顺序排列。方法是指定的
|使用STDMETHOD或COMMETHOD调用。
|
|此处定义的方法:
|
|AddRef(self)
|将内部refcount增加1并返回它。
|
|QueryInterface(自身、接口、iid=None)
|QueryInterface(接口)->实例
|
|释放(自我)
|将内部refcount减少1并返回它。
|
| ----------------------------------------------------------------------
|此处定义的数据描述符:
|
|_uuudict__
|实例变量字典(如果已定义)
|
|_uuuweakref__
|对象的弱引用列表(如果已定义)
|
| ----------------------------------------------------------------------
|此处定义的数据和其他属性:
|
|\uuuu元类\uuuuu=
|COM接口的元类。自动创建高级别
|来自CommMethod列表的方法。
现在我不知道该怎么办。我做错什么了吗?但您应该知道,以下所有代码都适用于InternetExplorer.Application
。这项服务有什么问题吗?或者,如果事件将被触发,我应该如何使用方法add_OnBarcodeScanned
调用我的python方法/类
谢谢您的时间。好问题!你找到解决办法了吗?@Pakman根据我记忆中的情况,我进行了深入的挖掘,我编写了自己的小库来使用COM服务。最后一个猜测是COM库有问题。最后,我无法使用该库(
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.