pythoncomtypespythoncom

The correct way to create a new instance using pythoncom and force early binding


Spent a little too much time trying to figure it out by myself... I'm working with a FEA app called Simcenter Femap. In my program I need to create N new instances of it after I get some data from base instance for some asyncio fun. Can't even start on the asyncio part because I can't force early binding on new instances. What is working for me at this point:

  1. Created a makepy wrapper, called it PyFemap as Femap help is suggesting, made an import

  2. Connected to a running instance

     femap_object = pythoncom.connect('femap.model')    
     feAppBaseInstance  = PyFemap.model(femap_object)
    

Every method of every Femap object works perfectly fine after this.

  1. I am able to create instances using DispatchEx('femap.model') and invoke methods that don't require data conversion.

But for the rest of the methods to work I need to force early binding on these instances through already existing wrapper (as I see it).

"Python programming on win32" suggests that I use gencache.EnsureModule to create a wrapper and link it to created instance. But when I try to do it through type library's CLSID I get an error that it's not registered. Is there really no way to do it with a wrapper I already created?

Also tried to do all of this using comtypes. Some parts work better for me with it some are worse. But the end result is the same. If I may, I'd like to ask how to do it with comtypes too. Sorry if I'm missing something really obvious.


Solution

  • I'll post the solution since I solved the issue. It is actually really obvious. I ended up using pythoncom.New(...) for multiple instances, but I think other methods would work just as well if you only need one. The issue was that I didn't attach the wrapper head class (model) to these new instances, which was pretty silly of me.

    To create a new instance

    femap_object = pythoncom.new('femap.model')
    

    To assign a win32 wrapper (PyFemap) to it.

    new_instance  = PyFemap.model(femap_object)