Here's a batch file to build the dll:

del /s /f /q dist >nul
del /s /f /q build >nul

echo Compiling .idl file:
midl MyTypeLib.idl
if errorlevel 1 goto error

echo Building distribution...
python.exe py2exe --packages encodings --bundle 2
if errorlevel 1 goto error

cd dist
regsvr32 testComServer.dll

goto end

echo ERROR

On Tue, May 19, 2009 at 12:25 PM, H.R.Gartenbauer <> wrote:

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 "", 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   
  [Error -2147312566] Error loading type library/DLL   
  Traceback (most recent call last):   
    File "<string>", line 1, in <module>   
  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 ' /registerserver', and a small test program (, 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?


file MyTypeLib.idl:

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

interface IMyInterface : IDispatch {
        [id(1)] HRESULT MyMethod([in] INT a, [in] INT b, [out, retval] INT *presult);


library MyTypeLib

              coclass MyObject {
              [default] interface IMyInterface;


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)

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


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"

      ctypes_com_server = [MyTypeLib],


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