[python-win32] COM problems when Python is not in registry

Guido Wesdorp mailings at johnnydebris.net
Wed Jul 30 13:15:18 EDT 2003


Hi there!

I'm writing a Windows installer for some application, and now I have 
some wierd COM problems. My application starts, but at some point it 
seems to lose the COM connection (resulting in an attribute error on the 
object returned, does a faulty COM initialization result None or so?). I 
don't have a clue why it does so, but it seems to happen consistently 
after a while.
The setup I use is somewhat strange, since I don't register any Python 
stuff to the registry, so also all COM modules and all are loaded 
through a code snippet (written by Mark Hammond, I think) that imports 
pywintypes22.dll and pythoncom22.dll using win32api instead of just 
importing them the usual way (which somehow doesn't work without using 
the registry). This seems to work okay, since part of the COM calls can 
be made, but after a while it still freezes (this might be at a point 
where the import code is called again, may be part of the problem). The 
code I use to import the dlls is pasted on the bottom of this posting.
Does anyone have a clue what the problem can be?
Cheers,

Guido Wesdorp, Infrae (Holland)

Code to import pywintypes22.dll and pythoncom22.dll:

------------------------------------------------------------------------------------------

import win32api, imp

def magic_import(modulename, filename):
        # win32 can find the DLL name.
        h = win32api.LoadLibrary(filename)
        found = win32api.GetModuleFileName(h)
        # Python can load the module
        mod = imp.load_module(modulename, None, found, ('.dll', 'rb',
                                imp.C_EXTENSION))
        # inject it into the global module list.
        sys.modules[modulename] = mod
        # And finally inject it into the namespace.
        globals()[modulename] = mod
        win32api.FreeLibrary(h)

try:
        import pywintypes
except ImportError:
        magic_import("pywintypes", "pywintypes22.dll")
try:
        import pythoncom
except ImportError:
        magic_import("pythoncom", "pythoncom22.dll")

------------------------------------------------------------------------------------------
Source: http://www.faqts.com/knowledge_base/view.phtml/aid/4408




More information about the Python-win32 mailing list