I'm trying to build a com server as a stand-alone dll using comtypes and py2exe, using sample code found in the comtypes tutorial.
The stand-alone DLL builds fine with --bundle 2.
But the DLL cannot register.
Using "regsvr32 testComServer.dll" this debug trace is produced:

  Traceback (most recent call last):    File "boot_ctypes_com_server.py", line 52, in <module>   
    File "zipextimporter.pyc", line 82, in load_module   
    File "MyTypeLib.pyc", line 15, in <module>   
    File "comtypes\client\_generate.pyc", line 97, in GetModule   
    File "comtypes\typeinfo.pyc", line 480, in LoadTypeLibEx   
    File "\loewis\25\python\Modules\_ctypes\callproc.c", line 757, in GetResult   
  WindowsError   
  :    
  [Error -2147312566] Error loading type library/DLL   
   
  Traceback (most recent call last):   
    File "<string>", line 1, in <module>   
  NameError   
  :    
  name 'DllRegisterServer' is not defined   

The regsvr32 error Message Box says:
  DllRegisterServer in testComServer.dll failed
  Return code was: 0x80040201

Below are included the files to recreate the problem.
The server works if  just running 'MyTypeLib.py /registerserver', and a small test program (tester.py, below) runs fine.
But the stand alone DLL will not register.

What am I doing wrong? Below are included the files to recreate the problem.

I've seen that 'loewis' path "\loewis\25\python\Modules\_ctypes\callproc.c"
before when testing comtypes in python without py2exe, and cleaning out the comtypes/gen directory seems to fix it.

I realize this may not be a py2exe problem. Do py2exe and comtypes work together?

Thanks,
Gartenbauer


---------------------------
file MyTypeLib.idl:

import "oaidl.idl";
import "ocidl.idl";

[
        uuid(D258EBF0-6247-4590-AF9E-7326FD975D61),
        dual,
        oleautomation
]
interface IMyInterface : IDispatch {
        [id(1)] HRESULT MyMethod([in] INT a, [in] INT b, [out, retval] INT *presult);
};

[
      uuid(4E340492-C4BF-4AC8-B9F1-8816C8FE8C8A)
]

library MyTypeLib
{
        importlib("stdole2.tlb");

        [uuid(5B5A8EAA-9877-419D-BA67-960257A0A5B2)]
              coclass MyObject {
              [default] interface IMyInterface;
        };
};

---------------------------
file MyTypeLib.py:

from ctypes import *
import comtypes
import comtypes.server.localserver

# force inclusion by p2exe of these in the DLL
from win32com import *
from pythoncom import *
from win32com.server import *
from win32com.server.util import *
from win32com.server.policy import *
from pywintypes import *

from comtypes.client import GetModule
# generate wrapper code for the type library, this needs
# to be done only once (but also each time the IDL file changes)
GetModule("MyTypeLib.tlb")

from comtypes.gen.MyTypeLib import MyObject

class MyObjectImpl(MyObject):
    # registry entries
    _reg_threading_ = "Both"
    _reg_progid_ = "MyTypeLib.MyObject.1"
    _reg_novers_progid_ = "MyTypeLib.MyObject"
    _reg_desc_ = "Simple COM server for testing"
    _reg_clsctx_ = comtypes.CLSCTX_INPROC_SERVER | comtypes.CLSCTX_LOCAL_SERVER
    _regcls_ = comtypes.server.localserver.REGCLS_MULTIPLEUSE

    # Note the 'this' second parameter
    def MyMethod(self, this, a, b, presult):
        presult[0] = a + b
        return 0


if __name__ == "__main__":
    from comtypes.server.register import UseCommandLine
    UseCommandLine(MyObjectImpl)

---------------------------
file setup.py:

import py2exe
from distutils.core import setup

MyTypeLib = dict(
    modules = ["MyTypeLib"],
    other_resources = [("TYPELIB", 1, file(r"MyTypeLib.tlb", "rb").read())],
    dest_base = r"testComServer"
    )

setup(name="MyTypeLib",
      ctypes_com_server = [MyTypeLib],
      zipfile=None,
      )

---------------------------
file tester.py

from comtypes.client import CreateObject
x = CreateObject("MyTypelib.MyObject")
print x
print x.MyMethod(42, 5)