1

我正在尝试在 64 位 Windows 7 上使用 Python 2.7 创建一个简单的 COM 服务器,但我无法成功注册 DLL。我能够在 32 位 Windows XP 上使用 Python 2.6 成功地做到这一点。我还可以直接从 Python 注册我的课程。

这是我的模块 heikki.py,基于pywin32 COM 教程

class Heikki:
  _reg_clsid_ = '{F4C7D945-BF6B-4BF8-BCBB-EA021FCCE623}'
  _reg_desc_ = "Heikki"
  _reg_progid_ = "Heikki.TestServer"
  _public_methods_ = ['Hello']

  def __init__(self):
      pass

  def Hello(self):
      return 1

if __name__=='__main__':
    from win32com.server import register
    register.UseCommandLine(Heikki)

我可以像这样从命令行成功注册它(我有 64 位 Python 2.7.1,pywin32 214,py2exe 0.6.9,c:\python27 在 PATH 中):

C:\temp> python heikki.py
Registered : Heikki.TestServer

C:\temp> python heikki.py --unregister
Unregistered : Heikki.TestServer

然后,我在 c:\temp 中创建了一个基于py2exe COM 服务器示例的 setup.py 文件:

from distutils.core import setup
import py2exe

class Target:
    def __init__(self, **kw):
        self.__dict__.update(kw)
        # for the version info resources (Properties -- Version)
        self.version = "1.0"
        self.company_name = "My name"
        self.copyright = "(C) 2011, My company"
        self.name = "heikki"

heikki_target = Target(
    description = "my com server desc",
    # use module name for win32com exe/dll server
    modules = ["heikki"],
    # specify which type of com server you want (exe and/or dll)
    create_exe = False,
    create_dll = True
    )

setup(
    version = "1.0",
    zipfile=None,
    description = "my com server",
    name = "heikki",
    author="Heikki",
    com_server = [heikki_target],
    )

然后我创建 DLL:

c:\temp> python setup.py py2exe

但是,这会导致错误消息:

The following modules appear to be missing
['win32com.gen_py', 'win32com.shell', 'win32com.shell.shell']

我通过在安装脚本中添加以下内容来部分修复:

导入系统

if 1:
 try:
    import py2exe.mf as modulefinder
 except ImportError:
    import modulefinder
 import win32com
 for p in win32com.__path__[1:]:
     modulefinder.AddPackagePath("win32com", p)
 for extra in ["win32com.shell"]:
    __import__(extra)
    m = sys.modules[extra]
    for p in m.__path__[1:]:
        modulefinder.AddPackagePath(extra, p)

但我仍然得到:

The following modules appear to be missing
['win32com.gen_py']

我读到在某些情况下可以忽略此错误,因此我继续前进。此时我的 dist 目录如下所示:

07/13/2009  05:24 PM             3,072 API-MS-Win-Core-ErrorHandling-L1-1-0.dll
07/13/2009  05:24 PM             3,584 API-MS-Win-Core-LibraryLoader-L1-1-0.dll
07/13/2009  05:24 PM             4,096 API-MS-Win-Core-LocalRegistry-L1-1-0.dll
07/13/2009  05:24 PM             3,584 API-MS-Win-Core-Misc-L1-1-0.dll
07/13/2009  05:24 PM             4,608 API-MS-Win-Core-ProcessThreads-L1-1-0.dll
07/13/2009  05:24 PM             3,072 API-MS-Win-Core-Profile-L1-1-0.dll
07/13/2009  05:24 PM             4,096 API-MS-Win-Core-Synch-L1-1-0.dll
07/13/2009  05:24 PM             4,096 API-MS-Win-Core-SysInfo-L1-1-0.dll
07/13/2009  05:24 PM             6,144 API-MS-Win-Security-Base-L1-1-0.dll
11/27/2010  05:19 PM            80,384 bz2.pyd
07/13/2009  05:40 PM           207,360 CFGMGR32.dll
07/13/2009  05:40 PM            93,184 DEVOBJ.dll
01/06/2011  12:07 PM         2,363,665 heikki.dll
07/13/2009  05:41 PM           421,376 KERNELBASE.dll
11/06/2007  03:02 PM         1,671,160 mfc90.dll
07/13/2009  05:41 PM           167,424 POWRPROF.dll
11/27/2010  05:19 PM         2,978,816 python27.dll
07/05/2009  04:56 AM           489,984 pythoncom27.dll
07/05/2009  04:54 AM           138,240 pywintypes27.dll
11/27/2010  05:19 PM            10,752 select.pyd
07/13/2009  05:41 PM         1,899,520 SETUPAPI.dll
11/27/2010  05:19 PM           689,664 unicodedata.pyd
07/05/2009  04:55 AM           125,440 win32api.pyd
07/05/2009  04:58 AM           354,816 win32com.shell.shell.pyd
07/05/2009  04:54 AM            21,504 win32event.pyd
07/05/2009  04:54 AM            44,032 win32process.pyd
07/05/2009  04:54 AM            18,432 win32trace.pyd
07/05/2009  05:00 AM         1,034,752 win32ui.pyd
07/05/2009  04:55 AM           236,544 winxpgui.pyd
11/27/2010  05:22 PM           471,552 _hashlib.pyd
07/05/2009  04:54 AM             9,216 _win32sysloader.pyd

所以我尝试注册DLL:

c:\temp\dist> regsvr32 heikki.dll

但这会弹出一个对话框,上面写着:

The module "heikki.dll" was loaded but the call to
DllRegisterServer failed with error code 0x80040201

根据MS 知识库

There was an error when regsvr32.exe invoked the entrypoint in the module specified in the command line.

然后我转向Dependency Walker,它给了我 2 个关于 heikki.dll 的警告:

IEFRAME.DLL
SHLWAPI.DLL

后一个我可以双击以获取更多信息,第一个说:

Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.

然后我在 regsrv32 上使用了 Dependency Walker;只需打开 regsvr32 就会对相同的文件发出警告。分析 heikki.dll 给出了以下错误:

<empty string>
ZLIB.PYD

在某些尝试中,我也遇到了 ieshims.dll 的错误,但我将 Internet Explorer 目录添加到 PATH 并将 DLL 复制到 dist 并摆脱了该错误。Dependency Walker 关于 <empty string>错误这样说:

which means GetProcAddress was called with an empty string

我也希望 zlib.pyd 错误是无害的,因为应该不再需要 zlib.pyd ,因为它应该是内置的 AFAIK。我尝试在我的 dist 目录中将 zlib1.dll 作为 zlib.pyd 删除,但是虽然这消除了 DDL 导入错误,但还有另一个关于 pyd 文件没有预期入口点的错误(initzlib 或其他东西,忘记了)。

在我的尝试中,我也无法摆脱 py2exe 的 gen_py 警告。我尝试了对 setup.py 文件以及 heikki.py 模块的各种调整,但没有成功。

在这一点上,我已经没有想法可以研究了。

4

1 回答 1

2

这看起来像是在 64 位 Windows 7 机器上使用 Python 构建 Com 服务器的副本。

将“import win32traceutil”添加到 heikki.py 的顶部并在第二个 Python 进程中运行 C:\Python27\Lib\site-packages\win32\lib\win32traceutil.py 以查看 heikki.dll 的回溯:

Traceback (most recent call last):
  File "boot_com_servers.py", line 37, in <module>
pywintypes.error: (126, 'GetModuleFileName', 'The specified module could not be found.')
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'DllRegisterServer' is not defined

更新:

在 64 位 Python 上的 py2exe 中有一个错误。由 py2exe 初始化的 sys.frozendllhandle 无效,导致 win32api.GetModuleFileName(sys.frozendllhandle) 失败。

您可能想在http://www.lfd.uci.edu/~gohlke/pythonlibs/#py2exe尝试修补的 py2exe 安装程序

于 2011-01-06T21:46:11.030 回答