pythoncomole

Python instance to COM Object


I'm having a bit of a play with using COM objects with Python and have run into a small problem.

The COM object I am using has a method called SetCallback which takes a object, the mothod looks like this:

    [id(0x60010010)]
    void SetCallback([in] IDispatch* callbackobject);

Taken from MS OLE/COM Object viewer

Now if I create and instance of my COM app in python everything is fine until I try and pass an instance of a object to this method.

callback = Callback()
mycomobject.SetCallback(callback)

class Callback():
    def SetStatusText(self,status):
        print status

The error I get in the python window is: TypeError: The Python instance can not be converted to a COM object

When doing the same thing in C# I used to make my class def look like this:

[ComVisible(True)]
public class Callback 
{
   //some methods here
}

and the method call is the same as Python version.

Is there something like this I have to do in Python in order to use an instance of a object as a callback for a COM object.


Solution

  • Worked it out! Just needed to make my own COM server and register it, then create the object via Dispatch:

    class Callback():
        _public_methods_ = ['SetStatusText']
        _reg_progid_ = "mycomobject.PythonCallback"
        _reg_clsid_ = "{14EF8D30-8B00-4B14-8891-36B8EF6D51FD}"
        def SetStatusText(self,status):
            print status
    
    if __name__ == "__main__":
        print "Registering COM server..."
        import win32com.server.register
        win32com.server.register.UseCommandLine(Callback)
        main()
    

    in main()

    callback = Dispatch("mycomobject.PythonCallback")
    #callback.SetStatusText("Hello World")
    mycomobject.SetCallback(callback)