如何启动 COM 服务器?代码在 Python 中 [英] How do I start a COM server? Code is in Python

查看:55
本文介绍了如何启动 COM 服务器?代码在 Python 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Python 代码作为 COM 服务器运行.最终我想运行一个可用的 RTD 服务器 here.但首先我想知道你到底需要做什么才能让任何 COM 服务器运行.所以让我们关注这个例子.

I want to run Python code as a COM server. Eventually I want to run an RTD server available here. But first I want to know what exactly you have to do to getting any COM server running. So let's focus on this example.

class HelloWorld:
    _reg_clsid_ = "{7CC9F362-486D-11D1-BB48-0000E838A65F}"

    _reg_desc_ = "Python Test COM Server"

    _reg_progid_ = "Python.TestServer"


    _public_methods_ = ['Hello']

    _public_attrs_ = ['softspace', 'noCalls']

    _readonly_attrs_ = ['noCalls']

    def __init__(self):
        self.softspace = 1
        self.noCalls = 0

    def Hello(self, who):
        self.noCalls = self.noCalls + 1
        # insert "softspace" number of spaces
        return "Hello" + " " * self.softspace + who


if __name__=='__main__':

    import win32com.server.register
    win32com.server.register.UseCommandLine(HelloWorld)

好的,这在没有错误并且服务器已注册的情况下起作用,因此它在 HKEY_CLASSES_ROOT 注册表中可用.但是我能用这个做什么?有人说你必须编译一个实例并有一个 .dll 或 .exe 文件.我还需要做什么?

Ok, this works in the way that there were no errors and server is registered, hence it is available in the HKEY_CLASSES_ROOT registry. But what can I do with this? Some say you have to compile a instance and have a .dll or .exe file. WHat else do I have to do?

推荐答案

好吧,我运行了您的示例.服务器的注册表项位于:

Well, I ran your example. The registry key for the server is at:

HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{7CC9F362-486D-11D1-BB48-0000E838A65F}

它有两个子键...一个用于 LocalServer32,一个用于 InProcServer32

It has two subkeys... one for LocalServer32 and one for InProcServer32

我在 Excel 中创建了一个简单的 VBA 宏:

I created a simple VBA macro in Excel:

Sub d()
Set obj = CreateObject("Python.TestServer")

MsgBox obj.Hello("joe")
End Sub

宏运行得很好.我的 Excel 版本是 64 位.我运行了宏,然后在显示消息框时启动了任务管理器.我可以看到 pythonw.exe 在后台运行.

Macro ran just fine. My version of Excel is 64-bit. I ran the macro and then fired up Task Manager while the message box was being displayed. I could see pythonw.exe running in the background.

我的 python 脚本和你的脚本之间的唯一区别可能是名称以及我添加了一行打印以确保我正在执行该函数:

The only difference between my python script and yours is probably the name and also that I added a line to print to make sure I was executing the function:

if __name__=='__main__':

    import win32com.server.register
    print("Going to register...")
    win32com.server.register.UseCommandLine(HelloWorld)

当我运行 64 位 csript.exe 测试时,它运行......正如预期......当我运行 32 位版本时它失败了.

When I ran the 64-bit csript.exe test, it worked... as expected... when I ran the 32-bit version it failed.

我知道为什么......有点......InProcServer32 的注册表项是 pythoncom36.dll

I know why...sort of... The registry entry for InProcServer32 is pythoncom36.dll

那不好.这是一条不完整的道路.我尝试修改 shell 上的路径变量以添加到系统上存在 DLL 的 3 个位置之一,但它不起作用.此外,尝试在 InProcServer32 中编码路径.这不起作用..一直说找不到文件.

That's no good. It is an incomplete path. I tried modifying the path variable on my shell to add to one of the 3 places where the DLL existed on my system, but it didn't work. Also, tried coding the path in the InProcServer32. That didn't work.. kept saying it couldn't find the file.

我运行了 procmon,然后我观察到它无法加载 vcruntime140.dll.在python下找到这些文件所在的目录,并添加到我的路径中.它走得更远.如果我足够关心,我可能会尝试更多.最终使用 procmon,我可以找到所有问题.但你可以这样做.

I ran procmon, and then I observerved that it couldn't load vcruntime140.dll. Found the directory under python where those files were, and added to my path. It got further along. If I cared enough, I might try more. Eventually using procmon, I could find all the problems. But you can do that.

我的简单解决方案是将 CLSID 的密钥 InProcServer32 重命名为 _InProcServer32.这是如何运作的?好吧,系统找不到 InProcServer32 所以它总是使用 LocalServer32——用于 32 位和 64 位进程.如果您需要处理过程中的速度,那么您需要通过使用 procmon 并坚持不懈地解决问题,直到您解决所有找不到文件的错误等.但是,如果您不需要进程的速度,那么使用 LocalServer32 可能会解决问题.

My simple solution was to rename the key InProcServer32 for the CLSID to be _InProcServer32. How does that work? Well, the system can't find InProcServer32 so it always uses LocalServer32--for 32-bit and 64-bit processes. If you need the speed of in process then you'd need to fix the problem by using procmon and being relentless until you solved all the File Not Found errors and such. But, if you don't need the speed of in process, then just using the LocalServer32 might solve the problem.

注意事项 我正在使用我的雇主限制访问的 Anaconda 发行版,我只能从员工商店安装它.天啊.

Caveats I'm using an Anaconda distro that my employer limits access to and I can only install it from the employee store. YMMV.

这篇关于如何启动 COM 服务器?代码在 Python 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆