将函数作为参数传递给 COM 对象函数
我使用的是 UPnP 对象方法 IUPnPDeviceFinder
::[CreateAsyncFind][1]
,第三个参数是回调函数。
我尝试创建自己的 COM 对象,但该方法不会回调我的函数。这是我的代码:
class Callback():
_public_methods_ = ['DeviceAdded','DeviceRemoved', 'SearchComplete']
_reg_progid_ = "UPnP.PythonCallback"
_reg_clsid_ = pythoncom.CreateGuid()
def DeviceAdded (device, UDN, calltype):
print (device, ": ", calltype, " UDN: ", UDN)
return
win32com.server.register.UseCommandLine(Callback)
callserver = Dispatch("UPnP.PythonCallback")
deviceType = "urn:schemas-upnp-org:device:MediaServer:1"
devices = finder.CreateAsyncFind(deviceType,0, callserver)
finder.StartAsyncFind(devices)
实际上,我只需要这样的东西:
def DeviceAdded (device, UDN, calltype):
print (device, ": ", calltype, " UDN: ", UDN)
return
deviceType = "urn:schemas-upnp-org:device:MediaServer:1"
devices = finder.CreateAsyncFind(deviceType,0, DeviceAdded)
finder.StartAsyncFind(devices)
Microsoft 在 VBScript 上做了一个例子。这正是我想在Python上做的(http://msdn.microsoft.com/en-us/library/windows/desktop/aa381078(v=VS.85).aspx) 如何将函数作为 COM 对象的参数传递?
I'm using the UPnP Object Method IUPnPDeviceFinder
::[CreateAsyncFind][1]
and the third parameter is a callback function.
I've tried to create my own COM Object but the method don't call back my function. Here's my code:
class Callback():
_public_methods_ = ['DeviceAdded','DeviceRemoved', 'SearchComplete']
_reg_progid_ = "UPnP.PythonCallback"
_reg_clsid_ = pythoncom.CreateGuid()
def DeviceAdded (device, UDN, calltype):
print (device, ": ", calltype, " UDN: ", UDN)
return
win32com.server.register.UseCommandLine(Callback)
callserver = Dispatch("UPnP.PythonCallback")
deviceType = "urn:schemas-upnp-org:device:MediaServer:1"
devices = finder.CreateAsyncFind(deviceType,0, callserver)
finder.StartAsyncFind(devices)
Actually, I just need something like this:
def DeviceAdded (device, UDN, calltype):
print (device, ": ", calltype, " UDN: ", UDN)
return
deviceType = "urn:schemas-upnp-org:device:MediaServer:1"
devices = finder.CreateAsyncFind(deviceType,0, DeviceAdded)
finder.StartAsyncFind(devices)
Microsoft made a example on VBScript. That's exactly what I want to do on Python (http://msdn.microsoft.com/en-us/library/windows/desktop/aa381078(v=VS.85).aspx)
How can I pass a function as a parameter on a COM Object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个问题(也许不是唯一的问题)是
DeviceAdded
是一个类的方法,因此它需要一个self
参数:我认为您的第一个问题是在正确的轨道上例如,至少与您提供的链接中的 VBScript(和 C++ 链接)进行比较。
另外,您不应在每次注册回调时创建新的 GUID。调用一次
pythoncom.CreateGuid
并将结果粘贴到代码中:One problem (maybe not the only one) is
DeviceAdded
is a method of a class, so it needs aself
parameter:I think you are on the right track with your first example, at least when compared to the VBScript (and C++ link) in the link you provided.
Also, You shouldn't create a new GUID each time you register the callback. Call
pythoncom.CreateGuid
once and paste the result into the code: