1

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?

bruno
  • 91
  • 2
  • 12

1 Answers1

1

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}"
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • Yeah. the main problem here is that neither even the Callback is called. The "right" way to do it would be passing the deviceadded as a parameter without the class just like the Microsoft example does. But it returns the error "The Python instance can not be converted to a COM object" – bruno Nov 25 '11 at 18:23