I removed the lines as you suggested and now the DLL registers.
When the DLL is registered, the test script can't create an instance of the object.

Traceback (most recent call last):
  File "c:\code\comtypes\", line 2, in <module>
    x = CreateObject("MyTypelib.MyObject")
  File "c:\Python25\lib\site-packages\comtypes\client\", line 197, in CreateObject
    obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
  File "c:\Python25\lib\site-packages\comtypes\", line 1119, in CoCreateInstance
    _ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
WindowsError: exception: access violation reading 0x00000004

The access violation only occurs when the compiled DLL is registered, not when the python code is registered.
Checking in the debugger, the arguments to CoCreateInstance are the same in both cases.

I don't know my way around COM.
Thanks as always for your help.

On Wed, May 20, 2009 at 2:46 PM, Thomas Heller <> wrote:
H.R.Gartenbauer schrieb:
> 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
>   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

If you look into the first traceback, you can see the problem:
>     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

Line 15, on, is this one:

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

This line tries to load the type library FILE MyTypeLib.tlb, which
is not present in the current directory.  Also, calling GetModule('MyTypeLib.tlb')
is unneeded anyway because the wrapper module that it would generate IS
already inside your dll, put there by py2exe.

So, the simplest thing would be to delete this line from your script.

BTW:  You import a lot of modules into your script, they are also unneeded:

> # 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 *

comtypes doesn't use or need anything from pywin32/win32com/pythoncom, so you
should delete these lines.

> 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?

This combination should work together, since both packages are by the same author ;-).

> Thanks,
> Gartenbauer


Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables
unlimited royalty-free distribution of the report engine
for externally facing server and web deployment.
Py2exe-users mailing list