将函数作为参数传递给 COM 对象函数

发布于 2024-12-17 22:25:06 字数 1157 浏览 0 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2024-12-24 22:25:06

一个问题(也许不是唯一的问题)是 DeviceAdded 是一个类的方法,因此它需要一个 self 参数:

def DeviceAdded(self, device, UDN, calltype):

我认为您的第一个问题是在正确的轨道上例如,至少与您提供的链接中的 VBScript(和 C++ 链接)进行比较。

另外,您不应在每次注册回调时创建新的 GUID。调用一次pythoncom.CreateGuid并将结果粘贴到代码中:

# Use "print pythoncom.CreateGuid()" to make a new one.
_reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}"

One problem (maybe not the only one) is DeviceAdded is a method of a class, so it needs a self parameter:

def DeviceAdded(self, device, UDN, calltype):

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:

# Use "print pythoncom.CreateGuid()" to make a new one.
_reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文